8bit to ASCII

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Einar Saukas
Manic Miner
Posts: 949
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas » Sat Dec 08, 2018 12:01 am

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

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

Re: 8bit to ASCII

Post by Ast A. Moore » Sat Dec 08, 2018 12:08 am

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

User avatar
Einar Saukas
Manic Miner
Posts: 949
Joined: Wed Nov 15, 2017 2:48 pm

Re: 8bit to ASCII

Post by Einar Saukas » Sat Dec 08, 2018 12:25 am

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 :)
0 x

User avatar
g0blinish
Microbot
Posts: 141
Joined: Sun Jun 17, 2018 1:54 pm

Re: 8bit to ASCII

Post by g0blinish » Sat Dec 08, 2018 10:01 am

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

as backside you got mirrored number xD
0 x

Post Reply