ARM print register register value in decimal value

I have an exponential routine that calculates and puts the result in r0. I can currently print the result in hexadecimal, but I also want to print it in decimal. After much searching on the Internet, I did not find an easy way to do this. It seems to be a simple task, but I cannot understand.

thank

+3
source share
1 answer

I am tempted to say "link to the C library and call printf". If this is not an option, here is something that probably works, given the "putchar" function, which outputs the ASCII character to r0. He repeatedly divides the number by 10, writing the numbers into a small buffer on the stack, and then outputs them in the reverse order. The umull command requires 4M or better architecture.

print_decimal:
        stmfd   sp!, {r4,r5,lr}

        cmp     r0, #0
        moveq   r0, #'0'
        bleq    putchar
        beq     done

        mov     r4, sp
        mov     r5, sp
        sub     sp, sp, #12

        rsblt   r0, r0, #0          ; r0 = abs(r0)
        movlt   lr, #1              ; lr = negative ? 1 : 0
        movgt   lr, #0

        ldr     r1, =0x1999999a     ; r1 = 2^32 / 10

loop:   umull   r2, r3, r0, r1      ; r3 = r0 / 10
        sub     r2, r0, r3, lsl #3
        sub     r2, r2, r3, lsl #1  ; r2 = r0 - 10*r3 = r0 % 10

        add     r2, r2, #'0'
        strb    r2, [r4, #-1]!

        movs    r0, r3
        bne     loop

        cmp     lr, #0
        movne   r0, #'-'
        blne    putchar

write:  ldrb    r0, [r4], #1
        bl      putchar
        cmp     r4, r5
        blt     write

        add     sp, sp, #12
done:
        ldmfd   sp!, {r4,r5,lr}
        mov     r0, #'\n'
        b       putchar
+4
source

Source: https://habr.com/ru/post/1735222/


All Articles