How do you provoke a floating point error in 32 bits (commonly known as coprocessor error [INT 16 :8086].
From Art of Assembly, FPU Control Register
Bits zero through five are the exception masks. These are similar to the interrupt enable bit in the 80x86's flags register. If these bits contain a one, the corresponding condition is ignored by the 80x87 FPU. However, if any bit contains zero, and the corresponding condition occurs, then the FPU immediately generates an interrupt so the program can handle the degenerate condition.
Make sure the Control Register has 6 lsbs cleared, then produce any of the conditions. Divide by zero is probably the easiest to produce.
int main()
{
int cw=0;
asm("fstcw (%0)\n\t"::"r"(&cw):"memory"); cw &= ~0x3f;
asm("fldcw (%0)\n\t"::"r"(&cw):"memory");
asm("fldz"); // divide 1 by 0.0
asm("fld1"); // or just omit these two loads if you have 387+ :)
asm("fdivp");
asm("wait"); // This is mandatory
return 0;
}
Output on x64/i5 / gcc 4.6 / ubuntu
Floating point exception
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With