Intel 64 and IA-32 Architectures Software Developers Manual Volume 1, Basic Architecture
Vol. 1 8-37
PROGRAMMING WITH THE X87 FPU
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 overwrites 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.