Click to See Complete Forum and Search --> : MSVC inline assembly query


mdale9
February 15th, 2005, 07:04 AM
I am a bit of a newbie to assembly and only need to do one thing at present, which is access the FSINCOS operation on the x87 math co-processer. I have trawled the web and found a few references and have come up with the following code:


void SinCos(double Angle, double *SinAns, double *CosAns) {
__asm {


fld QWORD PTR [Angle]
fsincos
fstp QWORD PTR [CosAns]
fstp QWORD PTR [SinAns]
fwait
}
}

main()
{

double s=0.0;
double c=0.0;
double ang = 3.0;


SinCos(ang,&s,&c); //call one version of the code


//call the other one version of the code
__asm {

fld QWORD PTR [ang]
fsincos
fstp QWORD PTR [c]
fstp QWORD PTR [s]
fwait
};

}

When I single step through it and look at the registers the operation is occuring as expected but only the second version pops the values into "c" and "s".

I am using VC++ 6.0 Professional with the default compiler options:
/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/small_test.pch" /YX /Fo"Debug/"

Please help.

Bob Davis
February 15th, 2005, 07:57 AM
I'm no expert in x86 assembly, so this could be pretty far off, but I notice one big difference in the two versions of the function you showed. In the first one, the symbols that you pass to the fstp instructions are already declared as pointers in your C code, while in your second version (the one that works), the symbols are plain old double variables.

It seems, then, that in your assembly, you have an extra level of indirection. Instead of storing the result in the variable, you're altering the actual value of the pointer that you're passing to your SinCos function. I don't know exactly how to syntactically fix what you're seeing. Remove the "ptr" token in those lines of code, maybe?

jwbarton
February 15th, 2005, 08:24 AM
Bob Davis is correct, you have an extra level of indirection.

You need to do something like:


void SinCos( double ang, double* sinAns, double* cosAns )
{
_asm
{
fld QWORD PTR [ang]
fsincos
mov ebx,[cosAns] ; get the pointer into ebx
fstp QWORD PTR [ebx] ; store the result through the pointer
mov ebx,[sinAns]
fstp QWORD PTR [ebx]
}
}


Best regards,
John

Andreas Masur
February 15th, 2005, 08:27 AM
[ Moved thread ]