作者:爱情只有确定键没有取消键_874 | 来源:互联网 | 2023-09-23 09:13
I am trying to print a number that I have stored. I'm not sure if I am close or way off. Any help would be appreciated though. Here is my code:
我正在尝试打印我存储的数字。我不确定我是否亲近或离开。任何帮助将不胜感激。这是我的代码:
.data
.balign 4
a: .word 4
.text
.global main
main:
ldr r0, addr_of_a
mov r1, #8
str r1, [r0]
write:
mov r0, #1
ldr r1, addr_of_a
mov r2, #4
mov r7, #4
swi #0
bx lr
addr_of_a: .word a
It compiles and runs, but I don't see anything printed. From what I understand, I need the address of where to start printing in r1, how many bytes in r2, the file descriptor in r0, and r7 specifies the write call if it is set to #4. I am simply trying to store #8, then print the stored number.
它编译并运行,但我没有看到任何打印。根据我的理解,我需要在r1中开始打印的地址,r2中的字节数,r0中的文件描述符,以及r7如果设置为#4则指定写入调用。我只是想存储#8,然后打印存储的号码。
2 个解决方案
4
The syscall write takes on the second argument (r1) as a pointer to the string you want to print. You are passing it a pointer to an integer, which is why it's not printing anything, because there are no ASCII characters on the memory region you are passing to it.
syscall write将第二个参数(r1)作为指向要打印的字符串的指针。您正在向它传递一个指向整数的指针,这就是为什么它不打印任何东西,因为您传递给它的内存区域上没有ASCII字符。
Below you'll find a "Hello World" program using the syscall write.
下面你将找到一个使用syscall写的“Hello World”程序。
.text
.global main
main:
push {r7, lr}
mov r0, #1
ldr r1, =string
mov r2, #12
mov r7, #4
svc #0
pop {r7, pc}
.data
string: .asciz "Hello World\n"
If you want to print a number you can use the printf function from the C library. Like this:
如果要打印数字,可以使用C库中的printf函数。像这样:
.text
.global main
.extern printf
main:
push {ip, lr}
ldr r0, =string
mov r1, #1024
bl printf
pop {ip, pc}
.data
string: .asciz "The number is: %d\n"
Finally, if you want to print the number with the syscall write you can also implement a itoa function (one that converts an integer to a string).
最后,如果要使用syscall write打印数字,还可以实现itoa函数(将整数转换为字符串)。
3
Hi I appreciate that this is a pretty old thread but I've scratched my head over this for a while and would like to share my solution. Maybe it'll help someone along the way!
嗨,我很欣赏这是一个非常古老的主题,但我已经对此感到困惑,并希望分享我的解决方案。也许它会帮助一路上的人!
I was aiming to print to digit without recourse to using C++ in any way, though I realise that simply decompiling a tostring() - or whatever equivalent exists in C++ - and seeing what that came up with would have been a far quicker route.
我的目标是打印到数字而不用任何方式使用C ++,虽然我意识到只是反编译一个tostring() - 或者C ++中存在的任何等价物 - 并且看到它的结果将是一个更快的路线。
Basically I ended up with creating a pointer to an empty .ascii string in the section .data and added the digit that I wanted to print + 48 to it before printing off that digit.
基本上我最终在.data部分创建了一个指向空的.ascii字符串的指针,并在打印该数字之前添加了我要打印的数字+ 48。
The +48 of course is to refer to the specific digit's ascii index number.
+48当然是指具体数字的ascii索引号。
.global _start
_start:
MOV R8, #8
ADD R8, R8, #48
LDR R9, =num
STR R8, [R9]
MOV R0, #1
LDR R1, =num
MOV R2, #1
MOV R7, #4
SWI 0
.data
num:
.ascii: " "
The biggest drawback of this approach is that it doesn't handle any number more than one digit long of course.
这种方法的最大缺点是它当然不能处理超过一位数的任何数字。
My solution for that was much, much uglier and beyond the scope of this answer here but if you've a strong stomach you can see it here:
我的解决方案非常,更加丑陋,超出了这个答案的范围,但是如果你有强烈的胃,你可以在这里看到它: