简体   繁体   English

如何在32位中引发浮点错误

[英]How do you provoke a floating point error in 32 bits

如何引发32位浮点错误(通常称为协处理器错误[INT 16:8086]。

From Art of Assembly, FPU Control Register 装配艺术,FPU控制寄存器

Bits zero through five are the exception masks. 从0到5的位是异常掩码。 These are similar to the interrupt enable bit in the 80x86's flags register. 这些类似于80x86标志寄存器中的中断使能位。 If these bits contain a one, the corresponding condition is ignored by the 80x87 FPU. 如果这些位包含1,则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. 但是,如果任何位包含零,并且发生相应的条件,则FPU立即生成中断,以便程序可以处理退化条件。

Make sure the Control Register has 6 lsbs cleared, then produce any of the conditions. 确保控制寄存器清除了6 lsbs,然后产生任何条件。 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 在x64 / i5 / gcc 4.6 / ubuntu上输出

Floating point exception 浮点异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM