User Tools

Site Tools


No renderer 'pdf' found for mode 'pdf'
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
        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
base/8_bit_to_hexadecimal_conversion.1503181059.txt.gz · Last modified: 2017-08-20 00:17 by tww_ctr