6.2.2 Instruction and Operand Formats

Because of the stack organization of registers, you can consider registers either as elements on a stack or as registers much like 8086-family registers. Table 6.2 lists the four main groups of coprocessor instructions and the general syntax for each. The names given to the instruction format reflect the way the instruction uses the coprocessor registers. The instruction operands are placed in the coprocessor data registers before the instruction executes.

Table 6.2 Coprocessor Operand Formats

Instruction Format
Syntax
Implied Operands
Example

Classical stack Faction ST, ST(1) fadd
Memory Faction memory ST fadd memloc
Register Faction ST(num),ST Faction ST, ST(num) fadd st(5), st fadd st, st(3)
Register pop FactionP ST(num), ST faddp st(4), st

Summary: All coprocessor instructions begin with F.

You can easily recognize coprocessor instructions because, unlike all 8086-family instruction mnemonics, they start with the letter F. Coprocessor instructions can never have immediate operands and, with the exception of the FSTSW instruction, they cannot have processor registers as operands.

6.2.2.1 Classical-Stack Format

Instructions in the classical-stack format treat the coprocessor registers like items on a stack—thus its name. Items are pushed onto or popped off the top elements of the stack. Since only the top item can be accessed on a traditional stack, there is no need to specify operands. The first (top) register (and the second if the instruction needs two operands) is always assumed.

In coprocessor arithmetic operations, the top of the stack (ST) is the source operand and the second register [ST(1)] is the destination. The result of the operation goes into the destination operand, and the source is popped off the stack. The result is left at the top of the stack.

Instructions that load constants are one example of instructions that require the classical-stack format. In this case, the constant created by the instruction is the implied source, and the top of the stack is the destination.

This example illustrates the classical-stack format, and Figure 6.3 shows the status of the register stack after each instruction:

fld1 ; Push 1 into first position

fldpi ; Push pi into first position

fadd ; Add pi and 1 and pop

6.2.2.2 Memory Format

Instructions using the memory format, such as data transfer instructions, also treat coprocessor registers like items on a stack. However, with this format, items are pushed from memory onto the top element of the stack or popped from the top element to memory. You must specify the memory operand.

Summary: Some coprocessor instructions operate on integers or BCDs.

Some instructions that use the memory format specify how a memory operand is to be interpreted—as an integer (I) or as a binary coded decimal (B). The letter I or B follows the initial F in the syntax. For example, FILD interprets its operand as an integer and FBLD interprets its operand as a BCD number. If the instruction name does not include a type letter, the instruction works on real numbers.

You can also use memory operands in calculation instructions that operate on two values (see Section 6.2.4, “Using Coprocessor Instructions”). The memory operand is always the source. The stack top (ST) is always the implied destination. The result of the operation replaces the destination without changing its stack position, as shown in this example and Figure 6.4:

.DATA

m1 REAL4 1.0

m2 REAL4 2.0

.CODE

.

.

.

fld m1 ; Push m1 into first position

fld m2 ; Push m2 into first position

fadd m1 ; Add m2 to first position

fstp m1 ; Pop first position into m1

fst m2 ; Copy first position to m2

6.2.2.3 Register Format

Instructions using the register format treat coprocessor registers as registers rather than as stack elements. Instructions that use this format require two register operands; one of them must be the stack top (ST).

In the register format, specify all operands by name. The first operand is the destination; its value is replaced with the result of the operation. The second operand is the source; it is not affected by the operation. The stack position of the operands does not change.

The only instructions using the register operand format are the FXCH instruction and the arithmetic instructions that do calculations on two values. With the FXCH instruction, the stack top is implied and need not be specified, as shown in this example and Figure 6.5:

fadd st(1), st ; Add second position to first -

; result goes in second position

fadd st, st(2) ; Add first position to third -

; result goes in first position

fxch st(1) ; Exchange first and second positions

6.2.2.4 Register-Pop Format

The register-pop format treats coprocessor registers as a modified stack. The source register must always be the stack top. Specify the destination with the register's name.

Instructions with this format place the result of the operation into the destination operand, and the stack top pops off the stack. The effect is that both values being operated on are lost and the result of the operation is saved in the specified destination register. The register-pop format is used only for instructions that do calculations on two values, as in this example and Figure 6.6:

faddp st(2), st ; Add first and third positions and pop -

; first position destroyed;

; third moves to second and holds result