[Return to top]

6502 Shorts - Hex to ASCII. By Lee Davison
[Back]


Introduction.
I know you've all done this before but here's a use for decimal mode. Read on....
The code.
The classic way to convert a nibble in A into ASCII is to test if the nibble is greater than nine and, if it is, to add seven before adding the 48 to make the ASCII character.


	CMP	#$0A		; set carry for +1 if >9	
	BCC	NoAdjust	; branch if <=9

	ADC	#6		; adjust if A to F
				; (six plus carry = 7!)
NoAdjust
	ADC	#"0"		; add ASCII "0"

This is eight bytes long and takes seven or eight cycles depending on the nibble.

Well here's a way that uses decimal mode addition to do that for you.


	SED			; set decimal mode
	CMP	#$0A		; set carry for +1 if >9	
	ADC	#"0"		; add ASCII "0"
	CLD			; clear decimal mode

Six bytes and always nine cycles. Or, if you do a number of nibbles and leave decimal mode set throughout, four bytes and five cycles per digit.


e-mail me [e-mail]
Last page update: 13th November, 2002.