Hi all!
I'd like to write a function to convert real4 number to ascii string. And I have some questions. For example, I have number 5.685. A processor represents it as 40B5EB85h or 01000000101101011110101110000101b, where 31 bit is a sign of the number, 30-23 bits are exponent and 22-0 bits are mantissa. I don't understand how to convert this binary representation of the number to decimal. :( I don't need a code, I need an algorithm.
Thank you!
Posted on 2002-01-29 22:29:20 by vkim
Here is the break down:

REAL4 5.685
DWORD 40B5EB85h
0100 0000 1011 0101 1110 1011 1000 0101

0 = sign

100 0000 1 = Exponent + 127

(note)-->1.011 0101 1110 1011 1000 0101 = number

(note) In the number there is always an [u]implied[/u] one
bit to create 24 bits of percision!

Algorithm:

Subtract 127 from exponent and shift decimal point
in number to get actual value.

101.1 0101 1110 1011 1000 0101 = actual value

Let me know if you need more info.
Glad to help!

p.s. Awesome debug tool!
Posted on 2002-01-29 23:12:30 by bitRAKE
bitRAKE,

I'm sorry for my dullness. :(

Subtract 127 from exponent and shift decimal point
in number to get actual value.

101.1 0101 1110 1011 1000 0101 = actual value

1) 10000001b-127=2. What must I do with this value?
2) 101 = 5 (ok, I understand it)
1 0101 1110 1011 1000 0101 = 1436549 (sorry, I don't understand it)

P.S. You are right, I need this function for the next version of DbgWin. ;)
Posted on 2002-01-30 00:20:05 by vkim

exponent = [30-23] -127
fraction = 1 + [22-0]

REAL4 = fraction * 2^exponent

But we want the number in the form A * 10^y

y = INT(exponent * log10(2))
A = fraction * 2^(FRAC(exponent * log10(2)))

Sorry, I have a hard time explaining without some code.

1) 10000001b-127=2. What must I do with this value?
2) 101 = 5 (ok, I understand it)
1 0101 1110 1011 1000 0101 = 1436549 (sorry, I don't understand it)

1) use this value to calculate the power of the exponent
2) This is a fixed binary decimal.
To convert it to decimal multiply by a power of ten.

Edit: I know you didn't want code, but I would like to code an integer version that doesn't effect the FPU stack. I will post it later. Please use it in your debug code if you wish.
Posted on 2002-01-30 00:44:18 by bitRAKE
bitRAKE,

Thank You very much for Your explanations! I would be very glad if you write some functions & macros for the debugging tool. For example, PrintReal4, PrintReal8, PrintReal10, ShowFPU, etc. Of course, Your copyright will be stored. :)
Posted on 2002-01-30 01:48:00 by vkim
This just demonstrates convertion from binary to decimal:
On the left side of the binary point 101 = 5
On the right side of the binary point
negative bits 1;3;5;6;7;8;10;12;13;14;19;21 are set
and can be converted as follows:


For a binary number, every time you move to the left
the power of 2 is doubled:
bit 00 1
01 2
02 4 etc.
everytime you move to the right the power of 2 is halved:
bit -01 0.5
bit -02 0.25
bit -03 0.125 etc.

In this example:

101.1 0101 1110 1011 1000 0101
bit -0 -1 -2
210 1 2345 6789 0123 4567 8901

bit 02 4 x 1 = 4
01 2 x 0 =
00 1 x 1 = 1
-01 0.5 x 1 = 0.5
-02 0.25 x 0 =
-03 0.125 x 1 = 0.125
-04 0.0625 x 0 =
-05 0.03125 x 1 = 0.03125
-06 0.015625 x 1 = 0.015625
-07 0.0078125 x 1 = 0.0078125
-08 0.00390625 x 1 = 0.00390625
-09 0.001953125 x 0 =
-10 0.0009765625 x 1 = 0.0009765625
-11 0.00048828125 x 0 =
-12 0.000244140625 x 1 = 0.000244140625
-13 0.0001223703125 x 1 = 0.0001223703125
-14 0.00006103515625 x 1 = 0.00006103515625
-15 0.000030517578125 x 0 =
-16 0.0000152587890625 x 0 =
-17 0.00000762939453125 x 0 =
-18 0.000003814697265625 x 0 =
-19 0.0000019073486328125 x 1 = 0.0000019073486328125
-20 0.00000095367431640625 x 0 =
-21 0.000000476837158203125 x 1 = 0.000000476837158203125
-22 0.0000002384185791015625 x 0 =
-23 0.00000011920928955078125 x 0 =
-24 0.000000059604644775390625 x 0 =
-25 0.0000000298023223876953125 x 0 =
total = 5.685000241779541015625

Not exactly 5.685 but close!

HTH

Farrier
Posted on 2002-01-30 01:48:44 by farrier
Doesn't Masm32 already have an FPTOA routine?

The only thing is that it uses the FPU, and works with doubles, but at least it should make a starting point to understanding how to change from real4 to ASCII.
Posted on 2002-01-30 06:58:46 by dxantos