News:

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

FPU Questions FST etc..

Started by Rainstorm, July 29, 2007, 12:55:50 AM

Previous topic - Next topic

Rainstorm

when loading a value into an FPU register, it needs to be empty or it will produce an error.
but in the case of the FST instruction for example , (assuming the destination is a register) even if it has a value in it, the new value will overwrite it..& be stored in the regisster correctly, without causing a stack fault......is this right ?

So is the stackfault produced only if an attempt to write to the register which is to be the new TOP of the stack is made, & it is not free..or something like that ?

can someone confirm/correct all this for me ...

thankyou

Rainstorm.


raymond

Quote from: Rainstorm on July 29, 2007, 12:55:50 AM
when loading a value into an FPU register, it needs to be empty or it will produce an error.
but in the case of the FST instruction for example , (assuming the destination is a register) even if it has a value in it, the new value will overwrite it..& be stored in the regisster correctly, without causing a stack fault......is this right ?

YES. Whatever value is in ST(0) will overwrite any value in the destination register without generating any invalid operation exception. From the Intel manual:
"If the destination operand is a non-empty register, the invalid-operation exception is not generated."

A good example is the common procedure to "pop the top of the stack" with fstp ST.

QuoteSo is the stackfault produced only if an attempt to write to the register which is to be the new TOP of the stack is made, & it is not free..or something like that ?

YES. When using the FLD or FILD instructions, the value will be loaded into the existing ST(7) register which will then become the ST(0) register. That register MUST be free for the operation to succeed. Otherwise, it will be an invalid operation resulting in an exception (some call it a stack fault) and the "new" ST(0) will contain a NAN (Not-A-Number) destroying any previous content of that register.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Rainstorm

Raymond,

thanks for confirming & clearing that up for me...

Thanks a lot also for writing & making available, your tutorials on the fpu..- Am in the proccess of reading them alongside the intel manuals & getting to know how the FPU works. - Your book has been very helpful.. & from a learner's point of view,.. I have found them excellent.., clear & comprehensible.,,.& very well written.

Rainstorm.

-

Rainstorm

hi,

didn't want to start a seperate thread..

from the masm programmer's guide..         fld    m1      ; Push m1 into first position
         fld    m2      ; Push m2 into first position
         fadd   m1      ; Add m2 to first position


shouldn't the last comment be  'add m1 to first position' ? - since m2 is already in the 1st position there & 'first positon' implies the TOP of the stack from the commenting...or am i getting something wrong ?

TNick

Rainstorm,

fadd m1

will add value in m1 to value in ST(0) and will store the result in ST(0).

Nick

raymond

This example in the masm programmer's guide is definitely in error. As TNick mentioned, fadd m1 obviously adds m1 and not m2.

And many thanks for your appreciation of the tutorial. I'm always glad to hear that it is of some use.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

farklesnots

Raymond,

I want to add my thanks for the FPU tutorial, too. I've used the FPU before, but because most of the documentation talks about ST(0)-ST(7) as a stack, that's how I viewed it. Your choice of a revolver concept to illustrate the use of the FPU made programming with the floating-point instructions much easier and less error-prone for me. Thanks again.

raymond

You're the first one to express appreciation for the "revolver" metaphor. Other options I had considered were:

- A chest with 8 drawers. In order to load a new value in ST(0), it would have entailed removing the bottom drawer, take out all the other drawers one by one and reinsert them in the lower slot, and finally put the former bottom drawer in the top slot. That required way too much effort.

- A fixed array of registers accessed with gates (similar to the actual physical layout of the registers on the chip). Although it may have been closer to reality, it was way too cumbersome to explain.

Since a "stack" in usually visualized as a pile of stuff one on top of the other, trying to describe such a term as "circular" is not that evident. The revolver metaphor seemed the better option.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com