ARM Assembly Arrays

Absiel picture Absiel · Feb 28, 2017 · Viewed 16.1k times · Source

I am trying to figure out how arrays work in ARM assembly, but I am just overwhelmed. I want to initialize an array of size 20 to 0, 1, 2 and so on.

A[0] = 0
A[1] = 1

I can't even figure out how to print what I have to see if I did it correctly. This is what I have so far:

.data
.balign 4       @ Memory location divisible by 4
        string: .asciz "a[%d] = %d\n"
        a:      .skip   80      @ allocates 20
.text
.global main
.extern printf

main:
        push    {ip, lr}        @ return address + dummy register
        ldr     r1, =a          @ set r1 to index point of array
        mov     r2, #0          @ index r2 = 0
loop:
        cmp     r2, #20         @ 20 elements?
        beq     end             @ Leave loop if 20 elements
        add     r3, r1, r2, LSL #2      @ r3 = r1 + (r2*4)
        str     r2, [r3]        @ r3 = r2
        add     r2, r2, #1      @ r2 = r2 + 1
        b       loop            @ branch to next loop iteration
print:
        push    {lr}            @ store return address
        ldr     r0, =string     @ format
        bl      printf          @ c printf
        pop     {pc}            @ return address

ARM confuses me enough as it is, I don't know what i'm doing wrong. If anyone could help me better understand how this works that would be much appreciated.

Answer

Abhishek D K picture Abhishek D K · Nov 20, 2018

This might help down the line for others who want to know about how to allocate memory for array in arm assembly language here is a simple example to add corresponding array elements and store in the third array.

.global  _start

_start:
      MOV R0, #5              
      LDR R1,=first_array     @ loading the address of first_array[0]
      LDR R2,=second_array    @ loading the address of second_array[0]
      LDR R7,=final_array     @ loading the address of final_array[0]
      MOV R3,#5               @ len of array
      MOV R4,#0               @ to store sum
check: 
      cmp R3,#1               @ like condition in for loop for i>1
      BNE loop                @ if R3 is not equal to 1 jump to the loop label
      B _exit                 @ else exit
loop:
      LDR R5,[R1],#4          @ loading the values and storing in registers and base register gets updated automatically R1 = R1 + 4 
      LDR R6,[R2],#4          @ similarly
      add R4,R5,R6         
      STR R4,[R7],#4          @ storing the values back to the final array 
      SUB R3,R3,#1            @ decrment value just like i-- in for loop
      B check
_exit:
      LDR R7,=final_array     @ before exiting checking the values stored 
      LDR R1, [R7]            @ R1 = 60 
      LDR R2, [R7,#4]         @ R2 = 80
      LDR R3, [R7,#8]         @ R3 = 100
      LDR R4, [R7,#12]        @ R4 = 120
      MOV R7, #1              @ terminate syscall, 1
      SWI 0                   @ execute syscall

.data
first_array:  .word 10,20,30,40  
second_array: .word 50,60,70,80 
final_array:  .word 0,0,0,0,0