如何在ARM程序集中正确创建数组?

时间:2022-03-31 12:50:00

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.

我目前正在学习一个类的ARM程序集,并遇到了一个我需要使用“数组”的问题。我知道ARM中没有数组这样的东西所以我必须分配空间并将其视为一个数组。我有两个问题。

  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. 我是否正确地向数组添加新值或者我只是覆盖了以前的值?如果我覆盖了这些值,我该如何添加新值?

  3. 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."
  4. 如何循环遍历数组的不同值?我知道我必须使用循环:但不知道如何使用它来访问不同的“索引”。

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

到目前为止,这是我从阅读ARM文档中获得的,因为我从在线资源中收集了这些文档。

        .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.

作为附注,我正在按照我的教授的要求使用ARMSim#,因此GNU工具识别的一些命令将无法被ARMSim#识别,或者至少我相信是这种情况。如果我错了,请纠正我。

1 个解决方案

#1


2  

  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. 你只是覆盖元素。在这个级别上有“像数组这样的东西”,但只有固定大小的预分配数组。 .skip正在分配固定大小的数组。*可变大小的可增长数组通常使用堆栈或堆来使用更复杂的动态内存分配代码来实现。

  3. 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
  4. 如果你有一个类似循环的标签:(实际名称是任意的)你可以使用b循环分支(返回)它。 (可能,您希望有条件地执行分支,以便程序不会永远循环。)您可以通过更改已经在执行的R0来访问循环中的不同元素

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.

b main也没有真正用于任何目的,因为它正在分支到他的下一个指令。如果删除它,代码将执行相同的操作。

[*] 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.

[*]或者,您可以说您的数组是MyArray和R0之间的唯一元素(不包括R0指向的内存),其中,通过更改R0,您正在扩展数组。但最大大小仍由.skip指令修复。

#1


2  

  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. 你只是覆盖元素。在这个级别上有“像数组这样的东西”,但只有固定大小的预分配数组。 .skip正在分配固定大小的数组。*可变大小的可增长数组通常使用堆栈或堆来使用更复杂的动态内存分配代码来实现。

  3. 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
  4. 如果你有一个类似循环的标签:(实际名称是任意的)你可以使用b循环分支(返回)它。 (可能,您希望有条件地执行分支,以便程序不会永远循环。)您可以通过更改已经在执行的R0来访问循环中的不同元素

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.

b main也没有真正用于任何目的,因为它正在分支到他的下一个指令。如果删除它,代码将执行相同的操作。

[*] 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.

[*]或者,您可以说您的数组是MyArray和R0之间的唯一元素(不包括R0指向的内存),其中,通过更改R0,您正在扩展数组。但最大大小仍由.skip指令修复。