I couldn't readily find some 6502 code to convert a byte to hex string (basically, to show the contents of particular memory locations on an LCD screen, etc.) so have written my own. It does work. What does everyone think?
' Parameters:
' A - value to convert from byte to hex string
' Outputs:
' X - LSB character
' Y - MSB character
.byteTOhexstr
PHA
' grab the MSB and move to LSB
LSR A
LSR A
LSR A
LSR A
' if A is greater than &A then go to A-F
CMP #&A
BCS numstrMSB_AtoF
.numstrMSB_0to9
ADC #48
TAX
JMP numstrLSB
.numstrMSB_AtoF
CLC
ADC #55
TAX
' LSB
.numstrLSB
PLA
' grab the LSB by masking out the MSB
AND #&0F
' if A is greater than &A then go to A-F
CMP #&A
BCS numstrLSB_AtoF
.numstrLSB_0to9
ADC #48
TAX
JMP numstrLSB
.numstrLSB_AtoF
CLC
ADC #55
TAY
RTS
I couldn't readily find some 6502 code to convert a byte to hex string (basically, to show the contents of particular memory locations on an LCD screen, etc.) so have written my own. It does work. What does everyone think?
Have you looked at this code? It can convert a binary value to any of four radices, hex being one of them. The largest binary value that it can convert is $FFFFFFFF or (2^32)-1.
To convert a single byte to printable hexadecimal characters, you could use the following code:
It's a few bytes longer, but a few cycles faster (especially if you avoid the JSR), to look the nybble up in a table of "0123456789ABCDEF". I tend to go that route, since that's often an integral part of IO where I need to maximize speed.
There aren't any branches associated with the CMP instructiions. To convert to ASCII hex, a test for 0...9 should be followed by an ORA #$30 (or ADC #$30). But if the nibble is greater than 9, the adjustment for A...F needs to be applied instead.
Last edited by MichaelM on Sun Jan 25, 2015 9:49 am, edited 1 time in total.
He's using the cmp instructions for their carry side-effects, Michael. Combined with the BCD +6 adjustment, the carry flag provides just the right +7 to jump from '9' to 'A' in ASCII.
Hi, I want to change this code to be able to write on a screen 2 bytes number. It shows only first byte now, I should make a jump to PRHEX before making 4 LSR for sure, but despite trying it doesn't work, so I think there is something I forgot. Can you tell me what is wrong in my thinking?
; put this just before PRBYTE
BMI PRBYTE
;-------------------------------------------------------------------------
; Subroutine to print a word in X (low byte) and A (high byte)
;-------------------------------------------------------------------------
PRWORD JSR PRBYTE ;print high byte
TXA ;continue to print low byte
; continue with PRBYTE
Prior to "jsr print" I do not see any ldx and A is always loaded with $ba! The "bmi phex" is only necessary in the original context. I suppose $ff80 points to your print string utility.
You initialize Y with 0 and then attempt to write the characters of a hex word backwards to ($80)+0, ($80)+$ff, ($80)+$fe, ($80)+$fd.
Please note that you should ask this kind of questions in a separate thread in the newbies section with a link to where you found the original code. And remember to post your modifications from the beginning as what you say might not be what you actually have programmed.