convert binary to decimal and show in assembly

15,589

No matter what language you use converting to decimal to print in decimal is the same process. Well one of two processes. You can start from either end. Say you had a 16 bit number 12345 (0x3039).

The first way would be

divide by 10000 result is 1 remainder 2345 save or print the 1
divide by 1000 result is 2 remainder 345 save or print the 2
divide by 100 result is 3 remainder 45 save or print the 3
divide by 10 result is 4 remainder 5 save or print the 4 and 5

The second way

divide by 10 result is 1234 remainder 5 save the remainder use the result
divide by 10 result is 123 remainder 4 save the remainder use the result
divide by 10 result is 12 remainder 3 save the remainder use the result
divide by 10 result is 1 remainder 2 save both the remainder and result 
print the results in the right order

Now if your question is how do I divide a 64 bit number by these powers of 10 with the instruction set I have, well sometimes you can, sometimes you cant, sometimes you have to use other math rules. Dividing by 10 is the same as dividing by 2*5 so you could divide by 2 (shift) then divide by 5.

0x3039 (12345) divided by 10000 is the same as shift right 4 then divide by 5 to the power 4 (625). 0x303 = 771, 771/625 = 1. If your divide isnt that big your multiply might not be either so 1 * 625 = 0x271, 0x271 << 4 = 0x2710, 0x3039 - 0x2710 = 0x929 = 2345. Once you get down to a number you can divide with the hardware then use the hardware.

You might need to divide by one of the numbers in the middle, say you had a 32 bit number (max 4,294,967,296) and you have hardware that can divide from a 32 bit number into 16 bit result and 16 bit remainder. You can knock a couple of digits off using the above method say leaving 94,967,295 then divide by 10000 giving 9496 remainder 7295 then work through those four digit numbers independently using the hardware.

If you dont have division hardware but multiply hardware (yes I know you specified 8086) you can do this:

http://www.divms.uiowa.edu/~jones/bcd/divide.html

If you remember from grade school how to do a multiply on pencil and paper:

    1234
   x1010 
   =====
    0000  
   1234   
  0000
+1234 
========
 1246340

Binary makes that quite simple as you might picture from the numbers I chose

  abcd
x efgh
====== 

If you want to multiply the four bits abcd times the four bits efgh then:

result = 0;
if(h) result+=abcd << 0;
if(g) result+=abcd << 1;
if(f) result+=abcd << 2;
if(e) result+=abcd << 3;

With most instruction sets you can cascade this mulitply as wide as you have memory, want to multiply 1 million bits by 1 million bits. no problem, 500,000 bytes plus a little more or a few registers (and a lot of clock cycles).

Share:
15,589
Moein Hosseini
Author by

Moein Hosseini

I'm Moein who is into GNU/Linux, programming, open source and reading. Currently, I'm at AmirKabir University of Technology (Tehran Polytechnic) for M.S of Information Technology. My search interests are Natural Language Processing, Big Data and Machine Learning. Also, my thesis is about Forecasting the news impact on different aspects of social media users political opinions. Right now I work at BisPhone as Software Engineer. I took up my B.A in Computer Engineering from K.N.Toosi University of Technology and also graduated from Nodets for middle and high school.

Updated on June 05, 2022

Comments

  • Moein Hosseini
    Moein Hosseini almost 2 years

    I have array of word,with 4 cells

    RESULT DW 4 DUP(0)

    it will contain Binary number,for example

    MOV RESULT,   0H
    MOV RESULT+2, 0H
    MOV RESULT+4, 35A4H
    MOV RESULT+6, E900H
    

    now result contain 0000000035A4E900H which means 900000000 in Decimal way. not I wanna print 900000000 on monitor.

    what should I do?