How to combine C program with external ASM

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi

i want to combine a c file whit asm file in avrstudio5
well,i creat a c file and wrie c code then creat a asm file in this project and write asm code like this follow:

c code :

#include 
volatile uint8_t pinbits; //define poer pins
extern void InitPort(void);//init port for output
extern void sendpinbits(void);//send to port
int main()
{
	pinbits=1;//set pin 1 as output
	InitPort();//call subroutine from assembler file
	while(1)//repeat for ever
	{
	pinbits=1;//Pin High
	sendpinbits();//call subroutine from assembler file
	pinbits=0;//Pin Low
	sendpinbits();//call subroutine from assembler file
	}
}

asm code :

.include "m16def.inc" 
.extern pinbits					;external variable
.global InitPort				;make accesible globally
InitPort:					;function name  
	push r18				;save register value
	lds r18, pinbits			;load variable to r18
	out _SFR_IO_ADDR(DDRD), r18	;set pins as output
	pop r18				;restore register
	ret					;return from subroutine
.global sendpinbits				;make global
sendpinbits:					;function name
	push r18				;save register value
	lds r18, pinbits			;load variable to r18
	out _SFR_IO_ADDR(PORTD), r18	;pins to High
	pop r18				;restore register
	ret					;return from subroutine

then in project propertise in toolchian tab import flag of asm file like this follow:

but when i want to assmble software unknown .extern and .global in asm file !????

thanks than you'r attention

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you using the Atmel assembler or the gcc assembler?

.include "m16def.inc" 

This is for the Atmel assembler, you need to use for the gcc assembler.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Are you using the Atmel assembler or the gcc assembler?

excusme i don't know .i creat a new file in avr gcc and i can't recognition i use Atmel assembler or the gcc assembler
how i to understand i use which assembler?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here we go again with mixed C and assembler. I'll raise the sceptical questions, and am prepared to take the heat..

In this case the OP does not know enough to even discern between Atmels AVR Assembler and the GNU avr-as assembler.

And the example of what he wants to do in assembler is trivial, and could just as well (or perhaps even better) be done in C.

So, mojtaba_led, why do you want to mix C and assembler? It can't be for doing what you sketch above. You have another problem you think needs mixed C/assembler? Then please tell us about it.

Or is this just about being curious and looking for a learning experience? That's quite OK.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A mixed project is easy in AS5 as long as you understand that you must use avr-as syntax for the Asm part and cannot use Atmel's own assembler?

Create a project for avr-gcc and you will get a .c file in the projectv automatically with main() in it. Now right click the project and use Add-New item... and just type something like myasm.S into the Name: box then click OK. Rather stupidly (yet another bug if you are listening Atmel!) the blank file is started with:

/*
 * myasm.c
 *
 * Created: 13/10/2011 09:08:46
 *  Author: Administrator
 */ 

Well that's wrong - it is NOT myasm.c it is myasm.S so correct the comment they got wrong. In the file you probably want to then enter something like:

/*
 * myasm.S
 *
 * Created: 13/10/2011 09:08:46
 *  Author: Administrator
 */ 
 #define __SFR_OFFSET 0
 #include 

	.global asmfunc

asmfunc:
	ldi R24, 0xAA
	ret

then in the C file use:

#include 

uint8_t asmfunc(void);

 int main(void){
	PORTB = asmfunc();
	while(1);
}

When run that outputs 0xAA to PORTB

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you very much

Quote:

Or is this just about being curious and looking for a learning experience? That's quite OK.

yes.

Quote:

Create a project for avr-gcc and you will get a .c file in the projectv automatically with main() in it. Now right click the project and use Add-New item... and just type something like myasm.S into the Name: box then click OK.

mean:
i do as follow :
file-->new-->project...
then choose avr gcc branch then c sub branch then write the name of file then ok ? like this follow:

and in countinue choose my device

then
software creat this :

/*
 * mix1.c
 *
 * Created: 10/13/2011 03:05:08 ب.ظ
 *  Author: mojtaba
 */ 

#include 

int main(void)
{
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

now i must do as follow:

file-->add-->new project...
then choose avr gcc branch then c sub branch and then write the name of asm file for example "mix1.S".like this follow:

then choose again my device anh then it creat :

/*
 * mix1.c
 *
 * Created: 10/13/2011 03:24:14 ب.ظ
 *  Author: mojtaba
 */ 

#include 

int main(void)
{
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

well , i must to change .c to .S in comment and write my c code in .c file and asm code in .S file ?

my sentences is right?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

my sentences is right?

No read what I wrote. In effect:

Oh for F**K's SAKE!! I'm afraid that's where my guide stops because that's where this POS decided to stop working.

(I'll get back to you when Atmel release a version of AS5 that works)

EDIT: actually it's not your fault we're trying to battle with a bug-fest. So I killed AS5 using process explorer and restarted and completed:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks
it's great!

Quote:

Oh for F**K's SAKE!! I'm afraid that's where my guide stops because that's where this POS decided to stop working.

(I'll get back to you when Atmel release a version of AS5 that works)


excuse me i do't understand you'r intention?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mojtaba!

clawson only got frustrated at the AVR Studio 5 locking up (notice the market "Not responding" it the title bar of the screen dump). He aired his frustration in public. We do that from time to time since, in ouy views, AVR Studio 5 is a POS ("piece of shit") that should not have been released because we perceive so many malfunctions in it, and we hear nothing at all about if the AVR Studio 5 team even are aware of what we find, or if they agree with us on there being much left to do and fix. There is not een a proper place to report bugs.

At least that is why I think clawson aired his frustration.

It was in no way aimed at you.

You can disregard his comment on the malfunction of AVR Studio 5, and continue reading at the next screen dump.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

excuse me i have another question:
in winavr i edit makefile as follow :
<<<<<<Makefile>>>>>>>>

# MCU name

MCU = atmega8

# Processor frequency.

F_CPU = 8000000

# Output format. (can be srec, ihex, binary)

FORMAT = ihex

# Target file name (without extension).

TARGET = mainC

# List Assembler source files here.

ASRC = extasm.S

in this case do not need to do this work?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You have a choice to make. Either you use that external Makefile so any maintenance of the project (such as files added/removed or change of target processor) are handled by you manually by editing that text file.

Or you embrace the IDE method of configuring a project. In which case you choose things like model to build for from pull down memu entries (etc.) and you don't need to worry about SRC= and ASRC= because simply listing a .c file in the project is the same as SRC=foo.c and listing a .S file in the project is the same as ASRC=bar.S