gpt4 book ai didi

arrays - ARM 汇编数组

转载 作者:行者123 更新时间:2023-12-02 03:59:36 27 4
gpt4 key购买 nike

我试图弄清楚数组在 ARM 汇编中是如何工作的,但我只是不知所措。我想将一个大小为 20 的数组初始化为 0、1、2 等。

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

我什至不知道如何打印我必须看到的内容,如果我做得正确的话。这是我到目前为止所拥有的:

.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 让我很困惑,我不知道我做错了什么。如果有人可以帮助我更好地理解它是如何工作的,我将不胜感激。

最佳答案

这可能有助于其他想要了解如何使用 arm 汇编 语言为数组分配内存的人这是一个添加相应数组元素并存储在第三个数组中的简单示例。

.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

关于arrays - ARM 汇编数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42503417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com