Intel 64 and IA-32 Architectures Software Developers Manual Volume 1, Basic Architecture

Vol. 1 8-5
PROGRAMMING WITH THE X87 FPU
The style of programming demonstrated in this example is supported by the floating-
point instruction set. In cases where the stack structure causes computation bottle-
necks, the FXCH (exchange x87 FPU register contents) instruction can be used to
streamline a computation.
8.1.2.1 Parameter Passing With the x87 FPU Register Stack
Like the general-purpose registers, the contents of the x87 FPU data registers are
unaffected by procedure calls, or in other words, the values are maintained across
procedure boundaries. A calling procedure can thus use the x87 FPU data registers
(as well as the procedure stack) for passing parameter between procedures. The
called procedure can reference parameters passed through the register stack using
the current stack register pointer (TOP) and the ST(0) and ST(i) nomenclature. It is
also common practice for a called procedure to leave a return value or result in
register ST(0) when returning execution to the calling procedure or program.
When mixing MMX and x87 FPU instructions in the procedures or code sequences, the
programmer is responsible for maintaining the integrity of parameters being passed
in the x87 FPU data registers. If an MMX instruction is executed before the parame-
ters in the x87 FPU data registers have been passed to another procedure, the
parameters may be lost (see Section 9.5, “Compatibility with x87 FPU Architecture”).
Figure 8-3. Example x87 FPU Dot Product Computation
(a)
R7
R6
R5
R4
R3
R2
R1
R0
Computation
ST(0)5.6
(b)
R7
R6
R5
R4
R3
R2
R1
R0
ST(0)
13.44
(c)
R7
R6
R5
R4
R3
R2
R1
R0
ST(1)
ST(0)
13.44
(d)
R7
R6
R5
R4
R3
R2
R1
R0
ST
(
ST
39.14
13.44
52.58
Dot Product = (5.6 x 2.4) + (3.8 x 10.3)
Code:
FLD value1 ;(a) value1 = 5.6
FMUL value2 ;(b) value2 = 2.4
FLD value3 ; value3 = 3.8
FMUL value4 ;(c)value4 = 10.3
FADD ST(1) ;(d)