On Friday, July 7, 2017 at 9:02:45 PM UTC-7, qkumba wrote:
> > :PrDigit jsr PRHEXZ ;FDE5
>
> Not for the right-justified version, though.
I cleaned up the source, added comments and moved the configuration-specific code into macros as Bredon intended (shout out to Big Mac).
The macro refactor saved a few bytes in the smallest config, and qkumba's mod saved 2 bytes in the two smallest configs:
len=$2F Print leading zeroes
0800:A2 12 A9 34 48 8A A0 FF A2 04 C8 85 3F 68 85 3E
0810:38 FD 2A 08 48 A5 3F FD 2E 08 B0 EE 68 98 20 E5
0820:FD A0 00 A5 3E CA F0 F6 10 E6 60 0A 64 E8 10 00
0830:00 03 27
len=$39 Left justify (skip leading zeroes)
0800:A2 12 A9 34 48 8A A0 FF 84 3D A2 04 C8 85 3F 68
0810:85 3E 38 FD 34 08 48 A5 3F FD 38 08 B0 EE 68 98
0820:D0 04 E6 3D 10 05 20 E5 FD A0 00 C6 3D A5 3E CA
0830:F0 F4 10 DE 60 0A 64 E8 10 00 00 03 27
len=$3D Right justify (spaces instead of leading zeroes)
0800:A2 12 A9 34 48 8A A0 FF 84 3D A2 04 C8 85 3F 68
0810:85 3E 38 FD 38 08 48 A5 3F FD 3C 08 B0 EE 68 98
0820:D0 06 E6 3D 30 02 A9 10 49 B0 20 ED FD A0 00 C6
0830:3D A5 3E CA F0 F2 10 DA 60 0A 64 E8 10 00 00 03 27
Revised, commented source below.
-JB
@JBrooksBSI
1 *-------------------------------
2 * DecPrint - 6502 print 16 bits
3 *
4 * 11/8/2015 by John Brooks
5 *-------------------------------
6 org $800
7
8 Demo ldx #$12 ;X=H
9 lda #$34 ;A=L
10 * Fall into DecPrintU16
11
12 *-------------------------------
13
14 DEC_SKIP0 = 0 ;Set to 1 to skip leading zeroes (left justify)
15 DEC_SPACE0 = 0 ;Set to 1 to print leading spaces (right justify)
16 DEC_DIGITS = 5 ;# of digits to print (1-5)
17 DEC_VARS = $3D
18
19 dum DEC_VARS ;Uses 3 temp bytes (ZP or ABS)
20 DecCtr ds 1 ;Leading zero ctr
21 DecWord ds 2 ;U16 being printed
22 dend
23
24 RomPrHexZ = $FDE5
25 RomCOut = $FDED
26
27 DEC_INIT mac
28 do DEC_SKIP0+DEC_SPACE0
29 sty DecCtr ;-1 means no non-zeroes seen yet
30 fin
31 <<<
32
33 DEC_ZERO mac
34 do DEC_SKIP0
35 bne :PrDigit ;Print all non-zero digits
36 inc DecCtr ;Mark that a zero was found
37 bpl :NoDigit ;Skip leading zeroes
38 else
39
40 do DEC_SPACE0
41 bne :PrDigit ;Print all non-zero digits
42 inc DecCtr ;Mark that a zero was found
43 bmi :PrDigit ;Print digit if we've seen a non-zero
44 lda #$10 ;Else print a space. $10="0" EOR " "
45 fin
46 fin
47 <<<
48
49 DEC_PRINT mac
50 do DEC_SPACE0
51 eor #"0" ;Print 0-9 or space
52 jsr RomCOut
53 else
54 jsr RomPrHexZ ;qkumba opt
55 fin
56 <<<
57
58 DEC_CTR mac
59 do DEC_SKIP0+DEC_SPACE0
60 dec DecCtr ;Mark that a digit was printed
61 fin
62 <<<
63
64 *-------------------------------
65 * Print U16 as decimal via COUT
66 * IN: X=hi, A=Lo
67 * OUT: X=$FF, Y=$00
68 *-------------------------------
69 DecPrintU16
70 pha ;ArgL
71 txa ;ArgH
72 ldy #0-1 ;Start digit is 0 (-1 for extra iny)
73 DEC_INIT ;Init leading zero ctr if needed
74 DecModLen = *+1
75 :MOD ldx #DEC_DIGITS-1 ;# of digits to print
76
77 :Loop iny ;Inc current digit 0-9
78 sta DecWord+1 ;Store remainderH
79 pla
80 sta DecWord ;Store remainderL
81
82 :DoDigit sec ;Try next larger digit
83 sbc :Pow10L-1,x ;Subtract power-of-10 lo
84 pha ;Save in case digit is good
85 lda DecWord+1
86 sbc :Pow10H-1,x ;Subtract power-of-10 hi
87 bcs :Loop ;If sub didn't borrow, try higher digit
88
89 :GotDigit
90 pla ;Subtract failed. Discard remainder low
91 tya ;A=digit 0-9
92 DEC_ZERO ;Handle leading zero case if needed
93 :PrDigit DEC_PRINT ;Print digit 0-9 (or space if right-justify)
94
95 ldy #0 ;Next smaller digit starts at 0
96 :NoDigit
97 DEC_CTR ;Update leading-zero ctr if needed
98 lda DecWord ;Get remainderL (prepare for subtract)
99 dex ;Now calc value of smaller digit
100 beq :PrDigit ;Last digit prints as-is (no leading-zero logic)
101 bpl :DoDigit ;Calc digits 2-DEC_DIGITS
102 rts
103
104 :Pow10L db <10,<100,<1000,<10000
105 :Pow10H db >10,>100,>1000,>10000
106
107 lst off