8

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
braden.groom
  • 305
  • 2
  • 5
  • 16

2 Answers2

7

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

datu-puti
  • 1,306
  • 14
  • 33
Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
  • 1
    I've searched for an answer on how to convert an integer to a char in ARM assembly. All of the results I get explain how to do it in C though. How would I go about doing this conversion in ARM without the C libraries? – braden.groom Jun 30 '13 at 21:16
  • Remember that characters are nothing more than numbers in the ASCII code. The number 48 corresponds to the char '0' in ASCII. The number 49 to '1' and so on. If you want to convert an integer to a char, therefore, you can add 48 to the integer, and then store the result as a byte (and not as an integer, as ints have 4 bytes) in some memory location. Then if you send a pointer of that memory location to the syscall write it will print the character. – Daniel Scocco Jul 01 '13 at 01:29
5

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.

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.

The +48 of course is to refer to the specific digit's ascii index number.

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

Tombas
  • 111
  • 1
  • 10
  • 1
    Many assemblers let you use ASCII constants in expressions, e.g. `#'0'` instead of `#48`. IDK if ARM assemblers allows this, but it `'0'` works with the GNU assembler for x86. Also, you don't need `.ascii` to be initialized, because you don't read it before writing. – Peter Cordes Feb 27 '16 at 09:00
  • Nice to know. I'll check that out, would have saved me a bit of bother! – Tombas Feb 27 '16 at 10:16