COMP2121 - Wk04
13th April 2009
Some general notes from an AVR Tutorial that I have found useful and need to reiterate.
- "Only the registers from R16 to R31 load a constant immediately with the LDI command, R0 to R15 don't do that.
- The first register is always the target register where the result is written to!
- Assembler directives always start with a dot in column 1 of the text. Instructions do NEVER start in column 1, they are always preceded by a Tab- or blank character! [There are no restrictions with respect to column placement of labels, directives, comments or instructions.]
- A very special extra role is defined for the register pairs R26:R27, R28:R29 and R30:R31. The role is so important that these pairs have extra names in AVR assembler: X, Y and Z. These pairs are 16-bit pointer registers, able to point to addresses with max. 16-bit into SRAM [(part of the data memory)] locations (X, Y or Z) or into locations in program memory (Z).
The lower byte of the 16-bit-address is located in the lower register, the higher byte in the upper register. Both parts have their own names, e.g. the higher byte of Z is named ZH (=R31), the lower Byte is ZL (=R30). These names are defined in the standard header file for the chips. Dividing these 16-bit-pointer names into two different bytes is done like follows:
.EQU Address = RAMEND ; RAMEND is the highest 16-bit address in SRAM LDI YH,HIGH(Address) ; Set the MSB LDI YL,LOW(Address) ; Set the LSB"¹
- Define names for registers with the .DEF directive, never use them with their direct name Rx.
- If you need pointer access reserve R26 to R31 for that purpose.
- 16-bit-counter are best located R25:R24.
- If you need to read from the program memory, e.g. fixed tables, reserve Z (R31:R30) and R0 for that purpose.
- If you plan to have access to single bits within certain registers (e.g. for testing flags), use R16 to R23 for that purpose.¹
Sections marked with ¹ are ©2002-2009 by http://www.avr-asm-tutorial.net, from Beginners Introduction to the Assembly Language of ATMEL AVR Microprocessors by Gerhard Schmidt 2003. Used under the supplied license, "You may use, copy and distribute these pages as long as you keep the copyright information with it."