News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Floating point exponent calculation

Started by kevinr, March 31, 2009, 04:43:16 AM

Previous topic - Next topic

kevinr

Hello,
I am currently learning floating points and we are supposed to covert something like:

1.5 to +1.10000000000000000000000 E+0

Now i know that floating points are stored as [signed bit][exponent][fraction]

So for 1.5 i get [0011 1111 1100 0000 0000 0000 0000 0000].

Now i was able to get everything but the exponent part which needs to be something like E+2, E-3 etc. I use shl eax, 1 and check the carry flag. I can check if exponent is positive / negative by the leading bit in [011 1111 1] but how do i covert [011 1111 1] = 0?

Btw, i cannot use any external functions.

Any help would be greatly appreciated. :)
Thanks,
Kevin

raymond

Have a look at the following:

http://www.ray.masmcode.com/tutorial/fpuchap2.htm#floats

If you think it may be useful and want to refer to it offline, you can download the entire FPU tutorial from the main site.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Mirno

Your course should have covered the basics of the IEEE754 (32bit) floating number already.

Bit 31 is the sign bit
Bits 30 down to 23 are the exponent
Bits 22 down to 0 are the mantissa.

You will also have learned that the exponent is treated as an unsigned number, but biased by 127 to allow for negatives. In other words before you do any calculations on it, you need to subtract 127.

You will also have covered the fact that the mantissa, while only being stored as a 23 bit number, all calculations are done as if it were a 24 bit number with the most significant (23rd bit) set. The so called implied one.

These rules are the general case, however neither apply in very specific circumstances - NaN, and zeros are both special cases, and treat the exponent slightly differently. In the case of NaN, you may or may not be aware that the mantissa is non-zero, however the spec is vague as to the value it must have. It is recommended that the mantissa be used as a method of error reporting in this instance - however it is not mandatory, or specified as to how this should be done.

Like I said, you will already be aware of (most of) these facts, because they will have been covered in detail in your lectures, and accompanying notes.

Mirno

kevinr

Thx for your replies :)

I tried to separate out the byte for exponent into its own variable but i cannot seem to do it so i can find the value and subtract from it.

I understand the floating point concept but i cannot seem to code it in assembly.

Any tips?

d0d0

Hi Kevin,

I had the same problem as you when I started looking at fp computations. If you already know the theory then you should be able to convert it on paper :bg. I found it much easier to work throught it with a pencil and paper before thinking about cpu instructions. You can then ask for help with your code here.

If not then go back to the basics. Raymond's tutorial helped me alot.

Mirno

In order to get the "byte" value of the exponent, you will need to shift it right.
In fact you'll need to shift it right 23 bits (get bit 23 - the start of the exponent - into bit zero).
You'll also need to mask out the other bits (and will do this).

The order you do these steps is up to you, but one is easier to read IMHO.

As much as everyone here is willing to help you learn, we are more inclined to help you improve your code. If you post what you've tried, and ask why it doesn't work the way you expect we can see that not only are you trying to do the work yourself, but we get an idea of your thought process and it's errors...
I hope you understand as there are a lot of clever people on the board, but every year we get people trying to cheat on their homework - this doesn't advance the learning of assembly (our goal), or help the individual trying to cheat.

Thanks,

Mirno

kevinr

Thanks guys!

I just masked it and shifted right and it worked  :U

Thank YOU!