NickH93 wrote:
Thank you so much for that code. It works perfectly.
I am so close to having a monitor that should display a byte of data at any given address.
The last piece of the puzzle is to just to be able to do the opposite. The value I want to display is in hex, and I want it in ASCII.
Is there a similar piece of code to do this?
Thanks!
You should search for the book "6502 Assembly Language Subroutines" written by Levanthal and Saville. Excellent in all and covers a lot of the routines you would require to write a monitor. It's pretty much a staple for 6502 programming and shows source code and usage. You'll find most monitors use a handful of useful routines for code conversion as you need to do this at the command line to get input data and display output data.
I've re-written many such routines either leveraging 65C02 opcodes and/or addressing modes and in some cases just to make them shorter and/or faster. Here's a piece of code that will do what you want from a byte value in the A reg:
Code:
;BIN2ASC subroutine: Convert byte in A register to two ASCII HEX digits
;Return: A register = high digit, Y register = low digit
BIN2ASC PHA ;Save A Reg on stack
AND #$0F ;Mask off high nibble
JSR ASCII ;Convert nibble to ASCII HEX digit
TAY ;Move to Y Reg
PLA ;Get character back from stack
LSR A ;Shift high nibble to lower 4 bits
LSR A
LSR A
LSR A
;
ASCII CMP #$0A ;Check for 10 or less
BCC ASOK ;Branch if less than 10
CLC ;Clear carry for addition
ADC #$07 ;Add $07 for A-F
ASOK ADC #$30 ;Add $30 for ASCII
RTS ;Return to caller
So, to use this, simply load the A reg with the byte value and then you can print the ASCII characters directly using something like this, which uses the above routine:
Code:
;PRBYTE subroutine:
; Converts a single Byte to 2 HEX ASCII characters and sends to console
; on entry, A reg contains the Byte to convert/send
; Register contents are preserved on entry/exit
PRBYTE PHA ;Save A register
PHY ;Save Y register
PRBYT2 JSR BIN2ASC ;Convert A reg to 2 ASCII Hex characters
JSR CHROUT ;Print high nibble from A reg
TYA ;Transfer low nibble to A reg
JSR CHROUT ;Print low nibble from A reg
PLY ;Restore Y Register
PLA ;Restore A Register
RTS ;And return to caller
In the above routine, CHROUT simply prints the character in the A reg to a terminal via an async port.
If you need to print a word (2 bytes) use the above code and this routine:
Code:
;PRWORD subroutine:
; Converts a 16-bit word to 4 HEX ASCII characters and sends to console
; on entry, A reg contains High Byte, Y reg contains Low Byte
; Register contents are preserved on entry/exit
PRWORD PHA ;Save A register
PHY ;Save Y register
JSR PRBYTE ;Convert and print one HEX character (00-FF)
TYA ;Get Low byte value
BRA PRBYT2 ;Finish up Low Byte and exit