0% found this document useful (0 votes)
67 views1 page

Macros in AVR Assembler

The document discusses macros in AVR assembler code. Macros can make code more readable by replacing reused code or 16-bit calculations. Macros can take arguments that are replaced during assembly and cannot be changed at runtime. Arguments are only used in the form @0, @1, etc. and can represent many data types. Examples show how macros can replace instructions to simplify code.

Uploaded by

vr_xlent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views1 page

Macros in AVR Assembler

The document discusses macros in AVR assembler code. Macros can make code more readable by replacing reused code or 16-bit calculations. Macros can take arguments that are replaced during assembly and cannot be changed at runtime. Arguments are only used in the form @0, @1, etc. and can represent many data types. Examples show how macros can replace instructions to simplify code.

Uploaded by

vr_xlent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

http://www.avrbeginners.net/assembler/macros.

html

Macros in AVR Assembler


Macros are a good way to make code more readable, for example if it contains code that is often reused or if a lot of 16-bit
calculations are done.

Macros in AVR assembler can be defined everywhere in the code as long as they're not used at a location before the macro
definition. They can take arguments which are replaced during assembly and can't be changed during runtime. The
arguments can only be used in the form @0 or @1 (while 0 or 1 are the argument numbers startnig from 0). The arguments
can be almost everything the assembler can handle: integers, characters, registers, I/O addresses, 16 or 32-bit integers,
binary expressions...

This works:

.macro ldi16 ; lets make a macro for loading two registers


; with a 16-bit immediate
ldi @0, low(@2) ; load the first argument (@0) with the low byte of @2
ldi @1, high(@2) ; same with second arg (@1) and high byte of @2
.endmacro ; end the macro definition

ldi16 r16, r17, 1024 ; r16 = 0x00 r17 = 0x04

While this does not:

ldi16 r16, r17, 1024 ; r16 = 0x00 r17 = 0x04

.macro ldi16
ldi @0, low(@2)
ldi @1, high(@2)
.endmacro

Above, I wrote that arguments are replaced during assembly. The following should make it clear:

ldi16 r16, r17, 1024 ; in the macro, this was:


; is assembled to:
ldi r16, 0 ; ldi @0, low(@2)
ldi r17, 0x04 ; ldi @1, high(@2)

As I said, macros can also be used to replace 16-bit calculations. This is one example (along with ldi16):

.macro addi ; This is the "Add Immediate to register" instruction we all


subi @0, -(@1) ; missed in the instruction set!
.endmacro
;Now here's the 16-bit version:
.macro addi16
subi @0, low(-@2)
sbci @1, high(-@2)
.endmacro

Macros can of course be more complex, take more arguments and crash the assembler. If too many macros are defined in one
file, the last ones can't be found. I've had this with more than 7 I think. Just split them into more files, that helps sometimes. Or
just don't be that lazy and write the code yourself...

1 of 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy