Add zero-page version

This commit is contained in:
Michaelangel007 2017-07-06 07:58:22 -07:00
parent c9ffff1961
commit ecc4942ece
6 changed files with 143 additions and 7 deletions

17
Makefile Normal file
View File

@ -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

View File

@ -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

Binary file not shown.

View File

@ -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

BIN
print_uint16_zp Normal file

Binary file not shown.

96
print_uint16_zp.s Normal file
View File

@ -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