save FPU ST0 to GPR32

Programming, for all ages and all languages.
Post Reply
blackoil
Member
Member
Posts: 146
Joined: Mon Feb 12, 2007 4:45 am

save FPU ST0 to GPR32

Post 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?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: save FPU ST0 to GPR32

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: save FPU ST0 to GPR32

Post 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.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: save FPU ST0 to GPR32

Post 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]
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: save FPU ST0 to GPR32

Post 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]
blackoil
Member
Member
Posts: 146
Joined: Mon Feb 12, 2007 4:45 am

Re: save FPU ST0 to GPR32

Post 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.
Post Reply