8bit to ASCII

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
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