8bit to ASCII

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

8bit to ASCII

Post by R-Tape »

I know there'll be a lot of common ground with the 16bit unsigned to ASCII thread, but I'd be interested to see how you guys would approach this. A holds a number 0-163, and you want to convert it to a string relatively quickly, not word boundary constrained, and not using a big table.

I came up with this:

Code: Select all

	;
num2ascii:		;arrive A holding number 0-163
	ld hl,numstr
	ld (hl),'0'
	inc hl
	ld (hl),47	;'0'-1
	dec hl		;back to numstring
	cp 100
	jr c,nhun
	ld (hl),'1'	;the hundred digit is now set
	sub 100		;account for that in A
nhun:	inc hl		;onto the tens
	ld b,10
tlp:	inc (hl)
	sub b		;keep subbing 10, and INCing digit until the carry is set
	jr nc,tlp
	inc hl		;now the ones, A is now 0-8
	add a,58	;add the ASCII '0' (48), and the extra 10 we subtracted when A overspun
	ld (hl),a	;done
	ret
	;	
	numstring: db	'000'
	;
I'll wager £5 this can't be improved upon... :?
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas »

Code: Select all

        ;
num2ascii:		;arrive A holding number 0-163
	ld hl,numstr
	ld (hl),'0'
	cp 100
	jr c,nhun
	ld (hl),'1'	;the hundred digit is now set
	sub 100		;account for that in A
nhun:	inc hl		;onto the tens
	ld (hl),47	;'0'-1
	ld b,10
tlp:	inc (hl)
	sub b		;keep subbing 10, and INCing digit until the carry is set
	jr nc,tlp
	inc hl		;now the ones, A is now 0-8
	add a,58	;add the ASCII '0' (48), and the extra 10 we subtracted when A overspun
	ld (hl),a	;done
	ret
	;
	numstring: db	'000'
	;
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas »

Code: Select all

        ;
num2ascii:		;arrive A holding number 0-163
	ld hl,47*256+48           ;both hundred and ten digits
	cp 100
	jr c,nhun
	inc l           ;the hundred digit is now set
	sub 100		;account for that in A
nhun:	ld b,10
tlp:	inc h
	sub b		;keep subbing 10, and INCing digit until the carry is set
	jr nc,tlp
	ld (numstring),hl ; store hundred and tens
	add a,58	;add the ASCII '0' (48), and the extra 10 we subtracted when A overspun
	ld (numstring+2),a	;done
	ret
	;
	numstring: db	'000'
	;
User avatar
Kweepa
Manic Miner
Posts: 311
Joined: Sat Feb 03, 2018 6:14 pm
Location: Albuquerque, New Mexico

Re: 8bit to ASCII

Post by Kweepa »

Perhaps the algorithm here could inspire you:
http://codebase64.org/doku.php?id=base: ... ii_routine
(it's 6502 - boo!)
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

Post by Ast A. Moore »

Kweepa wrote: Fri Dec 07, 2018 5:28 pm Perhaps the algorithm here could inspire you:
http://codebase64.org/doku.php?id=base: ... ii_routine
(it's 6502 - boo!)
Yeah, so in Z80 speak that would be:

Code: Select all

	ld hl,$3a2f
	scf
l1	inc l
	sbc 100
	jr nc,l1
l2	dec h
	adc a,10
	jp m,l2	
	adc a,$2f
	ld (string),hl
	ld (string+2),a
	ret
string	defm "---"

Don’t think it’ll be any quicker, though.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: 8bit to ASCII

Post by R-Tape »

Einar Saukas wrote: Fri Dec 07, 2018 5:27 pm

Code: Select all

        ;
num2ascii:		;arrive A holding number 0-163
	ld hl,47*256+48           ;both hundred and ten digits
	cp 100
	jr c,nhun
	inc l           ;the hundred digit is now set
	sub 100		;account for that in A
nhun:	ld b,10
tlp:	inc h
	sub b		;keep subbing 10, and INCing digit until the carry is set
	jr nc,tlp
	ld (numstring),hl ; store hundred and tens
	add a,58	;add the ASCII '0' (48), and the extra 10 we subtracted when A overspun
	ld (numstring+2),a	;done
	ret
	;
	numstring: db	'000'
	;
Ooh that's very cool. BTW, if I hadn't posted a code example, do you think this is the conclusion you would have reached?
Ast A. Moore wrote: Fri Dec 07, 2018 6:55 pm Don’t think it’ll be any quicker, though.
In this case, I'd probably get away with increasing the string for every dec A. So speed is less important than I originally stated TBH. In this case I got bored of code-wrestling and used a 489 look-up table, then felt ashamed!
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

Post by Ast A. Moore »

R-Tape wrote: Fri Dec 07, 2018 8:55 pm I got bored of code-wrestling and used a 489 look-up table, then felt ashamed!
All’s fair In love and programming. ;)

If there’s any advantage in the example I posted, it’s that it saves you one register, since it only uses three altogether. May come in handy.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: 8bit to ASCII

Post by R-Tape »

Ast A. Moore wrote: Fri Dec 07, 2018 6:55 pm Yeah, so in Z80 speak that would be:

Code: Select all

	ld hl,$3a2f
	scf
l1	inc l
	sbc 100
	jr nc,l1
l2	dec h
	adc a,10
	jp m,l2	
	adc a,$2f
	ld (string),hl
	ld (string+2),a
	ret
string	defm "---"

Don’t think it’ll be any quicker, though.
Maybe not, but that's verrry nice. It works, now I just need to work out how!
All’s fair In love and programming.
Not really. That look-up table made me feel like I'd stolen from an elderly relative.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

Post by Ast A. Moore »

R-Tape wrote: Fri Dec 07, 2018 10:24 pm That look-up table made me feel like I'd stolen from an elderly relative.
Hold the press. You just may be better off stealing from a relative. I was trying to speed up that code—and I succeeded: that last ADC A,$2F can be replaced with ADD A,$30 (the 6502 can only add and subtract with the carry)—when I noticed something odd. The routine fails to properly convert 100 and 200.

Too tired now to investigate any further. Either the original routine is incorrect, or my translation of it is wrong.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas »

R-Tape wrote: Fri Dec 07, 2018 8:55 pmOoh that's very cool. BTW, if I hadn't posted a code example, do you think this is the conclusion you would have reached?
No idea. I wasn't really trying to think of a good solution, I just wanted to make it better than yours to win £5. :)
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas »

Ast A. Moore wrote: Fri Dec 07, 2018 6:55 pm Yeah, so in Z80 speak that would be:

Code: Select all

	ld hl,$3a2f
	scf
l1	inc l
	sbc 100
	jr nc,l1
l2	dec h
	adc a,10
	jp m,l2	
	adc a,$2f
	ld (string),hl
	ld (string+2),a
	ret
string	defm "---"
Nice!!!

Even better:

Code: Select all

        ld hl,256*58+47
p1:     inc l
        sub 100
        jr nc,p1
p2:     dec h
        add a,10
        jr nc,p2
        ld (string),hl
        add a,48
        ld (string+2),a
        ret
string: db '000'
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

Post by Ast A. Moore »

Okay, grandma can hang on to her pearls for another day. I figured it out:

Code: Select all

	ld hl,$3a2f
l1	inc l
	sub 100
	jr nc,l1
l2	dec h
	add a,10
	jp m,l2	
	adc a,$30
	ld (string),hl
	ld (string+2),a
	ret
string	defm "---"
Faster, smaller.

EDIT: Ha! Einar beat me to it. His is even one byte smaller (but mine is marginally faster). :P Great minds . . .
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas »

Ast A. Moore wrote: Sat Dec 08, 2018 12:08 am EDIT: Ha! Einar beat me to it. His is even one byte smaller (but mine is marginally faster).
It depends... If middle digit is 9, then using JP is actually slower!

Ast A. Moore wrote: Sat Dec 08, 2018 12:08 am Great minds . . .
You did most of the work anyway :)
User avatar
g0blinish
Manic Miner
Posts: 281
Joined: Sun Jun 17, 2018 2:54 pm

Re: 8bit to ASCII

Post by g0blinish »

There are some ways to convert:
http://z80-heaven.wikidot.com/math#toc21

as backside you got mirrored number xD
Post Reply