dmarschal
Member
printhex:
stmdb sp!, {r0-r3,r7,lr} @ save regs
mov r2,#7 @ print out 8 numbers
sub sp,sp,#8 @ make room for the ascii string on stack
1:
and r3,r0,#0xf @ pick the lower nibble 0..f
mov r0,r0, ror #4 @ get the next nibble
cmp r3,#0xa
addlt r3,r3,#'0' @ convert to ascii 0..9 -> '0'..'9'
addge r3,r3,#'A'-0xa @ 0xa..0xf -> 'A'..'F'
strb r3,[sp,r2] @ store it on stack (reverse order!)
subs r2,r2,#1 @ do the 8 char loop
bpl 1b
mov r0,#console @ and write it on the console
mov r1,sp
mov r2,#8
invoke sys_write
add sp,sp,#8
ldmia sp!, {r0-r3,r7,pc} @ return
and here is a printdec routine:
Please define
"console",
"clr" macro,
"sys_write" and
'invoke" macro before calling the codes. You can find them in my earlier posts.
printdec handles negative numbers as well.
clr macro is here:
Here is an example how to call printdec from your code:
Have fun!
View attachment 12398
stmdb sp!, {r0-r3,r7,lr} @ save regs
mov r2,#7 @ print out 8 numbers
sub sp,sp,#8 @ make room for the ascii string on stack
1:
and r3,r0,#0xf @ pick the lower nibble 0..f
mov r0,r0, ror #4 @ get the next nibble
cmp r3,#0xa
addlt r3,r3,#'0' @ convert to ascii 0..9 -> '0'..'9'
addge r3,r3,#'A'-0xa @ 0xa..0xf -> 'A'..'F'
strb r3,[sp,r2] @ store it on stack (reverse order!)
subs r2,r2,#1 @ do the 8 char loop
bpl 1b
mov r0,#console @ and write it on the console
mov r1,sp
mov r2,#8
invoke sys_write
add sp,sp,#8
ldmia sp!, {r0-r3,r7,pc} @ return
and here is a printdec routine:
Code:
div10: .word 0x1999999a @ 2^32 / 10
printdec:
stmdb sp!, {r1-r5,r7,lr}
@ there is no DIV instruction for the ARM family
@ so here is the method:
clr r4 @ x/10 = x * (2^n/10) then ASR n (or use the upper 32 bits when n=32)
mov r5,sp
ldr r3,div10
cmp r0,r4
sub sp,sp,#12
rsblt r0,r0,r4
movlt r4,#'-'
11:
umull r1,r2,r0,r3
add r1,r2,r2, lsl #2
sub r1,r0,r1, lsl #1
dec r5
orr r1,r1,#'0'
strb r1,[r5]
movs r0,r2
bne 11b
cmp r4,#'-'
subeq r5,r5,#1
streqb r4,[r5]
mov r1,r5
add r2,sp,#12
mov r0,#console
sub r2,r2,r5
invoke sys_write
add sp,sp,#12
ldmia sp!, {r1-r5,r7,pc}
Please define
"console",
"clr" macro,
"sys_write" and
'invoke" macro before calling the codes. You can find them in my earlier posts.
printdec handles negative numbers as well.
clr macro is here:
Code:
.macro clr rreg
mov \rreg,#0
.endm
Code:
.globl _start
.include "macro.mac"
.data
.text
_start:
mov r0,#-15
bl printdec @ stores return address in LR
exit 0
.bss
.end
Have fun!
View attachment 12398
Last edited by a moderator: