Last month I was writing a little routine to print numbers in hex, and I got stuck. Today I picked it up and realized that I was very close to a solution, but surely someone has already solved this problem. And indeed, Steve Wozniak gave me valuable pointers in his wozmon.txt file... in particular:
PUSH the value onto the stack to save the cycles of having to LDA later.
Use ORA instead of ADC for the initial add, and use a BCC when testing for a digit.
That second point proved to be the key to solving my code problem. At any rate, here's my working code:
;
; Prints an 8 bit value in hex.
;
; Author: Robert Eaglestone with valuable advice from Steve Wozniak
; More Info: wozmon.txt
;
; System: Commander X16
; Version: Emulator R.37
; Compiler: CC65
; Build using: cl65 -t cx16 hex.s -C cx16-asm.cfg -o HEX.PRG
;
chrout = $ffd2 ; Commodore Kernal RULES!
.org $8000
.export LOADADDR = *
; $8000
value: .byte 0 ; 0-255
; $8001
Main: lda value
pha ; save A for LSD
print_hi: ;
; print the high nybble
;
lsr ; shift right four bits
lsr
lsr
lsr
jsr hex
print_lo: ;
; print the low nybble
;
pla ; fetch for LSD
hex:
and #$0f
ora #$30 ; i.e. "0"
cmp #$3a ; digit?
bcc echo ; yes
adc #6 ; add offset for A
echo:
jsr chrout ; print it
rts