如何在ARM汇编中打印一个数字? [英] How to print a number in ARM assembly?

查看:47
本文介绍了如何在ARM汇编中打印一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印一个我存储的数字.我不确定我是近还是远.任何帮助将不胜感激.这是我的代码:

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

它编译并运行,但我没有看到任何打印内容.据我了解,我需要在 r1 中从哪里开始打印的地址、r2 中的字节数、r0 中的文件描述符以及 r7 指定写入调用(如果它设置为 #4).我只是想存储#8,然后打印存储的数字.

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.

推荐答案

系统调用 write 将第二个参数 (r1) 作为指向要打印的字符串的指针.您将一个指向整数的指针传递给它,这就是它不打印任何内容的原因,因为您传递给它的内存区域上没有 ASCII 字符.

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.

您将在下面找到一个使用系统调用写入的Hello World"程序.

Below you'll find a "Hello World" program using the syscall write.

.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"

如果你想打印一个数字,你可以使用 C 库中的 printf 函数.像这样:

If you want to print a number you can use the printf function from the C library. Like this:

.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"

最后,如果你想用 syscall write 打印数字,你也可以实现一个 itoa 函数(一个将整数转换为字符串的函数).

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).

这篇关于如何在ARM汇编中打印一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆