I was needing to update some decimal printout code. I was initially using a routine that used a tables of powers of ten, but was needing to be able to print up to 32-bit values and didn't like the 40 bytes needed for the tens table. After some searching I found this thread, and the link to John Brooks' "clever V flag" routine.
It was almost exactly what I needed, except I needed two additions:
* I needed to be able to specify where the supplied number was, ideally in 0,X format
* I needed to be able to pad with leading spaces.
The following is my adaptation:
Code:
* Print up to 32-bit unsigned decimal number
********************************************
* See forum.6502.org/viewtopic.php?f=2&t=4894
* and groups.google.com/g/comp.sys.apple2/c/_y27d_TxDHA
*
* On entry:
* X=>base of four-byte zero page locations
* Y= number of digits to pad to, 0 for no padding
* Can print fewer than 32 bits by setting higher bytes
* to zero and setting Y appropriately
*
* On exit:
* The four bytes at 0,X to 3,X are set to zero
* X=preserved
* A,Y corrupted
*
* Needs OSPAD = to hold pad count
* OSTEMP = bit counter
* OSWRCH = routine to display a character
*
PRINTDEC sty OSPAD ; Number of padding+digits
ldy #0 ; Digit counter
PRDECDIGIT lda #32 ; 32-bit divide
sta OSTEMP
lda #0 ; Remainder=0
clv ; V=0 means div result = 0
PRDECDIV10 cmp #10/2 ; Calculate OSNUM/10
bcc PRDEC10
sbc #10/2+$80 ; Remove digit & set V=1 to show div result > 0
sec ; Shift 1 into div result
PRDEC10 rol 0,x ; Shift /10 result into OSNUM
rol 1,x
rol 2,x
rol 3,x
rol a ; Shift bits of input into acc (input mod 10)
dec OSTEMP
bne PRDECDIV10 ; Continue 32-bit divide
ora #48
pha ; Push low digit 0-9 to print
iny
bvs PRDECDIGIT ; If V=1, result of /10 was > 0 & do next digit
lda #32
PRDECLP1 cpy OSPAD
bcs PRDECLP2 ; Enough padding pushed
pha ; Push leading space characters
iny
bne PRDECLP1
PRDECLP2 pla ; Pop character left to right
jsr OSWRCH ; Print it
dey
bne PRDECLP2
rts
* Example code:
* LDX #OSNUM ; X=>four bytes in zero page
* LDY #8 ; Y=pad to 8 digits
* JSR PRINTDEC