background image

Vol. 1 8-27

PROGRAMMING WITH THE X87 FPU

tectures Software Developer’s Manual, Volumes 2A & 2B, list the floating-point exceptions that can be signaled for 
each floating-point instruction.
See Section 4.9.2, “Floating-Point Exception Priority,” for a description of the rules for exception precedence when 
more than one floating-point exception condition is detected for an instruction.

8.5.1 

Invalid Operation Exception

The floating-point invalid-operation exception occurs in response to two sub-classes of operations:

Stack overflow or underflow (#IS)

Invalid arithmetic operand (#IA)

The flag for this exception (IE) is bit 0 of the x87 FPU status word, and the mask bit (IM) is bit 0 of the x87 FPU 
control word. The stack fault flag (SF) of the x87 FPU status word indicates the type of operation that caused the 
exception. When the SF flag is set to 1, a stack operation has resulted in stack overflow or underflow; when the flag 
is cleared to 0, an arithmetic instruction has encountered an invalid operand. Note that the x87 FPU explicitly sets 
the SF flag when it detects a stack overflow or underflow condition, but it does not explicitly clear the flag when it 
detects an invalid-arithmetic-operand condition. As a result, the state of the SF flag can be 1 following an invalid-
arithmetic-operation exception, if it was not cleared from the last time a stack overflow or underflow condition 
occurred. See Section 8.1.3.4, “Stack Fault Flag,” for more information about the SF flag.

8.5.1.1  

Stack Overflow or Underflow Exception (#IS)

The x87 FPU tag word keeps track of the contents of the registers in the x87 FPU register stack (see Section 8.1.7, 
“x87 FPU Tag Word”). It 
then uses this information to detect two different types of stack faults:

Stack overflow — An instruction attempts to load a non-empty x87 FPU register from memory. A non-empty 
register is defined as a register containing a zero (tag value of 01), a valid value (tag value of 00), or a special 
value (tag value of 10).

Stack underflow — An instruction references an empty x87 FPU register as a source operand, including 
attempting to write the contents of an empty register to memory. An empty register has a tag value of 11.

NOTES

The term stack overflow originates from the situation where the program has loaded (pushed) 
eight values from memory onto the x87 FPU register stack and the next value pushed on the stack 
causes a stack wraparound to a register that already contains a value.
The term stack underflow originates from the opposite situation. Here, a program has stored 
(popped) eight values from the x87 FPU register stack to memory and the next value popped from 
the stack causes stack wraparound to an empty register.

When the x87 FPU detects stack overflow or underflow, it sets the IE flag (bit 0) and the SF flag (bit 6) in the x87 
FPU status word to 1. It then sets condition-code flag C1 (bit 9) in the x87 FPU status word to 1 if stack overflow 
occurred or to 0 if stack underflow occurred. 
If the invalid-operation exception is masked, the x87 FPU returns the floating point, integer, or packed decimal 
integer indefinite value to the destination operand, depending on the instruction being executed. This value over-
writes the destination register or memory location specified by the instruction.
If the invalid-operation exception is not masked, a software exception handler is invoked (see Section 8.7, 
“Handling x87 FPU Exceptions in Software”) and the
 top-of-stack pointer (TOP) and source operands remain 
unchanged.

8.5.1.2  

Invalid Arithmetic Operand Exception (#IA)

The x87 FPU is able to detect a variety of invalid arithmetic operations that can be coded in a program. These oper-
ations are listed in Table 8-10. (This list includes the invalid operations defined in IEEE Standard 754.)
When the x87 FPU detects an invalid arithmetic operand, it sets the IE flag (bit 0) in the x87 FPU status word to 1. 
If the invalid-operation exception is masked, the x87 FPU then returns an indefinite value or QNaN to the destina-