我尝试编写一些 AArch64 ASM 代码,以十进制将数字打印到终端。该代码应该将输入数字除以 10000,将结果存储在缓冲区中,再次乘以 10000,然后从原始数字中减去结果以删除数字的第一位。然后重复 1000、100、10,最后是 1。这应该计算数字的 BCD 表示形式并将其存储在缓冲区中。
在gdb中调试代码时,计算似乎运行得很完美,但是在运行应该将第一个数字放入缓冲区的str指令之后,缓冲区中的值仍然是0。
.section .data
BCDBuffer:
.space 5 //Buffer of size 5
BCDBufferLen = . - BCDBuffer
.section .text
.global _start
_start:
ldr x1, =BCDBuffer
mov x0, #65535
//first compute BCD
//Ill do this for 16 Bit numbers, so 65535 is largest number (5 digits)
//Ill assume the input is in register w0 and the output ptr in register 1
mov x4, #10000
udiv w3, w0, w4
str b3, [x1]
umull x3, w3, w4
sub w0, w0, w3
add x1, x1, #1
mov x4, #1000
udiv w3, w0, w4
str b3, [x1]
umull x3, w3, w4
sub w0, w0 , w3
add x1, x1, #1
mov x4, #100
udiv w3, w0, w4
str b3, [x1]
umull x3, w3, w4
sub w0, w0, w3
add x1, x1, #1
mov x4, #10
udiv w3, w0, w4
str b3, [x1]
umull x3, w3, w4
sub w0, w0, w3
add x1, x1, #1
str b0, [x1]
sub x1, x1, #4
//now there should be a bcd version of the number at address x1.
//Numbers in ascii start at 48, so if I just increment all of these by 48, then Ill get a string with the chars
asciistuff:
ldr b0, [x1]
add w0, w0, #48
str b0, [x1], #1
ldr b0, [x1]
add w0, w0, #48
str b0, [x1], #1
ldr b0, [x1]
add w0, w0, #48
str b0, [x1], #1
ldr b0, [x1]
add w0, w0, #48
str b0, [x1], #1
ldr b0, [x1]
add w0, w0, #48
str b0, [x1]
sub x1, x1, #3
mov x0, #0 //STDOUT
//register 1 already has the right ptr
mov x2, #5 //Buffer Length
mov w8, #64 //Write syscall
svc #0
mov x0, #0
mov w8, #93
svc #0
此说明:
str b3, [x1]
没有做你期望它做的事情。这是一条浮点存储指令,将浮点寄存器 3(又名
b3
、h3
、s3
、d3
、q3
、v3
)的低字节存储到地址处的内存中在 x1
中表示。由于您之前没有将浮点寄存器 3 设置为任何有意义的值,因此该指令不会将任何有意义的值存储到内存中。
要存储寄存器
w3
/ x3
的低字节,请使用 strb
(存储字节)指令:
strb w3, [x1]