Optimized down to 91 bytes

This commit is contained in:
Michaelangel007 2017-07-05 23:08:47 -07:00
parent 1ce8c90bde
commit c9ffff1961
3 changed files with 29 additions and 25 deletions

View File

@ -1,6 +1,6 @@
# Print unsigned 16-bit # Print unsigned 16-bit
6502 assembly code to print an unsigned 16-bit in 95 bytes ($5F). 6502 assembly code to print an unsigned 16-bit in 91 bytes ($5B).
Features: Features:
@ -14,18 +14,18 @@ Copy/Paste into your favorite emulator.
```asm ```asm
0800:A9 12 A2 34 4C 07 08 8E 0800:A9 12 A2 34 4C 07 08 8E
0808:64 08 8D 65 08 A9 00 8D 0808:61 08 48 A2 00 8E 5D 08
0810:60 08 8D 61 08 8D 62 08 0810:8E 5E 08 8E 5F 08 A2 10
0818:A2 10 F8 0E 64 08 2E 65 0818:F8 0E 61 08 68 2A 48 A0
0820:08 A0 FD B9 63 07 79 63 0820:FD B9 60 07 79 60 07 99
0828:07 99 63 07 C8 D0 F4 CA 0828:60 07 C8 D0 F4 CA D0 E9
0830:D0 E9 D8 A0 03 B9 5F 08 0830:68 D8 A0 03 B9 5C 08 20
0838:20 44 08 88 D0 F7 8A E0 0838:41 08 88 D0 F7 8A F0 11
0840:00 F0 11 60 48 20 7B F8 0840:60 48 20 7B F8 20 4B 08
0848:20 4E 08 68 29 0F D0 04 0848:68 29 0F D0 04 E0 00 F0
0850:E0 00 F0 EF C9 0A 90 02 0850:EF C9 0A 90 02 69 06 69
0858:69 06 69 B0 E8 4C ED FD 0858:B0 E8 4C ED FD 00 00 00
0860:00 00 00 00 00 00 0860:00 00
``` ```

Binary file not shown.

View File

@ -17,20 +17,24 @@ SCRN2 = $F879
; Also see: Applesoft LINPRT @ ED24 ; Also see: Applesoft LINPRT @ ED24
; ====================================================================== ; ======================================================================
PrintUint16 PrintUint16
STX _temp+0 STX _temp
STA _temp+1 PHA ; Optimized: STA _temp+1
LDA #0 LDX #0
STA _bcd+0 STX _bcd+0
STA _bcd+1 STX _bcd+1
STA _bcd+2 STX _bcd+2
Dec2BCD Dec2BCD
LDX #16 ; 16 bits LDX #16 ; 16 bits
SED ; "Double Dabble" SED ; "Double Dabble"
_Dec2BCD ; https://en.wikipedia.org/wiki/Double_dabble _Dec2BCD ; https://en.wikipedia.org/wiki/Double_dabble
ASL _temp+0 ASL _temp+0 ; abcd efgh | ijkl mnop |
ROL _temp+1 ; ROL _temp+1 ; C=a bcde fghi | jklm nop0 |
; ; Bit 7654_3210 | 7654_3210 |
PLA
ROL
PHA
LDY #$FD ; $00-$FD=-3 bcd[0] bcd[1] bcd[2] bcd[3] LDY #$FD ; $00-$FD=-3 bcd[0] bcd[1] bcd[2] bcd[3]
_DoubleDabble ; Y=FD Y=FE Y=FF Y=00 _DoubleDabble ; Y=FD Y=FE Y=FF Y=00
@ -39,9 +43,10 @@ _DoubleDabble ; Y=FD Y=FE Y=FF Y=00
STA _bcd-$FD,Y STA _bcd-$FD,Y
INY INY
BNE _DoubleDabble BNE _DoubleDabble
DEX DEX
BNE _Dec2BCD BNE _Dec2BCD
PLA ; keep stack
CLD ; X=0 = output length CLD ; X=0 = output length
DecWidth DecWidth
@ -52,8 +57,7 @@ BCD2Chars
DEY DEY
BNE BCD2Chars BNE BCD2Chars
TXA TXA ; Handle special case input = $0000 of no output
CPX #0 ; Handle special case input = $0000 of no output
BEQ _HaveLeadingDigit BEQ _HaveLeadingDigit
_PrintDone _PrintDone
@ -84,5 +88,5 @@ PutChar
JMP COUT JMP COUT
_bcd ds 4 ; 6 chars for printing dec _bcd ds 4 ; 6 chars for printing dec
_temp db 0,0 _temp db 0