I’m trying to get a basic understanding of floating point operations on x86. I understand that we have a dedicated FPU with a stack, but I’m not finding much relevant information on how the stack behaves in terms of different instructions.
Basically, the addressing of the fpu registers confuses me. If I refer to st(#), am I talking about a specific register? Or is it an offset from the top of the stack?
I think most of my questions can be answered by this one example:
If I have an empty FPU stack, and run:
fld x
fld y
fmul st, st(1)
Will the result be:
ST(0) = y * x
ST(1) = x
or:
ST(0) = x * y
ST(1) = y
?
Note that the difference between these is the value in ST(1).
It’s an offset from the top. The loads push the existing items further into the stack, the pops make them move back closer to the top. Here’s how your little program would look execute:
This reference explains it all pretty well.