我是汇编编程新手,正在为 ARM 编程。 我正在制作一个带有两个子例程的程序:一个子例程在内存中的字节向量上附加字节信息,另一个子例程打印该向量。向量的第一个地址包含后面的元素数量,最多 255 个。当我使用 GDB 对其进行调试时,我可以看到“appendbyte”子例程工作正常。但当涉及到“printvector”时,就出现了一些问题。首先,寄存器 r1 中加载的元素是错误的(它加载的是 0,而它应该是 7)。然后,当我使用“printf”函数后用GDB读取寄存器值时,很多寄存器得到了其他不应该改变的值,因为我没有修改它们,我只是使用了“printf”。为什么“printf”修改这些值。
我正在考虑一些关于对齐的事情。我不确定我是否正确使用了该指令。
完整代码如下:
.text
.global main
.equ num, 255 @ Max number of elements
main:
push {lr}
mov r8, #7
bl appendbyte
mov r8, #5
bl appendbyte
mov r8, #8
bl appendbyte
bl imprime
pop {pc}
.text
.align
printvector:
push {lr}
ldr r3, =vet @ stores the address of the start of the vector in r3
ldr r2, [r3], #1 @ stores the number of elements in r2
.align
loop:
cmp r2, #0 @if there isn't elements to print
beq fimimprime @quit subroutine
ldr r0, =node @r0 receives the print format
ldr r1, [r3], #1 @stores in r1 the value of the element pointed by r3. Increments r3 after that.
sub r2, r2, #1 @decrements r2 (number of elements left to print)
bl printf @call printf
b loop @continue on the loop
.align
endprint:
pop {pc}
.align
appendbyte:
push {lr}
ldr r0, =vet @stores in r0 the beggining address of the vector
ldr r1, [r0], #1 @stores in r1 the number of elements and makes r0 point to the next address
add r3, r0, r1 @stores in r3 the address of the first available position
str r8, [r3] @put the value at the first available position
ldr r0, =vet @stores in r0 the beggining address of the vector
add r1, r1, #1 @ increment the number of elements in the vector
str r1, [r0] @ stores it in the vector
pop {pc}
.data @ Read/write data follows
.align @ Make sure data is aligned on 32-bit boundaries
vet: .byte 0
.skip num @ Reserve num bytes
.align
node: .asciz "[%d]\n"
.end
问题出在
ldr r1, [r3], #1
和
bl printf
我希望我对这个问题很清楚。 提前致谢!
最佳答案
ARM ABI 指定寄存器 r0-r3 和 r12 在函数调用时被视为 volatile 。这意味着被调用者不必恢复它们的值。如果您使用 bl,LR 也会改变,因为 LR 将包含被调用函数的返回地址。
关于arm - Printf 更改寄存器中的值,ARM 汇编,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19359804/