Splitting of REAL4 Binary
As we know that the binary of single precision float contains 1 sign bit, 8 exponent bit and 22 mantissa bit.
Sign Bit
For extracting sign bit we have to shift number, 31 times to right.
.data
num REAL4 -1.5
.code
mov eax, num
shr eax, 31
; eax = 1 then num is negative
; eax = 0 then num is positive.
Exponent
For extracting exponent from REAL4 binary, we will first shift the number 23 times to right and then we will set all other bit to 0 except bit 0-7, with the help of AND instruction.
.data
num REAL4 -1.5
.code
mov eax, num
shr eax, 23
and eax, 0FFh
; Now eax will contain exponent
; you can access it in al as well.
sub al, 127 ; for further processing
For getting appropriate value from these bit you would have to subtract -127 from the extracted exponent bits.
Mantissa
We can extract mantissa from the number, with the help of a simple AND instruction, then we will set all the bit from 22-31 by 0 and keep the rest of the mantissa bits as it is.
In this case we won’t need shifting.
.data
num REAL4 -1.5
.code
mov eax, num
and eax, 07FFFFFh
; eax not contain 22 bit mantissa
Printing of floating
Sign: You must be kidding. (Print ‘+’ if sign bit is 0 else print ‘-‘)
Calculating Mantissa value (Value after point)
Let say we have 8 bit REAL number, with 3 exponent bit and 4 bits mantissa. In this case we will have exponent max value of 7 and mid value 3
Num = 1 100 0100 (-3.50000)
Sign = 1
Exp = 100
Mantissa = 0100
Getting integer value from mantissa
Exp = 4 – 3 = 1
Now for further processing we will add 1 in starting of mantissa like this: 1.0100
And then move point as per exponent value, then we will get 11.100, then
We can simply print left value of mantissa which is equal to 3 but the problem is with 100 right bit in mantissa which is 100, if we simply look at them, then they are equal to 4 but this is not the case. It is equal to 0.5000
For solving this issue we calculate in a following way:
We will first take the right most bit then get its power of 2 which is in this case equal to 2^1 = 2.
You should know that REAL4 numbers can only give precise 10 digit value and others are in power. That is why our dividend will be 1000000000
Mantissa Right Bit | 1 | 0 | 0 | 0 |
---|---|---|---|---|
Power | 2 (2^1) | 4 (2^2) | 8 (2^3) | 16 (2^4) |
Note: We are not dealing with 2^-1 which is the normally we do on paper.
1000/2 = 500
We can print it like .500
We can change value of dividend to 1000000 for REAL4 case. Or equal to 2^22 aligned to zero like 4194304 to 1000000.
Let say we have mantissa: 1110
Mantissa Right Bit | 1 | 1 | 1 | 0 |
---|---|---|---|---|
Power | 2 (2^1) | 4 (2^2) | 8 (2^3) | 16 (2^4) |
1000/2 = 500
1000/4 = 250
1000/8 = 125
Result = 500 + 250 + 125 = 875
Which we can write as 0.875
Example Comparision Table
Power of 2 | Division by 1 (floating number) | Division by 1000 (integer number) |
---|---|---|
2 | 1/2 = 0.5000 | 1000/2 = 500 |
4 | 1/4 = 0.2500 | 1000/2 = 250 |
8 | 1/8 = 0.1250 | 1000/2 = 125 |
Hope I conveyed the logic behind the printing of Floating number. I had spend two days to grab this concept. I would love to hear from you.
There are more thing to handle like Infinity, NAN and exceeding exponent but those can be deal later.
My Implementation:
https://gist.github.com/soachishti/b4bf2f55b99ef123a1a3
Irvine Lib Implementation (WriteFloat):
https://gist.github.com/soachishti/a4d7bb15ee6519f61c0a