From ecc4942ece6a4c6fbd3e7e8317639615a8998acc Mon Sep 17 00:00:00 2001 From: Michaelangel007 Date: Thu, 6 Jul 2017 07:58:22 -0700 Subject: [PATCH] Add zero-page version --- Makefile | 17 ++++++++ README.md | 30 ++++++++++++--- print_uint16 | Bin 98 -> 97 bytes print_uint16.s | 7 +++- print_uint16_zp | Bin 0 -> 85 bytes print_uint16_zp.s | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 Makefile create mode 100644 print_uint16_zp create mode 100644 print_uint16_zp.s diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e3ed461 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +TARGETS=print_uint16 print_uint16_zp +all: $(TARGETS) +.phony: dump + +clean: + rm $(TARGETS) + +print_uint16: print_uint16.s + merlin32 print_uint16.s + +print_uint16_zp: print_uint16_zp.s + merlin32 print_uint16_zp.s + +dump: print_uint16 print_uint16_zp + hexdump8 print_uint16 800 + hexdump8 print_uint16_zp 900 + diff --git a/README.md b/README.md index 1d19a69..5da9236 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # Print unsigned 16-bit -6502 assembly code to print an unsigned 16-bit in 91 bytes ($5B). +6502 assembly code to print an unsigned 16-bit. Features: -* No zero-page usage +* Highly optimized for space +* No zero-page usage [version](print_uint16.s) 90 ($5A) bytes +* With zero-page usage [version](print_uint16_zp.s) 78 ($4E) bytes * Only 2 ROM entry points: COUT, SCRN2 * Includes 7 byte demo @@ -12,11 +14,13 @@ Features: Copy/Paste into your favorite emulator. +Sans-Zero-Page version: + ```asm 0800:A9 12 A2 34 4C 07 08 8E -0808:61 08 48 A2 00 8E 5D 08 +0808:60 08 48 A2 00 8E 5D 08 0810:8E 5E 08 8E 5F 08 A2 10 -0818:F8 0E 61 08 68 2A 48 A0 +0818:F8 0E 60 08 68 2A 48 A0 0820:FD B9 60 07 79 60 07 99 0828:60 07 C8 D0 F4 CA D0 E9 0830:68 D8 A0 03 B9 5C 08 20 @@ -25,9 +29,24 @@ Copy/Paste into your favorite emulator. 0848:68 29 0F D0 04 E0 00 F0 0850:EF C9 0A 90 02 69 06 69 0858:B0 E8 4C ED FD 00 00 00 -0860:00 00 +0860:00 ``` +With-Zero-Page version: + +```asm +0900:A9 12 A2 34 4C 07 09 86 +0908:FC 48 A0 00 84 FD 84 FE +0910:84 FF A0 10 F8 06 FC 68 +0918:2A 48 A2 FD B5 00 75 00 +0920:95 00 E8 D0 F7 88 D0 ED +0928:68 D8 A0 03 B9 FC 00 20 +0930:39 09 88 D0 F7 8A F0 11 +0938:60 48 20 7B F8 20 43 09 +0940:68 29 0F D0 04 E0 00 F0 +0948:EF C9 0A 90 02 69 06 69 +0950:B0 E8 4C ED FD +``` # License @@ -37,6 +56,7 @@ Copy/Paste into your favorite emulator. # Thanks * qkumba for great optimization ideas as always +* Gid for a gental nudge to have a zero-page version * Everyone on comp.sys.apple2 diff --git a/print_uint16 b/print_uint16 index 8bdc1630464ed1e5c183975155d71297a39849da..d27e0dd838e18b248fd9ffb39023dd92ad20712e 100644 GIT binary patch delta 33 ocmYdFlwK*c$i#=8qc4HOV-Z7NEJt4)M_)Y0B7q-#2@|E=0I#SDkpKVy delta 35 qcmYdHl3ppa$i#=8qc4%eV-Z7NEJt4)M_)Y0B7q-#i4&#W7#RS>=?atp diff --git a/print_uint16.s b/print_uint16.s index 9be865b..a79d93c 100644 --- a/print_uint16.s +++ b/print_uint16.s @@ -1,6 +1,9 @@ ; Michael Pohoreski +; https://github.com/Michaelangel007/apple2_print_uint16 ; Optimized from printm ; Thanks to qkumba for optimizations +; Thanks to gid for nudging a zero-page version + ; F8 ROM Entry Points COUT = $FDED SCRN2 = $F879 @@ -58,7 +61,7 @@ BCD2Chars BNE BCD2Chars TXA ; Handle special case input = $0000 of no output - BEQ _HaveLeadingDigit + BEQ _HaveLeadingDigit _PrintDone RTS @@ -87,6 +90,6 @@ PutChar INX ; X = output string length JMP COUT -_bcd ds 4 ; 6 chars for printing dec +_bcd ds 3 ; 6 chars for printing dec _temp db 0 diff --git a/print_uint16_zp b/print_uint16_zp new file mode 100644 index 0000000000000000000000000000000000000000..b3c5e9a1f43b17cd52519e641c242eeae8991fec GIT binary patch literal 85 zcmV-b0IL6~5~4Ir2MLD!NT2|O{e=F6|DX`~2K;C$NTU6<0CfPB0O-*7h|uk5*q{Ts r`~V<135d}5itrI&NFaOoAVUdgDG$&D-~jON$qJAHX$EPq=uGYX^cpE` literal 0 HcmV?d00001 diff --git a/print_uint16_zp.s b/print_uint16_zp.s new file mode 100644 index 0000000..efa16a1 --- /dev/null +++ b/print_uint16_zp.s @@ -0,0 +1,96 @@ +; Michael Pohoreski +; https://github.com/Michaelangel007/apple2_print_uint16 +; Optimized from printm +; Thanks to qkumba for optimizations +; Thanks to gid for nudging a zero-page version + +; F8 ROM Entry Points +COUT = $FDED +SCRN2 = $F879 + +; Zero-Page Version - 4 locations used +_temp = $fc +_bcd = $fd ; NOTE: MUST be at $FD for ZP,X addressing in _DoubleDabble + + ORG $900 ; Intentionally different from sans-zero-page version for testing both + + LDA #$12 + LDX #$34 + JMP PrintUint16 + +; Print unsigned 16-bit integer +; A=High byte +; X=Low byte +; Also see: Applesoft LINPRT @ ED24 +; ====================================================================== +PrintUint16 + STX _temp + PHA ; Optimized: STA _temp+1 + + LDY #0 + STY _bcd+0 + STY _bcd+1 + STY _bcd+2 + +Dec2BCD + LDY #16 ; 16 bits + SED ; "Double Dabble" +_Dec2BCD ; https://en.wikipedia.org/wiki/Double_dabble + ASL _temp+0 ; abcd efgh | ijkl mnop | +; ROL _temp+1 ; C=a bcde fghi | jklm nop0 | +; ; Bit 7654_3210 | 7654_3210 | + PLA + ROL + PHA + + LDX #$FD ; $00-$FD=-3 bcd[0] bcd[1] bcd[2] bcd[3] +_DoubleDabble ; Y=FD Y=FE Y=FF Y=00 + LDA _bcd-$FD,X ; ZP,X + ADC _bcd-$FD,X + STA _bcd-$FD,X + INX + BNE _DoubleDabble + DEY + BNE _Dec2BCD + + PLA ; keep stack + CLD ; X=0 = output length + +DecWidth + LDY #3 ; maximum 6 digits output +BCD2Chars + LDA _bcd-1,Y + JSR HexA ; print 0, 1, or 2 hex digits + DEY + BNE BCD2Chars + + TXA ; Handle special case input = $0000 of no output + BEQ _HaveLeadingDigit + +_PrintDone + RTS + + +; Converts A to high ASCII digits, prints as they become available +; @return: A will be bottom nibble in high ASCII +HexA + PHA + JSR SCRN2+2 ; LSR x4 == 0>> 4 + JSR _HexNib + PLA + AND #$F +_HexNib + BNE _HaveLeadingDigit ; If have leading zero and no output yet ... + CPX #0 ; ... then skip storing it + BEQ _PrintDone + +_HaveLeadingDigit + CMP #$A ; n < 10 ? + BCC _Hex2Asc + ADC #6 ; n += 6 $A -> +6 + (C=1) = $11 +_Hex2Asc + ADC #'0' + $80 ; inverse=remove #$80 +PutChar + INX ; X = output string length + JMP COUT +