0

I have the created a minimal example that displays my problem.

I am reading user input (extracted for neatness and replaced with a static int) and printing the integer to the terminal through a "ToString" method. However is is printing extra details


.global _start


//; r0 the int to display
str:
    push {lr}
    push {r4-r11}

    mov r2, #0x0
    mov r3, #1000
    mov r7, #10

str_loop:
    mov r4, #0x0
    udiv r4, r0, r3
    add r4, r4, #0x30

    ldr r5, =to_string
    add r5, r5, r2
    strb r4, [r5]
    add r2, r2, #1

    sub r4, r4, #0x30
    mul r6, r4, r3

    sub r0, r0 ,r6

    udiv r6, r3, r7
    mov r3, r6
    cmp r3, #0
    beq return_str
    b str_loop

return_str:
    mov r4, #0xa
    ldr r5, =to_string
    add r5, r5, r2
    add r5, r5, #1
    strb r4, [r5]
    pop {r4-r11}
    pop {pc}

display:
    push {lr}
    push {r4-r11}
    mov r7, #0x4
    mov r0, #0x1
    ldr r1, =to_string
    mov r2, #0x8
    svc 0x0
    pop {r4-r11}
    pop {pc}


_start:
    mov r0, #999

    bl str
    bl display

    //; exit program
    mov r7, #0x1
    mov r0, #0
    svc 0x0

.section .data

to_string:
    .skip 10

When I execute this code after compilation using

arm-linux-gnueabi-as assignment.asm -o assignment.o
arm-linux-gnueabi-gcc-11 assignment.o -o assignment -static -nostdlib

It displays 0999 to the terminal I don't want the leading zero so I would like it to print just "999" to the screen.

How do I do that?

MSalters
  • 173,980
  • 10
  • 155
  • 350
JPHamlett
  • 365
  • 3
  • 9
  • 1
    Change your routine so it skips leading zeroes. – fuz Oct 31 '22 at 16:50
  • Make it stop *before* a store when the quotient is zero, except on the first iteration if the incoming value was zero. As in the C in [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) (You want `0` to produce `"0"` not the empty string.) – Peter Cordes Oct 31 '22 at 19:21
  • Also, write the bottom of a `do{}while(n != 0)` loop as `cmp` / `bne str_loop`, not a conditional branch to skip forward over an unconditional `b` back to the top. In general never do that in asm, always write one conditional branch that goes to the other place or falls through. – Peter Cordes Nov 01 '22 at 03:34

0 Answers0