Binary->decimal

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Wall_Axe
Manic Miner
Posts: 500
Joined: Mon Nov 13, 2017 11:13 pm

Binary->decimal

Post by Wall_Axe »

to display a number onscreen

for example an 8bit number:

check if its above 200 if so, subtract 200 and print a 2
check if remainer is above 100 if so subtract 100 and print 1

check if remainder is above 10, if so...
divide remainder by ten print that number in the tens spot, then take away that number*10 from remainder (so now its lower than 10)

print remainder in the ones spot

Is there a better way?
AndyC
Dynamite Dan
Posts: 1406
Joined: Mon Nov 13, 2017 5:12 am

Re: Binary->decimal

Post by AndyC »

That's pretty much how you'd do it in BASIC. In assembler though you can just rotate it through the carry flag and print a zero or one as appropriate.
Wall_Axe
Manic Miner
Posts: 500
Joined: Mon Nov 13, 2017 11:13 pm

Re: Binary->decimal

Post by Wall_Axe »

how do you mean? it sounds like you are talking about printing a binary to the screen?
AndyC
Dynamite Dan
Posts: 1406
Joined: Mon Nov 13, 2017 5:12 am

Re: Binary->decimal

Post by AndyC »

Ignore me, I misread what you were doing.
Bizzley
Microbot
Posts: 124
Joined: Thu Nov 16, 2017 10:47 am

Re: Binary->decimal

Post by Bizzley »

Assuming you mean how do you do this in assembler and speed isn't a consideration I'd go with something like this:

Code: Select all

		ld a,234	;example byte to be converted
		ld hl,pmess	;example string to send result to
		call convert
		ret
pmess		ds 3
;
;
convert		ld c,100
		call convert1
		ld c,10
		call convert1
		jr convert3
convert1	ld d,-1
convert2	inc d
		sub c
		jr nc,convert2
		add c
		ld e,a
		ld a,d
convert3	add 48
		ld (hl),a
		inc hl
		ld a,e
		ret
You can then pass the string to your print routine to print off however you want as well as remove any leading zeros if needs be. Alternatively replace the code from convert3 onwards with an in-place Print routine using HL as a pointer to the location on screen where you want the numbers to be printed to instead.

As a bonus you can also convert to base n numbers by replacing ld c,10 with ld c,n and ld c,100 with load c,n^2 e.g. 8 and 64 gets you the octal value.

There may be a ROM call that does the same thing, or is closer to what you want, if so I'm sure someone will point it out for you to try.
"He made eloquent speeches to an audience consisting of a few depressed daffodil roots, and sometimes the cat from next door."
User avatar
arkannoyed
Manic Miner
Posts: 436
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Binary->decimal

Post by arkannoyed »

Does the binary result need to be displayed in just the standard Speccy Char set?

If so, this does a fairly decent job;

Code: Select all

ld c,00h		any number 00-ff
          ld b,08h
loop0:
          rlc c
          sbc a,a
          and 01h
          add a,30h
          rst 10h
          djnz loop0
          ret
You'll need to alter the print coordinates first probably, or else it'll print to where you probably don't want it!

Or have I misunderstood entirely and you want to display a HEX number as Decimal on screen?
User avatar
arkannoyed
Manic Miner
Posts: 436
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Binary->decimal

Post by arkannoyed »

Ahhh, I see, Yes, I read it wrong!

Anyway, shorter version, completely missing the point though! :P

Code: Select all

ld c,00h
          ld b,08h
loop0:
          ld a,18h
          rlc c
          adc a,a
          rst 10h
          djnz loop0
          ret
13 bytes
Wall_Axe
Manic Miner
Posts: 500
Joined: Mon Nov 13, 2017 11:13 pm

Re: Binary->decimal

Post by Wall_Axe »

good one bizzley, that must be fast enough to show the score when it changes
Post Reply