===== 8 bit to hexadecimal conversion =====
by ABujok
This is another way to print a 8 bit integer value as a HEX value on a C64 Screen. The given integer must be loaded into the accu before calling the routine.
; Example:
lda #$3F ; load accu immediate with $3f or 63
jsr OUTHEX ; print $3F
rts ; bye
; Syntax for DASM
BSOUT = $ffd2 ; Print character in accu
;**************************
; print Akku hex value
;**************************
OUTHEX tax ; save value for low nibble
and #$f0 ; High nibble
clc ; clear carry
ror ; rotate one bit right
ror ; rotate one bit right
ror ; rotate one bit right
ror ; rotate one bit right
jsr NIB2HEX ; print nibble
txa ; restore value
and #$0f ; Low nibble
jsr NIB2HEX ; print nibble
rts
;*********************
;* Akku low Nibble to Hex
;*********************
NIB2HEX cmp #$0a ; Accu >= 10?
bcs HEX ; Yes
DIGIT clc ; Accu < 10
adc #$30 ; Accu + $30
jmp OUT ; print
HEX clc ;
adc #$37 ; Accu + $37
OUT jmp BSOUT ; Print Accu (HEX nibble) and bye
Slight optimization:
;**************************
; print Akku hex value
;**************************
OUTHEX tax ; save value for low nibble
lsr ; ignore CARRY and shift hi nybble to lonybble pos.
lsr ;
lsr ;
lsr ;
jsr NIB2HEX ; print nibble
txa ; restore value
and #$0f ; Low nibble
jsr NIB2HEX ; print nibble
rts
;*********************
;* Akku low Nibble to Hex
;*********************
NIB2HEX cmp #$0a ; Accu >= 10?
bcs HEX ; Yes
adc #$30 ; Accu < 10
jmp BSOUT ; Print #$30 - #39
HEX adc #$36 ; Accu >= 10, subtract #$09 to get "A" to "F" (CARRY always set here)
jmp BSOUT ; Print Accu (HEX nibble) and bye
Version not using KERNAL:
// Dest. = YREG:XREG
// Value to utput = ACC
OUTHEX:
sty $fb
stx $fc
ldy #$00
pha
lsr
lsr
lsr
lsr
tax
lda tab,x
sta ($fb),y
iny
pla
and #$0f
tax
lda tab,x
sta ($fb),y
rts
tab:
.text "0123456789abcdef"