Page 1 of 1

save FPU ST0 to GPR32

Posted: Tue Feb 03, 2009 12:22 am
by blackoil
Hi,

I want to save FPU ST0 content to GPR32

int i;
float f;

fld dword [ f ]
push eax ;placeholder
fistp dword [ esp ]
pop eax
mov [ i ], eax

is it ok?

Re: save FPU ST0 to GPR32

Posted: Wed Feb 04, 2009 9:44 am
by Troy Martin
There are no "int" and "float" datatypes in assembly AFAIK. And I'm pretty sure ST0 is an 80-bit register, not a 32-bit one.

Re: save FPU ST0 to GPR32

Posted: Wed Feb 04, 2009 12:16 pm
by Craze Frog

Code: Select all

fld dword [ f ]
push eax ;placeholder
fistp dword [ esp ]
pop eax 
mov [ i ], eax
You're not saving the contents, you're saving the value. And it can be done better.

Code: Select all

fld dword [f]
fistp dword [esp-4] ; placeholder
mov eax, dword [esp-4]
mov dword [i], eax
I am assuming you really need to get it to the register before putting it into i.

Re: save FPU ST0 to GPR32

Posted: Wed Feb 04, 2009 1:18 pm
by ru2aqare
Craze Frog wrote:

Code: Select all

fld dword [f]
fistp dword [esp-4] ; placeholder
mov eax, dword [esp-4]
mov dword [i], eax
I am assuming you really need to get it to the register before putting it into i.
What if there is an interrupt between fistp and mov? Then [esp-4] is thrashed. A better solution would be

Code: Select all

push eax ; push dummy value
fistp dword ptr [esp+0]
pop dword ptr [i]

Re: save FPU ST0 to GPR32

Posted: Fri Feb 06, 2009 9:52 am
by Craze Frog
A better solution would be
Nice, except that it's slower and also doesn't save FPU ST0 to GPR32 (which was vital to the question). If all he wanted to was to get ST0 into an integer memory location he would fistp it directly to the memory location like this:

Code: Select all

fld dword [v_f]
fistp dword [v_i]

Re: save FPU ST0 to GPR32

Posted: Fri Feb 06, 2009 9:43 pm
by blackoil
well, it's part code of my compiler for something like

int i;
flloat f;

i = (int) f;

I use it to draw bezier curve, it works well on real machine.
I don't get any stack corruption, as the placeholder ensure the correct value.