Page 1 of 1

Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 14:57
by PhilC
Hi All,

I'm doing my first ever 68060 bit of coding on the Raven to create an app that works as a sound input mixer for the audio card.

So I'm having to brush off my very old 68000 coding skills. Last time I really did some was back in the 90s.

I need to convert a number stored in a byte of data into a bit of text that I can then use with the VDI command v_gtext to output it on to the screen. Values could be 0 - 255.

Could someone please share a 68060 friendly bit of code for doing the converting. I'm okay with setting the coordinates in the VDI command V_gtext, Ie using:

Code: Select all

move.w	#xpos,d0
move.w	#ypos,d1

*insert ascii conversion stuff here

v_gtext	d0,d1,inserted_text_data

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 15:26
by dml
Something simple provided below. It's not supposed to be optimal - just simple to describe.

The key thing to understand is that digits are generated in reverse when formatting numbers. So the least significant digit first. That means the string is filled *backwards*.

Code: Select all

*-------------------------------------------------------*
pushhex:
*-------------------------------------------------------*
;	d0 	= 	number
;	d7 	= 	minimum digits to be formatted out (incl. leading/padding)
	move.l		numtext_ptr,a0
	move.l		a0,a1
	tst.l		d0
	beq.s		.zero
.loop	moveq		#$f,d1
	and.l		d0,d1
	lsr.l		#4,d0
.zero:	move.b		hextab(pc,d1.w),-(a0)
	subq.l		#1,d7
	bgt.s		.loop
	tst.l		d0
	bne.s		.loop
	move.l		a0,numtext_ptr
	rts

hextab:
	dc.b		'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
	even

So you set up numtext_ptr to point at the *end* of your temporary stringbuffer - which should itself be zero-terminated.

Code: Select all

 move.l #buffer,numtext_ptr 
 moveq #4,d7 ; pad to 4 digits
 move.l #$AAA,d0 ; a number to print (and will be formatted to $0AAA)
 bsr pushhex

 move.l numtext_ptr,a0
 bsr print_a0


numtext_ptr:
		dc.l buffer 

buffer_overflow:
		ds.b 1024 ; 1k should do for a hex number ;)
buffer:		dc.b 0
		even
You call the function and when finished, numtext_ptr will have moved, now pointing to the start of your zero-terminated string. You can then print that string normally.

You can do decimal, binary and floats the same way. You could push strings and multiple formatted numbers to the same buffer before printing but if doing that, remember the pushing is in reverse order of the printing.

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 15:30
by dml
Oops, you wanted decimal (somehow I grok'd it as hexadecimal).

So here's a decimal version:

Code: Select all

*-------------------------------------------------------*
pushdec:
*-------------------------------------------------------*
;	d0 	= 	number
;	d7 	= 	minimum digits
	move.l		numtext_ptr,a0
	move.l		a0,a1
	moveq		#'0',d3
	move.l		d3,d1
	moveq		#10,d4
	tst.l		d0
	beq.s		.zero
.loop	swap		d0
	clr.w		d0
	swap		d0
	divu		d4,d0
	move.l		d0,d1
	swap		d1
	add.w		d3,d1
.zero:	move.b		d1,-(a0)
	subq.l		#1,d7
	bgt.s		.loop
	tst.w		d0
	bne.s		.loop
	move.l		a0,numtext_ptr
	rts

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 15:43
by PhilC
thanks @dml , I'll give that a go later on :dualthumbup:

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 15:50
by dml
PhilC wrote: 30 Oct 2025 15:43 thanks @dml , I'll give that a go later on :dualthumbup:
Oh yeah - the decimal version above only accepts 16bit words in d0.w. Didn't make that clear. It's easy to do a long version - but seems like this will do for what you asked.

It's all cheesy code which is easily optimised but hopefully will get you moving.

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 16:19
by PhilC
dml wrote: 30 Oct 2025 15:50
PhilC wrote: 30 Oct 2025 15:43 thanks @dml , I'll give that a go later on :dualthumbup:
Oh yeah - the decimal version above only accepts 16bit words in d0.w. Didn't make that clear. It's easy to do a long version - but seems like this will do for what you asked.

It's all cheesy code which is easily optimised but hopefully will get you moving.
Yeah that's absolutely fine as I'm reading the values in as bytes but converting to words for what else I need. Thank you.

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 20:05
by sporniket
Once you have flexed your muscles, you might also try to implement the algorithm described in this application note. It's on my own todo list for the 68000 and 6809.

I like the fact that the described algorithm does not use division, nor use comparisons with each digit (I initially devised an implementation like the later, very cumbersome, especially because I tried to be clever and not compare sequencially from 9xxx to 0xxx, but used a kind of dichotomy/tree walking)

BCD2BIN.PDF

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 21:48
by PhilC
@dml using your hex example as I decided for my purposes hexidecimal is just as suitable.

But I get an error of data too large for the following:

Code: Select all

.zero:	move.b		hextab(pc,d1.w),-(a0)
Unfortunately i've forgotten more than I knew about 68000 back in the 80s and 90s :roll:

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 21:50
by dml
PhilC wrote: 30 Oct 2025 21:48 @dml using your hex example as I decided for my purposes hexidecimal is just as suitable.

But I get an error of data too large for the following:

Code: Select all

.zero:	move.b		hextab(pc,d1.w),-(a0)
Unfortunately i've forgotten more than I knew about 68000 back in the 80s and 90s :roll:
It's just because the hextab is more than 128bytes away from the instruction using it.

Either move the table to just immediately below the function, or i'll fix it to not do that :) (tomorrow!)

Re: Converting a byte in to Decimal ASCII help

Posted: 30 Oct 2025 21:53
by PhilC
dml wrote: 30 Oct 2025 21:50
PhilC wrote: 30 Oct 2025 21:48 @dml using your hex example as I decided for my purposes hexidecimal is just as suitable.

But I get an error of data too large for the following:

Code: Select all

.zero:	move.b		hextab(pc,d1.w),-(a0)
Unfortunately i've forgotten more than I knew about 68000 back in the 80s and 90s :roll:
It's just because the hextab is more than 128bytes away from the instruction using it.

Either move the table to just immediately below the function, or i'll fix it to not do that :) (tomorrow!)
Ah cool, thank you, i've moved it right after the subroutine.