User Tools

Site Tools


base:8_bit_to_hexadecimal_conversion

This is an old revision of the document!


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
	and #$f0	; High nibble
        lsr             ; ignore CARRY!
	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	sbc #$09	; Accu >= 10, subtract #$09 to get "A" to "F"
	jmp BSOUT	; Print Accu (HEX nibble) and bye

Alternatively, add #$36 (CARRY is set, so effectively becomes #$37) to get upper case characters in lower case mode. Also you can “branch always” after adding #$30 to OUT to save a byte at the expense of 3 cycles.

base/8_bit_to_hexadecimal_conversion.1503180345.txt.gz · Last modified: 2017-08-20 00:05 by tww_ctr