Sie sind auf Seite 1von 68

8087 – Math Coprocessor

Numeric Data Processor (NDP)


Why do we require an NDP
• The purpose of the 8087 was to speed up
computations for floating point arithmetic, such
as addition , subtraction, multiplication, division,
and square root.

• Does exponential, logarithmic or trigonometric


calculations, and
Besides floating-point it could also operate
on large binary and decimal integers.

• The performance enhancements were from


approximately 20% to over 500%, depending
on the specific application.
• As a coprocessor (8087) is connected to 8086,
8086 operates in maximum mode.

• Thus the MN/MX* is grounded.

• This interface is also called as coprocessor


configuration. Here 8086 is called as the host
and 8087 as coprocessor as it cannot operate
all by itself.
• Programmer writes a homogeneous program
which contains both 8086 as well as 8087
instructions.

• Only 8086 can fetch instructions but these


instructions also enter 8087.

• 8087 treats 8086 instructions as NOP.

• ESC is used as a prefix for 8087 instructions. When


as instruction with ESC prefix (5 MSB bits as 11011)
is encountered, 8087 is activated. The ESC
instruction is decoded by both 8086 and 8087.
• If the 8087 instruction has only an opcode (no
operands) then 8087 will start execution and 8086
will immediately move its next instruction.

• But if the instruction requires a memory operand,


then 8086 will have to fetch the first word of the
operand as 8087 cannot calculate the physical
address. This word will be captured by 8087. Now
the remaining words (for a large operand) can be
fetched by 8087 by simply incrementing the
address of the first word. Thus 8087 need help
from 8086.
• Once 8087 gets its operand, it begins processing by
making the BUSY output high.

• This BUSY output is connected to the TEST input of


the microprocessor.

• Now 8087 execute its instruction and 8086 moves


ahead with its next instruction. Hence
multiprocessing takes place.
• During execution, if 8087 needs to read/ write
more words from the memory, then it does so by
stealing bus cycles from the microprocessor in the
following manner.

• The RQ*/GT0* of 8087 is connected to RQ/GT0 of


the microprocessor.

• 8087 gives an active low request pulse.


• 8086 completes the current bus cycle and
gives the grant pulse and enters the hold
state.

• 8087 uses the shared system bus to perform


the data transfer with the memory.

• 8087 gives the release pulse and returns the


system bus back to the microprocessor.
• If 8086 requires the result of the 8087 operation, it
first executes the WAIT instruction.

• WAIT makes the microprocessor check the TEST


pin. If the TEST pin is high (8087 is BUSY), then the
microprocessor enters WAIT state. It comes out of
it only when TEST is low (8087 has finished its
execution).

• Thus 8086 get the correct result of an 8087


operation.
• During the execution if an exception occurs, which
is unmasked, 8087 interrupts microprocessor.

• The QS0 and QS1 lines are used by 8087 to


monitor the queue of 8086.

• 8087 needs to know when 8086 will decode the


ESC instruction so it synchronizes its queue with
8086 using QS0 and QS1:
Data types of 8087

Types of data used with all arithmetic


coprocessor family members are
• signed integer,
• BCD, and
• floating-point.

• Each has a specific use in a system, and many


systems require all three data types.
• When used with the coprocessor, signed integers
are 16- (word), 32- (doubleword integer), or 64-
bits (quadword integer) wide.

• Positive numbers are stored in true form


with a leftmost sign-bit of 0.

• Negative numbers are stored in two’s complement


form with a leftmost sign-bit of 1.
• Data are stored in memory using assembler
directives described in earlier chapters.
– DW directive defines words
– DD defines doubleword integers
– DQ defines quadword integers
Binary-Coded Decimal (BCD)
• Each number is stored as an 18-digit packed
integer in nine bytes of memory as two digits
per byte.
– the tenth byte contains only a sign-bit for
the 18-digit signed BCD number
– BCD requires 80 bits of memory
– Both positive and negative numbers are stored
in true form and never in ten’s complement
form the DT directive stores BCD data in the
memory.
Floating-Point Numbers
• Often called real numbers because they hold
signed integers, fractions, & mixed numbers.

• Floating-point numbers are written in scientific


binary notation and have three parts:
– a sign-bit, a biased exponent, and a significand
• Intel coprocessors support three types of floating-
point numbers:
– single (32 bits), double (64 bits) and
temporary (80 bits)
• Convert 10.25 from base 10 to Single
Precision

• 1010.01
• 1.01001 x 2^3

• Convert exponent to correct bias


• Since here bias is 127--- 3 means 127+3=130
• 130 = 1000 0010
• 0 1000 0010 0100 1000 0000 0000 0000 000
Architecture of 8087
• 80X87 is divided into two major sections:
– the control unit and numeric execution unit

• Both devices monitor the instruction stream.


– if the instruction is an ESCape (coprocessor) instruction,
the coprocessor executes it
– if not, the microprocessor executes it
• The control unit interfaces the coprocessor
to the microprocessor-system data bus.

• The numeric execution unit (NEU) is executes


all coprocessor instructions.
• The 8087 control unit internally maintains a parallel
queue, identical to the status queue of the main CPU.

• The control unit automatically monitors the / S7 line


to detect the CPU type and accordingly adjusts the
queue length. The 8087 uses the QS0 and QS1 pins to
obtain and identify the instructions fetched by the
host CPU (8086).

• 8086 identifies the coprocessor instructions using the


ESCAPE code bits in them. Once the CPU recognizes
the ESCAPE code, it triggers the execution of the
numeric processor instruction in 8087.
• The stack within the coprocessor contains
eight registers that are each 80 bits wide.
– these always contain an 80-bit extended-
precision floating-point number

• The only time data appear as any other form


is when they reside in the memory system.
• The 8087 has a 3 bit stack pointer which holds
the number of the register which is the current
top of the stack – ST(0) or simply ST.

• When 8087 is reset, stack pointer is loaded with


000.

• When 8087 reads the first number, stack is


decremented, so it goes to 111 and the number
is loaded there.

• Now 111 becomes ST(0) and 000 becomes ST(1).


• Tag Register – Holds status of contents of 8
Register Stack
• 00 Valid
• 01 – Zero
• 10 - Invalid or Infinity or NaN
• 11 – Empty
Status word is sent to 8086 from 8087
B - The busy shows the status of NEU.
• B=1 means that NEU is busy with execution.

• The condition code bits indicate conditions


about the coprocessor.
• These bits have different meanings for
different instructions
• TOP --- The top-of-stack (ST) bit indicates
the
current register addressed as the top-of-the-
stack (ST).
– This is normally register ST(0).
• The error summary (also called Interrupt Request)
bit is set if any unmasked error bit is set.
– PE, UE, OE, ZE, DE, or IE
• In the 8087 coprocessor, the error summary also
causes a coprocessor interrupt.
PE - The precision error indicates that the result or
operands exceed the selected precision. The result
will be rounded according to rounding control bits.

UE - An underflow error indicates a nonzero


result too small to represent with the current
precision selected by the control word. If this
exception is masked, the 8087 denormalizes the
fraction until exponent fits the destination format.

OE - An overflow error indicates a result too large to


be represented. The condition code will indicate that
the result is infinity.
Z - A zero error indicates the divisor was zero
while the dividend is a non infinity or nonzero
number. The resulting condition code will
indicate the result is infinity, even if the
exception is masked.

DE - A denormalized error indicates at least


one of the operands is denormalized. This may
also be generated, if the result is denormalized.
If this is masked, 8087 continues execution
normally.
IE - An invalid error indicates a stack overflow
or underflow, indeterminate form (0 ÷ 0, +∞, –
∞, etc.), or use of a NAN as an operand.
– indicates errors such as produced by taking the
square root of a negative number, etc.
• The control register selects the precision,
rounding control, and infinity control.
– also masks and unmasks exception bits
corresponding to the rightmost 6 bits of
the status register
• FLDCW is used to load a value into the
control register from a specified source
operand..
• Infinity Control - Two modes are Projective
and Affine closure

• Under affine closure (1)- positive infinity and


negative infinity and considered as opposite
ends of number line.

• Under projective closure(0), positive and


negative infinity are equal.
• Rounding control determines the type of
rounding
• The precision control sets the precision
of the result, as defined
• If the Interrupt Enable Mask is ‘1’, then all the
exceptions are masked.
• If any exceptions occurs, then an on-chip
exception handler runs and execution
continues normally.

• IF IEM = ‘0’, ie unmasked (i.e., mask bit = 0),


then user-written exception handlers take
care of such situations. This is done by using
the INT pin which is normally connected to
one of the interrupt input pins of 8259A PIC.
• 8087 sets the appropriate flag bit in the status
word in case of occurrence of any one of the
exception conditions.

• The exception mask in the control register is


then checked and if the mask bit is set i.e.,
masked, then a built-in fix-up procedure is
followed.

• If the exception is unmasked (i.e., mask bit = 0),


then user-written exception handlers take care
of such situations. This is done by using the INT
pin which is normally connected to one of the
interrupt input pins of 8259A PIC.
• MICROCODE CONTROL UNIT : generates control
signals which are required for execution of
instruction.

• PROGRAMMABLE SHIFTER : used for shifting


operands during execution of instructions like
FMUL, FDIV.

• DATA BUS INTERFACE: connects internal data bus of


8087 to main processor data bus

• Temporary Registers: - It is used to store any


intermediate result.
• Operand Queue: 8087 maintains a parallel
queue similar to the processor, whose length
can be adjusted depending on the processor.
Arithmetic Module: -
All the arithmetic operations are performed in
arithmetic module using arithmetic instructions
like FADD, FMUL, FSQRT, and FPREM.

Exponent Module: -
All the calculations of exponent part are
performed in exponent module.
Instruction Set
1. Data Transfer
2. Arithmetic
3. Comaprison
4. Transcendental
5. Constant
6. Coprocessor Control
Data Transfer Instructions

FLD
FST
FSTP
• FLD loads floating-point memory data to the
top of the internal stack.
– FLD ST(7)
– FLD MEM
• FST stores a copy of the top of the stack to
the memory location or coprocessor register
indicated by the operand.

• FST ST(7)
• FST MEM
• FSTP -
Arithmetic Instructions
• FADD
• FSUB
• FMUL
• FDIV
• FPREM
• FSCAL
• FSQRT
• FRNDINT
FADD – Performs addition of operand with top
of the stack. The result. The results are stored
in the destination operand controlled by the D
bit.
Operand can be any stack register or memory.

FADD ST, ST(2) ---- ST = ST + ST(2)


FADD ST(2),ST ---- ST(2)=ST+ST(2)
FSQRT
• Finds the square root of the top of the
stack and leaves the resultant square
root at the top of the stack.
– an invalid error occurs for the square
root of a negative number
• The IE bit of the status register should be
tested when an invalid result can occur
• FSCALE ---- This instruction multiplies the
content of the top of the stack (ST) by 2^n,
where n is the integer part of ST(1) and stores
the result in ST.

• FRNDINT – This instruction rounds the


content of ST(0) to its integer value. The
rounding is controlled by the RC field of the
control word.
Transcendental Instructions
• FPTAN
• F2XMI
• FLY2X
FPTAN – PTAN stands for Partial Tangent.
- Finds the partial tangent of Y/X = tan θ
- ST = X and ST(1) = Y
- The value of ‘theta’ should be stored at te
top of the stack and should be less than or
equal to pi/4.
FYL2X - computes Y * Log 2X

• X should be greater than or equal to 0.


• Y can be any value.
• X value must be in ST and Y in ST(1).
Comparison Instructions
• FCOM – The contents of top of stack is
compared with contents of memory or
contents of any other stack register.
Constant Returning Instructions
• FLDPI
Coprocessor Control Instructions

• FINIT – This instruction prepares the 8087 for


further execution. It performs the same
function as hardware reset. Control Word is
set to 3FFFh, Tag status is empty. Flags are
cleared and ST is initilized to 000.
Coprocessor Control Instructions – Continued..
.model small
.8087
.data
n1 dd 10.25
n2 dd 1.33
n3 dd 4499.5917
Program to find volume of a sphere
vol dd ?
.code
mov ax,@data
mov ds,ax
finit
fldpi
fmul n2
fmul n1
fmul n1
fmul n1
fst vol
mov ah,4ch
int 21h
end

Das könnte Ihnen auch gefallen