next up previous contents
Next: Analogy for Addressing Modes Up: Memory Handling Previous: Reserving Memory   Contents

Loading an Address to X, Y or Z

There is no instruction to load a 16-bit constant to the register pairs X, Y or Z. Instead, you need to use to ldi instructions to load an addres to a register pair. When you load an address, it is important to know which register contains the most significant byte, and which register contains the least significant byte. For X, register 26 contains the least significant (right most) byte, and register 27 contains the most significant (left most) byte.

Two built-in functions of the assembler allow you to extract just the most significant byte and the least significant byte of a 16-bit value. high is a function that returns the most significant byte of a 16-bit value, and low is a function that returns the least significant byte of a 16-bit value.

In our previous example (to add one to the variable counter), we can use the following instructions:

.dseg
counter: .byte 1
.cseg
   ...
   ldi   r26,low(counter) ; r26 is the least sig. byte of addr. of counter
   ldi   r27,high(counter) ; r27 is the most sig. byte of addr. of counter
   ; at this point, X is the address of counter
   ld    r16,X  ; X is the VALUE of counter
   inc   r16    ; increment the value
   st    X,r16  ; store the incremented value
   ...



Tak Auyeung 2003-11-10