You can try this routine from my C02 Monitor. Initially based on code adapted from "6502 Assembly Language Subroutines" with changes from me and later Mike Barry.
Code:
;HEX2ASC - Accepts 16-bit Hexadecimal value and converts to an ASCII decimal string. Input is
; via the A and Y Registers and output is up to 5 ASCII digits in DATABUFF. The High Byte is in
; the Y Register and Low Byte is in the A Register. Output data is placed in variable DATABUFF
; and terminated with a null character.
; Note: leading zeros are suppressed. PROMPTR routine is used to print the ASCII decimal value.
; Core routine based on Michael Barry's code. Saves many bytes with two updates/changes ;-)
HEX2ASC STA BINVALL ;Save Low byte
STY BINVALH ;Save High byte
LDX #5 ;Get ASCII buffer offset
STZ DATABUFF,X ;Zero last buffer byte for null end
;
CNVERT LDA #$00 ;Clear remainder
LDY #16 ;Set loop count for 16-bits
;
DVLOOP CMP #$05 ;Partial remainder >= 10/2
BCC DVLOOP2 ;Branch if less
SBC #$05 ;Update partial (carry set)
;
DVLOOP2 ROL BINVALL ;Shift carry into dividend
ROL BINVALH ;Which will be quotient
ROL A ;Rotate A Reg
DEY ;Decrement count
BNE DVLOOP ;Branch back until done
ORA #$30 ;OR in $30 for ASCII
;
DEX ;Decrement buffer offset
STA DATABUFF,X ;Store digit into buffer
;
LDA BINVALL ;Get the Low byte
ORA BINVALH ;OR in the High byte (check for zero)
BNE CNVERT ;Branch back until done
;
;Conversion is complete, get the string address, add offset, then call prompt routine and return
; note DATABUFF is fixed location in Page 0, carry flag need not be cleared as result can never
; set flag after ADC instruction.
TXA ;Get buffer offset
ADC #<DATABUFF ;Add Low byte address
LDY #>DATABUFF ;Get High byte address
BRA PROMPTR ;Branch to PROMPTR to Print numeric string
;
;PROMPT routine: Send indexed text string to terminal. Index is contained in A Reg.
; String buffer address is stored in variable PROMPTL/PROMPTH.
PROMPT ASL A ;Multiply by two for msg table index
TAX ;Xfer to X Reg - index
LDA MSG_TABLE,X ;Get low byte address
LDY MSG_TABLE+1,X ;Get high byte address
;
;PROMPTR routine: takes message address in Y/A and prints via PROMPT2 routine
PROMPTR STA PROMPTL ;Store low byte
STY PROMPTH ;Store high byte
;
;PROMPT2 routine: prints message at address (PROMPTL) till null character found
PROMPT2 LDA (PROMPTL) ;Get string data (5)
BEQ HINEXIT ;If null character, exit (borrowed RTS) (2/3)
JSR B_CHROUT ;Send character to terminal (6)
INC PROMPTL ;Increment low byte index (5)
BNE PROMPT2 ;Loop back for next character (2/3)
INC PROMPTH ;Increment high byte index (5)
BRA PROMPT2 ;Loop back and continue printing (3)
;
Note that the B_CHROUT routine outputs a character to a serial port on my Pocket SBC. You can replace it to send the data to wherever you need it.