Course: Computer Architectures · Source lecture: ARM v7-M Architecture, Directives and Memory
1. Address, register and stored value
A register can hold an address, but it does not become memory. If r0=0x9000 and memory at 0x9000 contains 17:
MOV r1, r0 ; r1 = 0x9000, copy the address
LDR r2, [r0] ; r2 = 17, read memory at that addressThe brackets mean “use the value in the base register as a memory address.”
2. Width and little-endian assembly
Each memory address identifies one byte. A word occupies four consecutive addresses. With:
| Address | Byte |
|---|---|
0x9004 | 0x11 |
0x9005 | 0x22 |
0x9006 | 0x33 |
0x9007 | 0x44 |
and r0=0x9004:
LDRB r1, [r0] ; 0x00000011
LDRH r2, [r0] ; 0x00002211
LDR r3, [r0] ; 0x44332211Little-endian means the lowest-address byte becomes the least-significant byte. It does not reverse the addresses; it determines how the bytes are assembled into the register value.
3. Unsigned and signed loads
LDRB and LDRH zero-extend. LDRSB and LDRSH sign-extend.
For byte 0xFE, unsigned interpretation is 254 and signed interpretation is 254-256=-2:
LDRB -> 0x000000FE
LDRSB -> 0xFFFFFFFEFor halfword 0x8001, the sign bit is one, so LDRSH produces 0xFFFF8001. The sign comes from the highest bit of the entire loaded width, not merely from the first byte.
4. Stores
Stores copy the low part of a register into memory. With r1=0xAABBCCDD:
STRB r1, [r0] ; writes DD
STRH r1, [r0] ; writes DD then CC
STR r1, [r0] ; writes DD, CC, BB, AAThe register value is not changed. In little-endian memory, the rightmost byte is written to the lowest address.
5. Offsets and writeback
LDRB r1, [r0, #1] ; read r0+1; r0 unchanged
LDRB r1, [r0, #1]! ; read r0+1; update r0 to r0+1
LDRB r1, [r0], #1 ; read old r0; then update r0 to r0+1Starting from r0=0x9004, the first form reads 0x9005 and leaves r0=0x9004; the second reads 0x9005 and leaves r0=0x9005; the third reads 0x9004 and leaves r0=0x9005.
The transfer instruction decides the width. The offset decides pointer movement. Thus LDRB r1,[r0],#4 reads one byte but advances four bytes.
6. Register offsets and shifts
A register may supply the offset:
LDR r1, [r0, r2]
LDR r1, [r0, r2, LSL #2]The second form uses r2 << 2, which is r2 × 4 for the usual non-overflowing positive indexes. It does not modify r2 or r0.
7. Arrays and lookup tables
The general formula is:
address = base + index × bytes_per_elementFor a byte array at 0x9000, index 3 is 0x9003. For a word array, index 3 is 0x9000 + 3×4 = 0x900C. A halfword array uses two-byte steps.
The source’s byte lookup table stores 1,4,9,...,121; LDRB r4,[r0,r2] retrieves the value for index x. Its word factorial table stores 1,1,2,6,24,120,720,...; LDR r4,[r0,r2,LSL #2] retrieves x!.
8. Multiple transfers
LDRD loads two words and STRD stores two words. LDMIA r0!,{r1,r2,r3,r4} loads four words at r0, r0+4, r0+8, and r0+12, then advances r0 by 16 bytes. STMIA stores the reverse direction. I means increasing addresses; D decreasing; A means after adjustment; B before adjustment.
9. Memory map and memory-mapped I/O
The Cortex-M3 map includes code, SRAM, peripheral, external and system regions. A peripheral register has an address, so ordinary loads/stores can read status or trigger a hardware action. This is memory-mapped I/O. Cacheable and bufferable attributes matter because a stale or delayed peripheral access can describe the wrong hardware state.
Common confusions
r0can contain0x9000;[r0]means the memory at0x9000.#4is an offset of four bytes; it does not change a byte load into a word load.0x900Cis index 3 only for a four-byte word array, not a byte array.LDRBdoes not preserve a negative signed byte; useLDRSBfor sign extension.
Retrieval checks
Q: What does LDRB r2,[r0],#1 do twice from r0=0x9004?
A: It reads 0x9004, then 0x9005; afterward r0 is 0x9006.
Q: What is LDR r1,[r0] for bytes 11 22 33 44 at increasing addresses?
A: 0x44332211.
Q: What is the word-array address of index 5 from base 0x9000?
A: 0x9000 + 5×4 = 0x9014.