apple2_print_uint16/print_uint16.s

91 lines
2.0 KiB
ArmAsm
Raw Normal View History

2017-07-06 02:38:09 +00:00
; Michael Pohoreski
; Optimized from printm
; Thanks to qkumba for optimizations
; F8 ROM Entry Points
2017-07-06 01:50:13 +00:00
COUT = $FDED
SCRN2 = $F879
2017-07-05 23:42:22 +00:00
ORG $800
LDA #$12
LDX #$34
JMP PrintUint16
2017-07-05 23:42:22 +00:00
; Print unsigned 16-bit integer
; A=High byte
; X=Low byte
; Also see: Applesoft LINPRT @ ED24
2017-07-05 23:42:22 +00:00
; ======================================================================
PrintUint16
2017-07-06 01:50:13 +00:00
STX _temp+0
2017-07-05 23:42:22 +00:00
STA _temp+1
LDA #0
STA _bcd+0
STA _bcd+1
STA _bcd+2
Dec2BCD
LDX #16 ; 16 bits
SED ; "Double Dabble"
_Dec2BCD ; https://en.wikipedia.org/wiki/Double_dabble
ASL _temp+0
ROL _temp+1
LDY #$FD ; $00-$FD=-3 bcd[0] bcd[1] bcd[2] bcd[3]
_DoubleDabble ; Y=FD Y=FE Y=FF Y=00
LDA _bcd-$FD,Y
ADC _bcd-$FD,Y
STA _bcd-$FD,Y
INY
BNE _DoubleDabble
DEX
BNE _Dec2BCD
2017-07-06 01:50:13 +00:00
CLD ; X=0 = output length
2017-07-05 23:42:22 +00:00
DecWidth
2017-07-06 03:28:10 +00:00
LDY #3 ; maximum 6 digits output
2017-07-05 23:42:22 +00:00
BCD2Chars
2017-07-06 02:38:09 +00:00
LDA _bcd-1,Y
2017-07-06 03:28:10 +00:00
JSR HexA ; print 0, 1, or 2 hex digits
2017-07-05 23:42:22 +00:00
DEY
2017-07-06 02:38:09 +00:00
BNE BCD2Chars
2017-07-05 23:42:22 +00:00
2017-07-06 03:28:10 +00:00
TXA
CPX #0 ; Handle special case input = $0000 of no output
BEQ _HaveLeadingDigit
2017-07-06 01:50:13 +00:00
2017-07-05 23:42:22 +00:00
_PrintDone
RTS
2017-07-06 02:45:51 +00:00
; Converts A to high ASCII digits, stores chars in _output
; @return: A will be bottom nibble in high ASCII
2017-07-05 23:42:22 +00:00
HexA
PHA
2017-07-06 01:50:13 +00:00
JSR SCRN2+2 ; LSR x4 == 0>> 4
2017-07-05 23:42:22 +00:00
JSR _HexNib
PLA
2017-07-06 01:50:13 +00:00
AND #$F
2017-07-05 23:42:22 +00:00
_HexNib
2017-07-06 02:38:09 +00:00
BNE _HaveLeadingDigit ; If have leading zero and no output yet ...
CPX #0 ; ... then skip storing it
BEQ _HexAsciiDone
_HaveLeadingDigit
2017-07-05 23:42:22 +00:00
CMP #$A ; n < 10 ?
BCC _Hex2Asc
ADC #6 ; n += 6 $A -> +6 + (C=1) = $11
_Hex2Asc
ADC #'0' + $80 ; inverse=remove #$80
PutChar
2017-07-06 03:28:10 +00:00
JSR COUT
2017-07-06 01:50:13 +00:00
INX ; X = output string length
2017-07-06 02:38:09 +00:00
_HexAsciiDone
2017-07-05 23:42:22 +00:00
RTS
_bcd ds 4 ; 6 chars for printing dec
_temp db 0,0