User Guide

240 x87 Floating-Point Programming
AMD64 Technology 24592—Rev. 3.15—November 2009
Figure 6-2. x87 Physical and Stack Registers
Stack Organization. The bank of eight physical data registers, FPR0–FPR7, are organized internally
as a stack, ST(0)–ST(7). The stack functions like a circular modulo-8 buffer. The stack top can be set
by software to start at any register position in the bank. Many instructions access the top of stack as
well as individual registers relative to the top of stack.
Stack Pointer. Bits 13–11 of the x87 status word (“x87 Status Word Register (FSW)” on page 241)
are the top-of-stack pointer (TOP). The TOP specifies the mapping of the stack registers onto the
physical registers. The TOP contains the physical-register index of the location of the top of stack,
ST(0). Instructions that load operands from memory into an x87 register first decrement the stack
pointer and then copy the operand (often with conversion to the double-extended-precision format)
from memory into the decremented top-of-stack register. Instructions that store operands from an x87
register to memory copy the operand (often with conversion from the double-extended-precision
format) in the top-of-stack register to memory and then increment the stack pointer.
Figure 6-2 shows the mapping between stack registers and physical registers when the TOP has the
value 2. Modulo-8 wraparound addressing is used. Pushing a new element onto this stack—for
example with the FLDZ (floating-point load +0.0) instruction—decrements the TOP to 1, so that
ST(0) refers to FPR1, and the new top-of-stack is loaded with +0.0.
The architecture provides alternative versions of many instructions that either modify or do not modify
the TOP as a side effect. For example, FADDP (floating-point add and pop) behaves exactly like
FADD (floating-point add), except that it pops the stack after completion. Programs that use the x87
registers as a flat register file rather than as a stack would use non-popping versions of instructions to
ensure that the TOP remains unchanged. However, loads (pushes) without corresponding pops can
cause the stack to overflow, which occurs when a value is pushed or loaded into an x87 register that is
513-134.eps
79 0
13 11
fpr0
TOP
x87
Status
Word
fpr1
fpr2
fpr3
fpr4
fpr5
fpr6
fpr7
ST(6)
ST(7)
ST(0)
ST(1)
ST(2)
ST(3)
ST(4)
ST(5)