1

I have an ATMEL C project (ATmega328) in ATMEL Studio 7 and need to mix in some assembly code. I don't want to use the C inline assembly asm() directive because I want to keep it separate as a pure assembly file.

I want to include .asm to my project, but here is the thing.

For a C project it allows me to create .s or .S files. These are apparently using a different assembler than the .asm files because in .s files commands like .inc or .equ is not recognized.

However, if I rename the .s to .asm, ATMEL Studio won't invoke the assembler (as for a pure assembly project). I guess I could solve this with the Pre-Build event to manually invoke the right assembler for the .asm files.

I can stick with .s (although I prefer the .asm) file if there was a way to include all the I/O register definitions. #include <avr/io> itself doesn't generate an error but it doesn't define anything either.

#include <avr/io.h>

.global ReadFlux

ReadFlux:   
            ldi  r24,'#'
            out  TCNT0,r24               
            in   r24,TCNT0
            ret

...so TCNT0 is not known and generates an error. I need to get the assembly version of it; m328def.inc but that one can't be found and also use .equ, not recognized by the .s assembler.

What is the proper way to setup the .s so I don't have to manually define every single IO register? or (preferably, use the .asm instead).

EDIT
So there are two assemblers; avrasm2.exe and avr-gcc.exe with the assembler-with-cpp directive. In a pure Assembly project it seems like avrasm2.exe is used and in a C/C++ project the GNU assember is used. These 2 flawors are not working in the same way, hence the confusion.

For example with avrasm2.exe I can write like this:

out DDRB,r16

but with avr-gcc.exe I have to write it like this:

out _SFR_IO_ADDR(DDRB),r16

I think the mystery has been solved for now, but I still prefere the avrasm2.exe instead.

Max Kielland
  • 5,627
  • 9
  • 60
  • 95
  • I'm pretty sure that there will be some help in the documentation. Did you read it? -- The *.s or *.S files will probably be assembled by the C compiler's assembler, be it GCC or anyone else. -- The difference between *.s and *.S is commonly, that *.S (uppercase S) is preprocessed by the C preprocessor. You can use `#include` in *.S but not in *.s. – the busybee Dec 31 '20 at 16:26
  • Yes, I have done many assembly projects before, but that has been pure assembly projects. In this case the `.asm` is used. But in a C project it seems like another flawor of assembly is used. Documentation isn't up to date while some stuff works and some doesn't. using .S and #include (as I showed abobe) still doesn't work. – Max Kielland Dec 31 '20 at 17:46
  • The "avr/io.h" contains declarations in C, not assembly. If you look for declarations in assembly, there might be some,and then you can include these. – the busybee Jan 01 '21 at 10:22
  • Are the object files generated by avrasm2 compatible with object files generated by avr-gcc, so that you can link them? – the busybee Jan 01 '21 at 10:24

0 Answers0