Abs*_*iel 2 arrays assembly arm armv8
我试图弄清楚数组在 ARM 汇编中是如何工作的,但我只是不知所措。我想将一个大小为 20 的数组初始化为 0、1、2 等。
A[0] = 0
A[1] = 1
Run Code Online (Sandbox Code Playgroud)
我什至不知道如何打印我必须看到的内容,如果我做得正确的话。这是我到目前为止所拥有的:
.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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33498 次 |
| 最近记录: |