8bit to ASCII

The place for codemasters or beginners to talk about programming any language for the Spectrum.
R-Tape
Posts: 2229
Joined: Thu Nov 09, 2017 11:46 am

8bit to ASCII

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...
0 x

Einar Saukas
Manic Miner
Posts: 744
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

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'
;
``````
1 x

Einar Saukas
Manic Miner
Posts: 744
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

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'
;
``````
0 x

Kweepa
Manic Miner
Posts: 211
Joined: Sat Feb 03, 2018 6:14 pm
Location: Austin, Texas

Re: 8bit to ASCII

Perhaps the algorithm here could inspire you:
http://codebase64.org/doku.php?id=base: ... ii_routine
(it's 6502 - boo!)
0 x

Ast A. Moore
Dynamite Dan
Posts: 1074
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

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
jp m,l2
ld (string),hl
ld (string+2),a
ret
string	defm "---"

``````
Don’t think it’ll be any quicker, though.
1 x
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.

R-Tape
Posts: 2229
Joined: Thu Nov 09, 2017 11:46 am

Re: 8bit to ASCII

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!
0 x

Ast A. Moore
Dynamite Dan
Posts: 1074
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

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.
0 x
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.

R-Tape
Posts: 2229
Joined: Thu Nov 09, 2017 11:46 am

Re: 8bit to ASCII

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
jp m,l2
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.
0 x

Ast A. Moore
Dynamite Dan
Posts: 1074
Joined: Mon Nov 13, 2017 3:16 pm

Re: 8bit to ASCII

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.
0 x
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.

Einar Saukas
Manic Miner
Posts: 744
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

R-Tape wrote:
Fri Dec 07, 2018 8:55 pm
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?
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.
0 x