Can't Convert Int To Ascii

Ask anything your want about Megadrive/Genesis programming.

Moderator: BigEvilCorporation

Post Reply
Scorpion Illuminati
Interested
Posts: 28
Joined: Fri Oct 02, 2015 4:58 pm

Can't Convert Int To Ascii

Post by Scorpion Illuminati » Wed Oct 14, 2015 11:12 pm

Hi,

I'm trying to convert an Integer to ascii so that i may display it. However for some reason it doesn't seem to work, yet the Hex to ascii version does which leads me to believe it's a code issue. Here is the integer version code:

Code: Select all

ItoA_Int_w:
   ; Converts a word to integer ASCII
   ; a0 --- In/Out: String address
   ; d0 (w) In: Number to convert

   ; 4 nybbles + 0x + terminator, working backwards
   add.l  #0x7, a0

   ; Zero terminate
   move.b #0x0, -(a0)

   move.w #0x0, d1   ; Char ptr
   move.w #0x3, d2   ; 4 nybbles in a word
@NybbleLp:
   move.b d0, d3         ; Byte to d3
   andi.b #0x0F, d3      ; Bottom nybble
@Numeric:
   add.b  #ASCIINumericOffset, d3   ; In numeric range (0 - 9)
   move.b d3, -(a0)      ; Back to string
   lsr.w  #0x4, d0         ; Next nybble
   dbra   d2, @NybbleLp   ; Loop

   rts
and here is the hex version:

Code: Select all

ItoA_Hex_w:
   ; Converts a word to hex ASCII
   ; a0 --- In/Out: String address
   ; d0 (w) In: Number to convert

   ; 4 nybbles + 0x + terminator, working backwards
   add.l  #0x7, a0

   ; Zero terminate
   move.b #0x0, -(a0)

   move.w #0x0, d1   ; Char ptr
   move.w #0x3, d2   ; 4 nybbles in a word
@NybbleLp:
   move.b d0, d3         ; Byte to d3
   andi.b #0x0F, d3      ; Bottom nybble
   cmp.b  #0x9, d3
   ble    @Numeric         ; Branch if in numeric range
   add.b  #(ASCIIAlphaOffset-0xA), d3   ; In alpha range (A - F)
   move.b d3, -(a0)      ; Back to string
   lsr.w  #0x4, d0         ; Next nybble
   dbra   d2, @NybbleLp   ; Loop
   bra    @End
@Numeric:
   add.b  #ASCIINumericOffset, d3   ; In numeric range (0 - 9)
   move.b d3, -(a0)      ; Back to string
   lsr.w  #0x4, d0         ; Next nybble
   dbra   d2, @NybbleLp   ; Loop

   @End:

   ;0X
   move.b #'X', -(a0)
   move.b #'0', -(a0)

   rts
Any assistance would be greatly appreciated.

Sincerely,

Scorpion Illuminati
Scorpion Illuminati - An open source rhythm game for the Sega Genesis
http://www.scorpionilluminati.tk

walker7
Interested
Posts: 45
Joined: Tue Jul 24, 2012 6:27 am

Re: Can't Convert Int To Ascii

Post by walker7 » Thu Oct 22, 2015 6:44 pm

I have created two routines that can do this kind of conversion.

The first one converts a 32-bit number a converts it to a 10-digit string.

Code: Select all

Num_to_UBCD:
;==============================================================================
; INPUT:	d0	= Number
; OUTPUT:	a0	= POINTS TO: Output
; NOTES:	This takes a 32-bit number and converts it to a 10-digit
; number in unpacked BCD. Each digit is one byte, and each byte ranges from
; $00-$09.
;==============================================================================
	movem.l	d1-d2/a0-a1,-(a7)		; -- Save registers.
	lea	Log10_TABLE(pc),a1		; Point to power-of-10 table.
	moveq	#9,d3				; Initialize Loop Counter.
.2:	move.l	(a1)+,d2
	moveq	#-1,d1
.1:	addq.b	#1,d1
	sub.l	d2,d0
	bcc.b	.1
	move.b	d1,(a0)+
	add.l	d2,d0
	dbra	d3,.2
	movem.l	(a7)+,d1-d2/a0-a1		; ++ Restore registers.
	rts

Log10_TABLE:
;==============================================================================
; This is a power-of-10 table.
;==============================================================================
	dc.l	1000000000, 100000000, 10000000, 1000000, 100000
	dc.l	10000, 1000, 100, 10, 1
What this does is convert a 10-digit number from binary to unpacked binary coded decimal (UBCD). Therefore, all the bytes range from $00-$09. In this routine, put the number you want to convert into d0 and the RAM address where the 10-digit string will go in a0.


Now, if you want to do a conversion to ASCII, use this code:

Code: Select all

Num_to_ASCII:
;==============================================================================
; INPUT:	d0	= Number
; OUTPUT:	a0	= POINTS TO: Output
; NOTES:	This takes a 32-bit number and converts it to a 10-digit
; number in ASCII. Each digit is one byte, and each byte ranges from $30-$39.
;==============================================================================
	move.l	d3,-(a7)		; -- Save register.
	bsr.b	Num_to_UBCD		; Convert number to unpacked BCD.
	moveq	#9,d3			; Initialize Loop Counter.
.1:	addi.b	#'0',0(a0,d3.w)		; Adjust digit for ASCII.
	dbra	d3,.1			; Loop for next Digit.
	move.l	(a7)+,d3		; ++ Restore register.
	rts
Notice that this uses the previous routine. The input is the same, but the only difference is that the bytes are in the range of $30-$39, making it an ASCII string.

And there you go!
When programming, you can do it if you put your mind to it.

Post Reply