float conversion in bochs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
wndproc
Posts: 20
Joined: Sat Feb 02, 2008 3:02 pm
Location: Austria

float conversion in bochs

Post 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:

Code: Select all

640000 4294327296
On Bochs though I get this:

Code: Select all

3136 3136
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:

Code: Select all

3136 3136
Any suggestion is welcome, thank you.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: float conversion in bochs

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
wndproc
Posts: 20
Joined: Sat Feb 02, 2008 3:02 pm
Location: Austria

Re: float conversion in bochs

Post 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
wndproc
Posts: 20
Joined: Sat Feb 02, 2008 3:02 pm
Location: Austria

Re: float conversion in bochs

Post 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:

Code: Select all

_asm finit;
(Or what ever the naming convention is like, for inline assembler at your compiler.)
Post Reply