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/STRBbyte; LDRBzero-extends to 32 bitsLDRSBsigned byte, sign-extended LDRH/STRHhalf-word (16 bits), zero-extended LDRSHsigned half-word
Example. If the byte in memory is 0xF0: LDRB r0, [r1] gives r0=0x000000F0 (240) and LDRSB r0, [r1] gives r0=0xFFFFFFF0 ().
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 r1afterwards[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/STRhas 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], #4reads the element and advances the pointer.LSL #2on 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, =labelloads the address of a variable (pseudo-instruction: the assembler places the address in a constant table near the code and generates a PC-relativeLDR).ADR r0, labelcomputes the address as PC + offset (nearby labels only).LDR r0, =1000000loads a constant that does not fit an immediate.
To read the contents of a variable two steps are needed:
LDR r1, =count @ r1 = &count
LDR r0, [r1] @ r0 = count
ADD r0, r0, #1
STR r0, [r1] @ count = count + 1
.data
count: .word 0Multiple transfers
Definition (multiple transfers LDM and STM).
LDM/STMload 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 registers and subtracts from sp.
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 returnsThe 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, xexpecting the address ofx: it gives the contents (PC-relative addressing). The address isLDR 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 0x000000F0 (240); LDRSB 0xFFFFFFF0 (). In STR r0,[r1] the first register is the source.
Addressing (r1 = 0x1000, r2 = 3)
[r1]0x1000;[r1,#8]0x1008;[r1,#-4]0x0FFC;[r1,r2]0x1003;[r1,r2,LSL #2]0x100C.r1unchanged.- Pre-indexed
[r1,#4]!: accesses 0x1004 and updatesr1 = 0x1004. - Post-indexed
[r1],#4: accesses 0x1000, thenr1 = 0x1004(walking an array). - 12-bit immediate offset (plus or minus 4095);
LSL #2turns 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 (); 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.