8087 Instruction Set and Example
8087 Instruction Set and Example
Data transfer:
Instruction
result
FLD source
load a real into st(0)
FST dest
store real at dest (86 mem)
FSTP dest
store/pop st(0)
FXCH {st(i)}
exchange two regs (St(0),
St(1)) or St(0) and the operand
FILD source
int load
FIST dest
int store
FISTP dest
int store/pop
FBLD source
bcd load (tbyte source)
FBSTP dest
bcd store tbyte dest
FADD source
add a real (mem) to st(0)
FADD {St(1),ST}
FADD st(i),st or FADD ST,ST(i)
FADDP St(i),ST
add st to st(i) and pop
st(0)
FIADD {st,} source int add to st
Miscellaneous
FSQRT {st}
FABS {st}
FCHS {ST} change sign
FLDZ {st} load a zero
Control word
The control word is a 16 bit word that works like a flag register
on the 86 processor. It keeps information about zerodivide in
bit 2 for example. Bits 3 and 4 are overflow and underflow
respectively.
Precision control is set in bits 8 and 9 and rounding is set in
bits 10 and 11.
Rounding control bit settings are:
00 round to nearest/even
O1 round down
10 round up
11 chop/truncate
Status word
Status word is also a 16 bit word value.
condition bits are named c3, c2,c1 and c0.
Their position in the status word, though is:
C3 is bit 14
C2,c1,c0 are bits 10,9,8 respectively.
If you are curious, the eight possible bit
settings in bits (11,12,13) indicate which
register in the chain is currently st(0)
Int values
87 processor recognizes word, dword and
qword signed int types, in the same manner as
the 86 processor.
The stack
Pushing and popping on the stack change the
register who is currently the top.
Pushing more than 8 times will push the last piece of
data out and put St(0) at the most recently pushed
item.
It is the programmers responsibility to count stack
push/pop operations.
Whoever is on top of the stack, is referenced by St or
ST(0). St(1) is next. And so on to St(7).
Int transfer
FILD: load int. Type (word, dword, etc) is whatever
the operand type is. St(0) is this new value. St(1)
points to the previous value on top.
FIST: copy St(0) value to memory, converting to a
signed int (following rounding control settings in the
control word)
FISTP: same as above, but pop the stack as well.
BCD
FBLD load a bcd (tbyte) onto the stack
FBSTP store a tbyte bcd value into the
memory operand, popping the stack as you
go.
Example:
FBLD myval
FBSTP myval
Int arithmetic
FPREM
Takes implicit operands st,st(1)
Does repeated subtract leaves st>0,(possibly
st==st(1)) or st==0.
May need to repeat it, because it only reduces
st by exp(2,64)
If st>st(1) it needs to be repeated.
FPREM sets bit C2 of the status word if it
needs to be repeated, clears this bit if it
completes operation.
operands
Stack operands may be implicitly referenced.
FIADD, FISUB, FIDIV, FIDIVR, FIMUL and
FISUBR have only one form (example using
add instruction):
FIADD {ST,} intmem
St(0) is implied dest for all of these.
comparison
Condition codes
C3
0
0
1
1
c0
0
1
0
1
st>source
st<source
st==source
not comparable
Example Programs
data segment
org 00h
X DD 9.75
Y DD 13.09375
SUM DD ?
data ends
code segment
assume cs:code, ds:data
start: MOV AX,data
MOV DS,AX
FINIT
FLD X
FLD Y
FADD ST(0), ST(1)
FST SUM
MOV AH,4CH
INT 21H
code ends
END start
C:\MASM615>coprocessor
1234
2468
C:\MASM615>