This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:piot:chapter_4_5 [2026/03/01 18:50] – [Program Memory Constant Addressing] ktokarz | en:multiasm:piot:chapter_4_5 [2026/03/01 19:43] (current) – [Addressing Modes] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Addressing Modes ====== | ||
| + | |||
| + | Addressing modes define how the processor accesses data and the target address of a jump. There are more than a dozen different addressing modes, such as: Direct Addressing, Indirect Addressing, Indirect with Displacement, | ||
| + | |||
| + | =====Direct Single Register Addressing===== | ||
| + | Direct single register addressing informs the processor that the instruction' | ||
| + | <code asm> | ||
| + | inc R0 | ||
| + | ldi R2, 0x05 | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | =====Direct Register Addressing, Two Registers===== | ||
| + | In this addressing mode, there are two register operands. They are contained in the source register (Rr) and destination register (Rd). The result is stored in the destination register (Rd). As it is shown in Fig {{ref> | ||
| + | <code asm> | ||
| + | add R2, R3 | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====I/O Direct Addressing===== | ||
| + | I/O direct addressing mode can be used to access the Special Function Register (SFR) from the internal peripheral address range. As presented in Fig {{ref> | ||
| + | <code asm> | ||
| + | in R2, SFR | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_3> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====Data Direct Addressing===== | ||
| + | To address the byte in a 64-kilobyte address space, 16 bits are required. In Direct Data Addressing mode, the address is contained in the 16 LSbs of a two-word instruction, | ||
| + | <code asm> | ||
| + | lds R2, variable | ||
| + | </ | ||
| + | |||
| + | Please note that in models of AVR microcontrollers supporting memory larger than 64kB, the LDS instruction uses the RAMPD register to access memory addresses above 64 KB. | ||
| + | <figure avr_addr_4> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====Data Indirect Addressing===== | ||
| + | Indirect addressing uses the content of an index register as a pointer to memory. As shown in Fig {{ref> | ||
| + | An example of the instruction is load data from memory addressed with an X pointer. | ||
| + | <code asm> | ||
| + | LD R2, X | ||
| + | </ | ||
| + | <figure avr_addr_5> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====Data Indirect Addressing with Pre-decrement===== | ||
| + | In the pre-decremented version of indirect addressing, the X,- Y-, or the Z-pointer is decremented before the operation. The operand address is the decremented contents of the X-, Y-, or Z-pointer. It is shown in Fig {{ref> | ||
| + | <code asm> | ||
| + | ST -X, R2 | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_6> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====Data Indirect Addressing with Post-increment===== | ||
| + | In addressing with post-increment, | ||
| + | <code asm> | ||
| + | LD R2, X+ | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_7> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====Data Indirect with Displacement===== | ||
| + | The addressing with displacement calculated the sum of two values: the index register and the constant. As shown in Fig {{ref> | ||
| + | <code asm> | ||
| + | LDD R2, Y+table | ||
| + | </ | ||
| + | <figure avr_addr_8> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====Program Memory Constant Addressing===== | ||
| + | With this addressing mode, it is possible to read the byte from the program memory. As shown in Fig {{ref> | ||
| + | |||
| + | The upper 15 bits (Most Significant bits - MSbs) select the word address (each word contains 2 bytes). | ||
| + | |||
| + | The lowest bit (Less Significant bit - LSb) selects which byte of that word is used: | ||
| + | |||
| + | * 0 → low byte, | ||
| + | |||
| + | * 1 → high byte. | ||
| + | |||
| + | <figure avr_addr_9> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | Instruction which reads the program memory is **lpm**. | ||
| + | <code asm> | ||
| + | LPM R2, Z | ||
| + | </ | ||
| + | |||
| + | When the **elpm** instruction is used, the RAMPZ register provides extra address bits so the processor can access larger program memories. | ||
| + | The **elpm** (Extended Load Program Memory) is an instruction used to read bytes from program memory when the memory is larger than 64 KB. | ||
| + | The normal **lpm** instruction can only access 64 KB because it uses the 16‑bit Z register. | ||
| + | The **elpm** adds extra address bits from the RAMPZ register, allowing access to extended program memory on larger AVR devices. | ||
| + | |||
| + | In short: | ||
| + | The **elpm** is equal to **lpm** with support for large program memory using RAMPZ. | ||
| + | |||
| + | |||
| + | =====Program Memory Addressing with Post-increment===== | ||
| + | <todo @pczekalski> | ||
| + | Program memory can be addressed with post-increment mode. In this mode, the Z pointer is automatically incremented after reading. | ||
| + | As shown in Fing {{ref> | ||
| + | The example of instruction is **lpm Z+**. If **elmp Z+** is used, the RAMPZ Register is used to extend the address in the Z register. | ||
| + | <code asm> | ||
| + | LMP Z+ | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_10> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====Store Program Memory Post-increment===== | ||
| + | Some versions of AVR microcontrollers allow for writing to the program memory (Flash). They implement the **spm** (Store Program Memory) instruction. Normally, Flash is read‑only during program execution, but **spm** allows the microcontroller to modify or update its own program, for example, inside a bootloader. | ||
| + | As visible in Fig {{ref> | ||
| + | |||
| + | <todo @mfojcik> | ||
| + | <figure avr_addr_11> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====Direct Program Memory Addressing===== | ||
| + | This addressing mode does not address data but rather modifies the program execution flow. It is used in jump and subroutine call instructions. These instructions use the constant immediate to specify the absolute target address to jump to. As shown in Fig {{ref> | ||
| + | <code asm> | ||
| + | CALL function | ||
| + | </ | ||
| + | |||
| + | <figure avr_addr_12> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <todo @mfojcik> | ||
| + | |||
| + | =====Indirect Program Memory Addressing===== | ||
| + | The target address of the jump or call can be stored in the Z pointer register. After **ijmp** or **icall**, program execution continues at the address contained by the Z register (i.e., the PC is loaded with the contents of the Z register). It is shown in Fig {{ref> | ||
| + | <code asm> | ||
| + | ICALL | ||
| + | </ | ||
| + | <figure avr_addr_13> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====Extended Indirect Program Memory Addressing===== | ||
| + | For versions of AVR microcontrollers with bigger program memory than 128 kB, additional bits used to extend the address are stored in the EIND register. Program execution continues at the address contained by the Z register and the EIND register (i.e., the PC is loaded with the contents of the EIND and Z register) as shown in fig {{ref> | ||
| + | |||
| + | <figure avr_addr_14> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====Relative Program Memory Addressing===== | ||
| + | If the target address is within the range of -2048 to 2047 from the current address, the shorter relative control transfer instructions can be used. | ||
| + | As shown in fig {{ref> | ||
| + | <code asm> | ||
| + | RJMP target | ||
| + | </ | ||
| + | <figure avr_addr_15> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||