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?
Binary->decimal
Re: Binary->decimal
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.
Re: Binary->decimal
how do you mean? it sounds like you are talking about printing a binary to the screen?
Re: Binary->decimal
Ignore me, I misread what you were doing.
Re: Binary->decimal
Assuming you mean how do you do this in assembler and speed isn't a consideration I'd go with something like this:
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.
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
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."
- arkannoyed
- Manic Miner
- Posts: 436
- Joined: Mon Feb 05, 2018 9:56 am
- Location: Northamptonshire
Re: Binary->decimal
Does the binary result need to be displayed in just the standard Speccy Char set?
If so, this does a fairly decent job;
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?
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
Or have I misunderstood entirely and you want to display a HEX number as Decimal on screen?
- arkannoyed
- Manic Miner
- Posts: 436
- Joined: Mon Feb 05, 2018 9:56 am
- Location: Northamptonshire
Re: Binary->decimal
Ahhh, I see, Yes, I read it wrong!
Anyway, shorter version, completely missing the point though!
13 bytes
Anyway, shorter version, completely missing the point though!
Code: Select all
ld c,00h
ld b,08h
loop0:
ld a,18h
rlc c
adc a,a
rst 10h
djnz loop0
ret
Re: Binary->decimal
good one bizzley, that must be fast enough to show the score when it changes