ARM assembly print register value in decimal
I have an exponentiation subroutine that does the calculation and puts the result in r0. Currently I can print the result in hexadecimal, but I also want to print it in decimal. After doing a lot of searching online I haven't fo开发者_StackOverflowund a straightforward way of doing it. Seems like a simple task to do but I can't figure it out.
thanks
I'm tempted to say "link with a C library and call printf". If that's not an option, here is something that probably works, given a "putchar" function that outputs the ASCII character in r0. It repeatedly divides the number by 10, writing the digits into a small buffer on the stack, then outputs them in reverse order. It requires architecture 4M or better for the "umull" instruction.
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
精彩评论