How to convert binary number to decimal

Started by
5 comments, last by wiseduck 18 years, 6 months ago
I am programming a small game for an old 8-bit computer (MSX). It needs to be in assembly. I am going very well, but got caught on that problem. How to have a binary counter displayed in decimal? To make it easier to some, how would you turn an int into a char* (string C-style) with your own function?
Advertisement
You need to use modulo (%) multiple times in base 10 to get out single digits from your int. Then, from the number 0-9 you get from the iterative modulo result, use that to figure out which ASCII character to output based on the ASCII table.

I.e. if "0" in ASCII is decimal 60, then your char is (60 + modulo_result). Disclaimer: I don't think "0" is 60, but I'm too lazy to look it up.

Did that make sense?
Yeah, it did make sense. The main problem with your method is that it relies too much on division. The machine I am programming on uses Z80A (3,6MHz), it is CISC, don't have hardware division, and takes aproximately 0.01 second to multiply two 16-bit numbers.

Off course what would be needed in this case is division of a 16 bit number of a 4 bit one. It could be much more faster with this same algorythm, but I seek something even more optmized.

Thanks!

[Edited by - wiseduck on October 6, 2005 1:40:24 AM]
Wow... I didn't think of the performance constraints of using division! :)

Good luck solving this one then.
Hopefully this applies, but I've only written one assembler program and that was years ago.

Try reading each individual bit and perform the following equations

number in base 10 =

(2^0) * rightmost bit
+ (2^1) * next rightmost bit
+ (2^2) * next rightmost bit
+ (2^3) * next rightmost bit
+ (2^4) * next rightmost bit
+ (2^5) * next rightmost bit
+ (2^6) * next rightmost bit
+ (2^7) * next rightmost bit

The ^ means the 2 is raised to the power of the bit position.

The maximum value it can have is 255.

Another possibility is to read it in as a char type, then key off the ASCII ASCII to retrieve the decimal value based on an offset from some base value.

It also seems that for an 8 bit system that their should be an 8 bit integer type available so you could directly convert it.


[Edited by - APCJim on October 6, 2005 3:33:12 AM]
I thought some more about it, and how assembler code works (though I don't remember all the commands) and came up with this:

1: push the counter value into a register (I'll keep calling it counter)
2: push 00000000 into a second register

3: if the counter is larger than 100 (or 01100100 in binary) add 00000001 to the 2nd register and subtract 100 (or 01100100 in binary) from the counter
4: if the counter is larger than 100 (or 01100100 in binary) add 00000001 to the 2nd register and subtract 100 (or 01100100 in binary) from the counter

5: Push 00000000 into a 3rd register
6: If the counter is larger than 10 (or 00001010 in binary) add 00000001 to the 3rd register and subtract 10 (or 00001010 in binary)from the counter
7: Repeat step 6 until the counter is less than 10

The remainder in the counter is your rightmost digit.

To convert to ASCII:
Add the value in the counter register to the ASCII 0 and push that value to the screen as your right most digit.
Add the value in the 3rd register to the ASCII 0 and push that value to the screen as your second digit.
If the second register is 0 then push an ASCII blank to the screen or add it to ASCII 0 and push that to the screen for your left most digit.
From what it looks, your second solution is a kind of a static modulo solution. Sure it is better than a generic algorythm, but I can't know if it will be fast enough (I will try it).

Your first attempt is the one I liked most. How couldn't I think of it before... Using each 8-bit as a character! Treat it like a string from the start. Thinking a bit more, I could put each algarism in 4 bits and have 2 algarisms in a register/memory position. This will spend more memory, but it is so much more faster that it is worth.

Thanks, man!

This topic is closed to new replies.

Advertisement