Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly create an array in ARM assembly?

I'm currently learning ARM assembly for a class and have come across a problem where I'd need to use an "array." I'm aware that there is no such thing as an array in ARM so I have to allocate space and treat that as an array. I have two questions.

  1. Am I correctly adding new values to the array or am I merely overwriting the previous value? If I am overwriting the values, how do I go about adding new values?
  2. How do I go about looping through the different values of the array? I know I have to use loop: but don't know how to use it to access different "indexes."

So far, this is what I've gotten from reading ARM documentation as I've gathered from resources online.

        .equ SWI_Exit,  0x11

        .text
        .global _start

_start: .global _start
        .global main

        b       main

main:
        ldr     R0, =MyArray
        mov     R1, #42
        str     R1, [R0], #4
        mov     R1, #43
        str     R1, [R0], #4
        swi     SWI_Exit

MyArray: .skip 20 * 4
        .end

As a side note, I am using ARMSim# as required by my professor, so some commands recognized by GNU tools won't be recognized by ARMSim#, or at least I believe that is the case. Please correct me if I'm wrong.

like image 876
allejo Avatar asked Oct 12 '13 05:10

allejo


1 Answers

  1. You are just overwriting elements. At this level there are "such things as arrays", but only fixed-sized, preallocated arrays. The .skip is allocating the fixed-size array.* A variable-sized, growable array would typically be implemented with more complex dyanamic memory allocation code using the stack or a heap.
  2. If you had a label like loop: (the actual name is arbitrary) you could branch (back) to it by using b loop. (Probably, you would want to do the branch conditionally so that the program didn't loop forever.) You can access different elements in the loop by changing R0, which you are already doing

Also the b main isn't really serving any purpose since it is branching to he next instruction. The code will do the same thing if you remove it.

[*] Alternately, you could say that your array is the just elements between MyArray and R0 (not including the memory R0 points to), in which, by changing R0 you are extending the array. But the maximum size Is still fixed by the .skip directive.

like image 53
scott Avatar answered Sep 18 '22 01:09

scott