Arrays, Strings and Structures in ARM Assembly
In questa pagina 5
In questa pagina 3
Arrays
Formula (address of an array element). An array of elements of bytes occupies consecutive bytes. Element is at address
Example. With a = {3, -1, 7, 2, 5} at address 0x2000, LDR r0, [r1, r2, LSL #2] with r2=3 reads address and gives r0=2.
For int (4 bytes): LDR r0, [r1, r2, LSL #2] reads a[r2] with r1 = address of a (see ARM Memory-Access InstructionsLDR and STR for words, bytes and half-words with and without sign; addressing modes with immediate or scaled-register offset, pre-indexed with write-back and post-indexed, with effective address examples; loading addresses and constants; LDM and STM.ARM Memory-Access Instructions →).
.data
a: .word 3, -1, 7, 2, 5 @ int a[5]
buf: .space 20 @ 20 uninitialised bytes
msg: .asciz "ciao" @ string with terminator 0Sum of the elements, with an index
LDR r1, =a @ base
MOV r2, #0 @ i
MOV r0, #0 @ sum
loop: CMP r2, #5
BGE end
LDR r3, [r1, r2, LSL #2] @ r3 = a[i]
ADD r0, r0, r3
ADD r2, r2, #1
B loop
end: @ r0 = 3 - 1 + 7 + 2 + 5 = 16Maximum, with a pointer
LDR r1, =a
ADD r2, r1, #20 @ r2 = address after the last element
LDR r0, [r1], #4 @ max = a[0], r1 advances
loop: CMP r1, r2
BHS end @ comparing addresses: unsigned
LDR r3, [r1], #4
CMP r3, r0
MOVGT r0, r3
B loop
end: @ r0 = 7The post-incremented pointer avoids the multiplication of the index: this is what a compiler does with for (p = a; p < a + 5; p++).
Strings
Definition (C string and length). A C string is an array of bytes terminated by 0. Length (
strlen):strlen: MOV r1, r0 @ r1 walks the string, r0 = start loop: LDRB r2, [r1], #1 CMP r2, #0 BNE loop SUB r0, r1, r0 SUB r0, r0, #1 @ r1 went past the terminator too BX lr
Example. With the string "ciao" the loop reads bytes (terminator included) and returns .
Copy (strcpy(dst, src), dst in r0, src in r1):
copy: LDRB r2, [r1], #1
STRB r2, [r0], #1
CMP r2, #0
BNE copy
BX lrMatrices
Formula (address of a matrix element, row-major). A matrix
int m[R][C]is stored by rows: element is at
Example. With C = 3, m[2][1] is at base + base + 28.
Structures
The fields of a struct are stored in sequence, each aligned to its own size:
struct point { char type; int x; int y; };| Field | Offset | Size |
|---|---|---|
type |
0 | 1 (+ 3 padding bytes) |
x |
4 | 4 |
y |
8 | 4 |
Definition (struct fields). Total size 12 bytes. With
r0= address of the struct:LDRB r1, [r0]readstype,LDR r2, [r0, #4]readsx,STR r3, [r0, #8]writesy. An array of structs is walked with step 12.
Example. With r0=0x2000, LDR r2, [r0, #4] reads field x at address 0x2004 and STR r3, [r0, #8] writes y at 0x2008.
Typical mistakes
- Step 1 instead of 4 for arrays of
int, or step 4 instead of 1 for strings. - Forgetting the alignment padding when computing struct offsets.
- Comparing pointers with
BGE/BLT(signed) instead ofBHS/BLO.
Versione ripasso
Arrays
Element of an array of -byte items: . For int: LDR r0,[r1,r2,LSL #2] reads a[r2] (ARM Memory-Access InstructionsLDR and STR for words, bytes and half-words with and without sign; addressing modes with immediate or scaled-register offset, pre-indexed with write-back and post-indexed, with effective address examples; loading addresses and constants; LDM and STM.ARM Memory-Access Instructions →). With a = {3,-1,7,2,5} at 0x2000, r2 = 3 gives address 0x200C, value 2. Directives: .word 3,-1,7,2,5, .space 20, .asciz "ciao".
- Sum with index:
LDR r3,[r1,r2,LSL #2];ADD r0,r0,r3;ADD r2,r2,#1; loop untilr2 = 5; result 16. - Maximum with pointer:
LDR r3,[r1],#4;CMP r3,r0;MOVGT r0,r3; stop withCMP r1,r2andBHS(addresses, unsigned); result 7.
Strings
Zero-terminated byte array. strlen: LDRB r2,[r1],#1; CMP r2,#0; BNE loop; result ("ciao" reads 5 bytes, gives 4). strcpy: LDRB/STRB with post-increment until the zero is copied.
Matrices and structs
- Row-major
int m[R][C]: element at ;C = 3,m[2][1]at base + 28. struct { char type; int x; int y; }: offsets 0, 4, 8 (3 padding bytes aftertype), size 12;LDRB r1,[r0],LDR r2,[r0,#4],STR r3,[r0,#8]; array of structs step 12.
Typical mistakes: step 1 instead of 4 for int (or 4 instead of 1 for strings); forgetting padding; BGE/BLT on pointers instead of BHS/BLO.