The MASM Forum Archive 2004 to 2012

Specialised Projects => Compiler Based Assembler => Assembler With Microsoft Visual C => Topic started by: jj2007 on May 14, 2012, 03:04:29 PM

Title: C++ float
Post by: jj2007 on May 14, 2012, 03:04:29 PM
This is from an example how to use C code in assembler (\Masm32\MasmBasic\MB2C\CalcTest.asc):

extern "C"
int _fltused=1; //get rid of a stupid error message

extern "C"
double _stdcall jjDiv(int x , int y)
{
  try
  {
  float ffx=x*1.001;
  float ffy=y*1.001;
return(ffx/ffy);
  }
  catch (int)
  {
return(0);  //not very useful ;-)
  }
}

Commandline:"%ProgramFiles%\Microsoft Visual Studio 10.0\VC\bin\cl.exe" /c /Oty2 /Zl /EHsc /Gs /GS- /FojjCalc.obj /Fa jjCalc.cpp

Question: What I had in mind was a simple return (x/y) - but Olly tells me C++ uses idiv. What is the correct way to force C++ to return a REAL8? My workaround doesn't look very elegant...
Title: Re: C++ float
Post by: clive on May 14, 2012, 03:08:49 PM
Something like this should probably suffice:
float ffx=(float)x*1.001;
float ffy=(float)y*1.001;
return((double)ffx/ffy);


As it traverses left-to-right
Title: Re: C++ float
Post by: jj2007 on May 14, 2012, 05:06:26 PM
Thanks, Clive - you set me on the right track:

extern "C"
double _stdcall jjDiv(int x , int y)
{
  try
  {
return((float)x/y);
  }
  catch (int)
  {
return(3.14159);
  }
}


The catch hander is pretty useless, though, as fidiv does not trigger the exception. I would have to set the appropriate FPU mask bit :8)