You will not be able to post if you are still using Microsoft email addresses such as Hotmail etc
See here for more information viewtopic.php?f=20&t=7296
DO NOT USE DEVICES WHERE THE IP CHANGES CONSTANTLY!
At this time it is unfortunately not possible to white list users when your IP changes constantly.
You may inadvertently get banned because a previous attack may have used the IP you are now on.
So I suggest people only use fixed IP address devices until I can think of a solution for this problem!

Converting a byte in to Decimal ASCII help

General Discussion, STOS.
Post Reply
User avatar
PhilC
Moderator
Moderator
Posts: 7122
Joined: Fri Mar 23, 2018 8:22 pm

Converting a byte in to Decimal ASCII help

Post 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
If it ain't broke, test it to Destruction.
dml
Posts: 764
Joined: Wed Nov 15, 2017 10:11 pm

Re: Converting a byte in to Decimal ASCII help

Post 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.
dml
Posts: 764
Joined: Wed Nov 15, 2017 10:11 pm

Re: Converting a byte in to Decimal ASCII help

Post 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
User avatar
PhilC
Moderator
Moderator
Posts: 7122
Joined: Fri Mar 23, 2018 8:22 pm

Re: Converting a byte in to Decimal ASCII help

Post by PhilC »

thanks @dml , I'll give that a go later on :dualthumbup:
If it ain't broke, test it to Destruction.
dml
Posts: 764
Joined: Wed Nov 15, 2017 10:11 pm

Re: Converting a byte in to Decimal ASCII help

Post by dml »

PhilC wrote: Thu Oct 30, 2025 3:43 pm 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.
User avatar
PhilC
Moderator
Moderator
Posts: 7122
Joined: Fri Mar 23, 2018 8:22 pm

Re: Converting a byte in to Decimal ASCII help

Post by PhilC »

dml wrote: Thu Oct 30, 2025 3:50 pm
PhilC wrote: Thu Oct 30, 2025 3:43 pm 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.
If it ain't broke, test it to Destruction.
User avatar
sporniket
Site sponsor
Site sponsor
Posts: 1154
Joined: Sat Sep 26, 2020 9:12 pm
Location: France
Contact:

Re: Converting a byte in to Decimal ASCII help

Post 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
(31.85 KiB) Downloaded 6 times
User avatar
PhilC
Moderator
Moderator
Posts: 7122
Joined: Fri Mar 23, 2018 8:22 pm

Re: Converting a byte in to Decimal ASCII help

Post 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:
If it ain't broke, test it to Destruction.
dml
Posts: 764
Joined: Wed Nov 15, 2017 10:11 pm

Re: Converting a byte in to Decimal ASCII help

Post by dml »

PhilC wrote: Thu Oct 30, 2025 9:48 pm @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!)
User avatar
PhilC
Moderator
Moderator
Posts: 7122
Joined: Fri Mar 23, 2018 8:22 pm

Re: Converting a byte in to Decimal ASCII help

Post by PhilC »

dml wrote: Thu Oct 30, 2025 9:50 pm
PhilC wrote: Thu Oct 30, 2025 9:48 pm @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.
If it ain't broke, test it to Destruction.
Post Reply

Return to “SOFTWARE”