Page 1 of 1
8bit to ASCII
Posted: Fri Dec 07, 2018 4:36 pm
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...
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 5:01 pm
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'
;
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 5:27 pm
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'
;
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 5:28 pm
by Kweepa
Perhaps the algorithm here could inspire you:
http://codebase64.org/doku.php?id=base: ... ii_routine
(it's 6502 - boo!)
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 6:55 pm
by Ast A. Moore
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.
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 8:55 pm
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!
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 9:49 pm
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.
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 10:24 pm
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.
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 11:47 pm
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.
Re: 8bit to ASCII
Posted: Fri Dec 07, 2018 11:56 pm
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.
Re: 8bit to ASCII
Posted: Sat Dec 08, 2018 12:01 am
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'
Re: 8bit to ASCII
Posted: Sat Dec 08, 2018 12:08 am
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).
Great minds . . .
Re: 8bit to ASCII
Posted: Sat Dec 08, 2018 12:25 am
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!
You did most of the work anyway
Re: 8bit to ASCII
Posted: Sat Dec 08, 2018 10:01 am
by g0blinish
There are some ways to convert:
http://z80-heaven.wikidot.com/math#toc21
as backside you got mirrored number xD