Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you provoke a floating point error in 32 bits

How do you provoke a floating point error in 32 bits (commonly known as coprocessor error [INT 16 :8086].

like image 222
Michael Manuel Vandycke Avatar asked Nov 02 '22 23:11

Michael Manuel Vandycke


1 Answers

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

like image 143
Aki Suihkonen Avatar answered Nov 08 '22 09:11

Aki Suihkonen