Page 1 of 1
float conversion in bochs
Posted: Thu Jul 03, 2008 12:43 am
by wndproc
Hi again,
I've got a problem, which can be simply put in a few lines:
Code: Select all
float var = (float)width;
Print((dword)(var * 1000.0f));
Print(" ");
Print((dword)(-var * 1000.0f));
(The Print(dword var); function here is self describing and well tested.)
(width is a dword with the value 640.)
On MS Virtual PC and real hardware I get this as a result:
On Bochs though I get this:
And here's some more weired behavior:
Code: Select all
float var = (float)width;
Print((dword)var);
Print(" ");
Print((dword)-var);
This gives me the same output:
Any suggestion is welcome, thank you.
Re: float conversion in bochs
Posted: Thu Jul 03, 2008 1:05 am
by Brendan
Hi,
I can think of one possiblity...
If you tell the CPU that an FPU is present by clearing the EM flag in CR0, then when the CPU sees an FPU instruction it will give it to the FPU and the CPU will skip the instruction (assuming that the FPU will do it). In this case, for older CPUs (that don't have an FPU) instructions that need an FPU will be skipped by the CPU and won't be executed because there is no FPU.
Your code probably does "fld [width]; fistp [var]" to convert float into int, and if these instructions are ignored then (assuming it's a local variable on the stack) "var" would end up containing whatever happened to be on the stack beforehand.
Basically, AFAIK it's possible that your real computers and MS Virtual PC do have an FPU, and Bochs is configured without FPU support; and your code (or the generic Bochs BIOS or Bochs itself?) clears the MP flag in CR0 regardless of whether or not an FPU is present.
I could be entirely wrong though...
Cheers,
Brendan
Re: float conversion in bochs
Posted: Thu Jul 03, 2008 1:18 am
by wndproc
Thank you for the quick reply. I'll do some research on this.
As for now I have tried pushing something on the stack before "float var = (float)width;" and popping it off befor the "Print()".
Result: No matter what I push, bochs now always results in "0 0", whereas MSVPC outputs the same as always
Re: float conversion in bochs
Posted: Thu Jul 03, 2008 2:08 am
by wndproc
Problem solved:
I have never used floating point instructions using assembler code. Therefore I guess I've missed some very basics. I was impressed what can be done on the fpu these days (fsincos, fsqrt, ...) and inquisitive as I am, I tried some of them.
INCLUDING FINIT
So, the fpu wasn't initialized on bochs startup. Until today I didn't even know the fpu has to be initialized, but that's the way you learn it.
Conclusion:
Everyone out there having troubles with flouts in bochs, try adding this one line to your source:
(Or what ever the naming convention is like, for inline assembler at your compiler.)