How to print a number in ARM assembly?

后端 未结 2 1757
眼角桃花
眼角桃花 2021-01-18 17:55

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


        
2条回答
  •  一生所求
    2021-01-18 18:18

    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.

    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"
    

    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"
    

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

提交回复
热议问题