【发布时间】:2021-04-27 14:04:10
【问题描述】:
正如问题所述,我想在 aarch64 中打印多位数字。
通常在解决问题时,我首先使用 python 来查看它们是否有效,特别是因为我对任何组装都很陌生。我的 Pythonic 解决方案是
number = 1234
while number != 0:
digit = 1234 % 10 # always gives me the last digit
print(digit, end='') # to print the digit and not the next line
number = number - digit # to remove the digit I just got so the last digit is 0
number = number / 10 # to remove said last digit and shorten number by 1
我试图在 AARCH64 中实现这一点的方式:
printNumber:
mov x16, #10 /* apparently need this for udiv/msub */
udiv x14, x12, x16 /* x12 is the number I defined above, bc idk what registers are save to use (e.g. when syscall 64, print, happens, 0-8 are used) */
msub x13, x17, x16, x12 /* thanks to: https://stackoverflow.com/questions/35351470/obtaining-remainder-using-single-aarch64-instruction */
sub x12, x12, x13 /* x13 is what above is digit, x12 is number */
udiv x12, x12, x16
add x13, x13, #48 /* digit to string, possible error source 1 */
mov x0, #1 /* the print part */
mov x1, x13 /* the other part where I suspect a mistake */
mov x2, #1
mov w8, #64
svc #0
cmp x12, #0 /* the loop part */
beq exit /* genereric exit method I left out */
b printNumber
代码状态:它编译和运行没有问题,虽然它什么也没打印,但我无法调试,因为我使用实际的 aarch64 设备进行编程并且没有使用模拟器(尽管我确信有一种方法我不使用'不知道)
我希望很清楚我想要做什么,我知道一些问题,比如我应该将堆栈用于其中一些事情,但我希望我可以让它发挥作用。我也希望可以看到我为此付出了一些努力并试图寻找这样的解决方案,但找不到任何这样做的人,或者任何其他打印数字的非 c-lib 方式(请注意我不需要数字作为str,我真的只想打印它)
【问题讨论】:
-
write系统调用需要一个指向内存的指针。你需要把你的数字放入记忆中。 -
sub和第二个udiv不是必需的。 AArch64 中的无符号整数除法会向零截断,因此在第一个udiv之后,x14中已经有了number的新值。我认为 Python 的等价物是number = number // 10。此外,您可以在循环开始之前移动mov x16, #10;无需在每次迭代时重做。 -
@MaritnGe 然后是 Linux (Android)。考虑使用运行普通 Linux 的 ARM 设备,例如 Raspberry Pi。否则您以后可能会遇到麻烦,因为 Android 对允许运行的代码类型有一些限制。
-
是的,如果您想学习这样的基础知识,最好使用 sim 或开发平台。
-
@MaritnGe Termux 不是 Linux 模拟器。它是一个类似于您在桌面系统上使用的终端仿真器。您编写的程序直接在系统上运行,无需任何仿真。
标签: android assembly arm arm64