Salta al contenuto
Note per Studenti ARM Memory-Access Instructions Report

ARM Memory-Access Instructions

In questa pagina 5
In questa pagina 4

In a load/store architecture (see The ARM Core ArchitectureThe 32-bit ARM core as the reference microcontroller core: registers r0-r15 with SP, LR and PC, the status register CPSR with flags NZCV, processor modes, the load/store model, conditional execution, the flexible second operand, Thumb, and the main differences of 64-bit ARMv8.The ARM Core Architecture →) data are brought into registers with a load, processed, and written back to memory with a store.

Loads and stores

Definition (LDR and STR instructions)

Instruction Effect
LDR Rd, [addr] loads a word (4 bytes)
STR Rd, [addr] stores a word
LDRB / STRB byte; LDRB zero-extends to 32 bits
LDRSB signed byte, sign-extended
LDRH / STRH half-word (16 bits), zero-extended
LDRSH signed half-word

Example. If the byte in memory is 0xF0: LDRB r0, [r1] gives r0=0x000000F0 (240) and LDRSB r0, [r1] gives r0=0xFFFFFFF0 (−16-16).

In STR r0, [r1] the first register is the source (it is read), not the destination.

Addressing modes

Examples with r1 = 0x1000, r2 = 3:

Definition (addressing modes of LDR and STR)

Syntax Effective address r1 afterwards
[r1] 0x1000 unchanged
[r1, #8] 0x1008 unchanged
[r1, #-4] 0x0FFC unchanged
[r1, r2] 0x1003 unchanged
[r1, r2, LSL #2] 0x1000 + 12 = 0x100C unchanged
[r1, #4]! (pre-indexed with write-back) 0x1004 0x1004
[r1], #4 (post-indexed) 0x1000 0x1004
  • The immediate offset for word or byte LDR/STR has 12 bits (plus or minus 4095).
  • Pre-indexed !: computes the address, accesses, then updates the base.
  • Post-indexed: accesses the address held in the base, then updates it. Handy for walking an array: LDR r0, [r1], #4 reads the element and advances the pointer.
  • LSL #2 on the index multiplies by 4: element index becomes byte offset for an array of words.

Example. With r1=0x1000: LDR r0, [r1, #4]! reads at 0x1004 and leaves r1=0x1004; LDR r0, [r1], #4 reads at 0x1000 and leaves r1=0x1004.

Addresses and constants

  • LDR r0, =label loads the address of a variable (pseudo-instruction: the assembler places the address in a constant table near the code and generates a PC-relative LDR).
  • ADR r0, label computes the address as PC + offset (nearby labels only).
  • LDR r0, =1000000 loads a constant that does not fit an immediate.

To read the contents of a variable two steps are needed:

armasm
        LDR r1, =count      @ r1 = &count
        LDR r0, [r1]        @ r0 = count
        ADD r0, r0, #1
        STR r0, [r1]        @ count = count + 1
        .data
count:  .word 0

Multiple transfers

Definition (multiple transfers LDM and STM). LDM/STM load or store a list of registers at consecutive locations. Suffixes: IA (increment after), IB, DA, DB (decrement before).

Example. STMDB sp!, {r4-r6, lr} saves 44 registers and subtracts 4⋅4=164\cdot4=16 from sp.

armasm
STMDB sp!, {r4-r6, lr}      @ = PUSH {r4-r6, lr}: sp -= 16, saves 4 registers
LDMIA sp!, {r4-r6, pc}      @ = POP  {r4-r6, pc}: restores and returns

The registers in the list go to memory in register-number order: the lowest register at the lowest address. Main use: saving and restoring registers in functions (see Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →) and copying blocks of memory.

On a microcontroller the same LDR/STR also reach the peripheral registers, which are mapped in the address space (see Input-Output Modules and Programmed Input-OutputWhy I/O modules are needed; functions of the module and status, control and data registers; memory-mapped and isolated I/O with examples; programmed I/O with busy waiting and its cost; comparison of the three techniques.Input-Output Modules and Programmed Input-Output →).

Typical mistakes

  • Using LDR r0, x expecting the address of x: it gives the contents (PC-relative addressing). The address is LDR r0, =x.
  • Walking an array of words with step 1 instead of 4.
  • Reading a signed byte with LDRB.

Versione ripasso

Loads and stores

LDR/STR word; LDRB/STRB byte; LDRSB signed byte; LDRH/STRH half-word; LDRSH signed. With byte 0xF0: LDRB →\to 0x000000F0 (240); LDRSB →\to 0xFFFFFFF0 (−16-16). In STR r0,[r1] the first register is the source.

Addressing (r1 = 0x1000, r2 = 3)

  • [r1] →\to 0x1000; [r1,#8] →\to 0x1008; [r1,#-4] →\to 0x0FFC; [r1,r2] →\to 0x1003; [r1,r2,LSL #2] →\to 0x100C. r1 unchanged.
  • Pre-indexed [r1,#4]!: accesses 0x1004 and updates r1 = 0x1004.
  • Post-indexed [r1],#4: accesses 0x1000, then r1 = 0x1004 (walking an array).
  • 12-bit immediate offset (plus or minus 4095); LSL #2 turns an index into a byte offset.

Addresses and constants

LDR r0,=label loads the address (pseudo-instruction with a nearby constant table); ADR computes PC + offset (nearby labels); LDR r0,=1000000 loads large constants. For the contents: LDR r1,=count, LDR r0,[r1], ADD, STR.

Multiple transfers

LDM/STM with IA, IB, DA, DB: STMDB sp!,{r4-r6,lr} == PUSH (sp−=16sp \mathrel{-}= 16); LDMIA sp!,{r4-r6,pc} == POP. Lowest register at the lowest address (Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →).

Typical mistakes: LDR r0, x gives the contents, the address is LDR r0, =x; step 1 instead of 4 for words; LDRB for a signed byte.

Teoria collegata