Something simple provided below. It's not supposed to be optimal - just simple to describe.
The key thing to understand is that digits are generated in reverse when formatting numbers. So the least significant digit first. That means the string is filled *backwards*.
Code: Select all
*-------------------------------------------------------*
pushhex:
*-------------------------------------------------------*
; d0 = number
; d7 = minimum digits to be formatted out (incl. leading/padding)
move.l numtext_ptr,a0
move.l a0,a1
tst.l d0
beq.s .zero
.loop moveq #$f,d1
and.l d0,d1
lsr.l #4,d0
.zero: move.b hextab(pc,d1.w),-(a0)
subq.l #1,d7
bgt.s .loop
tst.l d0
bne.s .loop
move.l a0,numtext_ptr
rts
hextab:
dc.b '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
even
So you set up numtext_ptr to point at the *end* of your temporary stringbuffer - which should itself be zero-terminated.
Code: Select all
move.l #buffer,numtext_ptr
moveq #4,d7 ; pad to 4 digits
move.l #$AAA,d0 ; a number to print (and will be formatted to $0AAA)
bsr pushhex
move.l numtext_ptr,a0
bsr print_a0
numtext_ptr:
dc.l buffer
buffer_overflow:
ds.b 1024 ; 1k should do for a hex number ;)
buffer: dc.b 0
even
You call the function and when finished, numtext_ptr will have moved, now pointing to the start of your zero-terminated string. You can then print that string normally.
You can do decimal, binary and floats the same way. You could push strings and multiple formatted numbers to the same buffer before printing but if doing that, remember the pushing is in reverse order of the printing.