Salta al contenuto
Note per Studenti Arrays, Strings and Structures in ARM Assembly Report

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 nn elements of ss bytes occupies n⋅sn \cdot s consecutive bytes. Element ii is at address

base+i⋅s\text{base} + i \cdot s

Example. With a = {3, -1, 7, 2, 5} at address 0x2000, LDR r0, [r1, r2, LSL #2] with r2=3 reads address 0x2000+3⋅4=0x200C\mathrm{0x2000}+3\cdot4=\mathrm{0x200C} 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 →).

armasm
        .data
a:      .word 3, -1, 7, 2, 5      @ int a[5]
buf:    .space 20                 @ 20 uninitialised bytes
msg:    .asciz "ciao"             @ string with terminator 0

Sum of the elements, with an index

armasm
        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 = 16

Maximum, with a pointer

armasm
        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 = 7

The 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 55 bytes (terminator included) and returns 5−1=45-1=4.

Copy (strcpy(dst, src), dst in r0, src in r1):

armasm
copy:   LDRB r2, [r1], #1
        STRB r2, [r0], #1
        CMP  r2, #0
        BNE  copy
        BX   lr

Matrices

Formula (address of a matrix element, row-major). A matrix int m[R][C] is stored by rows: element (i,j)(i, j) is at

base+(i⋅C+j)⋅4\text{base} + (i \cdot C + j) \cdot 4

Example. With C = 3, m[2][1] is at base + (2⋅3+1)⋅4=(2 \cdot 3 + 1) \cdot 4 = base + 28.

Structures

The fields of a struct are stored in sequence, each aligned to its own size:

c
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] reads type, LDR r2, [r0, #4] reads x, STR r3, [r0, #8] writes y. 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 of BHS/BLO.

Versione ripasso

Arrays

Element ii of an array of ss-byte items: base+i⋅s\text{base} + i \cdot s. 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 until r2 = 5; result 16.
  • Maximum with pointer: LDR r3,[r1],#4; CMP r3,r0; MOVGT r0,r3; stop with CMP r1,r2 and BHS (addresses, unsigned); result 7.

Strings

Zero-terminated byte array. strlen: LDRB r2,[r1],#1; CMP r2,#0; BNE loop; result r1−r0−1r1 - r0 - 1 ("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 (i,j)(i,j) at base+(i⋅C+j)⋅4\text{base} + (i \cdot C + j) \cdot 4; C = 3, m[2][1] at base + 28.
  • struct { char type; int x; int y; }: offsets 0, 4, 8 (3 padding bytes after type), 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.

Teoria collegata