ia32 floating point
DESCRIPTION
IA32 Floating Point. History 8086: first computer to implement IEEE FP separate 8087 FPU (floating point unit) 486: merged FPU and Integer Unit onto one chip Summary Hardware to add, multiply, and divide Floating point data registers Various control & status registers - PowerPoint PPT PresentationTRANSCRIPT
– 1 – CSCI-2500
IA32 Floating PointIA32 Floating Point
HistoryHistory 8086: first computer to implement IEEE FP
separate 8087 FPU (floating point unit)
486: merged FPU and Integer Unit onto one chip
SummarySummary Hardware to add, multiply, and divide Floating point data registers Various control & status registers
Floating Point FormatsFloating Point Formats single precision (C float): 32 bits double precision (C double): 64 bits extended precision (C long double): 80 bits
Instructiondecoder andsequencer
FPUInteger
Unit
Memory
– 2 – CSCI-2500
FPU Data Register StackFPU Data Register Stack
FPU register format (extended precision)FPU register format (extended precision)
s exp frac
063647879
FPU registersFPU registers 8 registers Logically forms shallow
stack Top called %st(0) When push too many,
bottom values disappear
stack grows down
“Top” %st(0)
%st(1)
%st(2)
%st(3)
– 3 – CSCI-2500
Floating Point RegistersFloating Point Registers
ST: Stack top, called ST(0)ST: Stack top, called ST(0)
ST(1), the register just below the stack top.ST(1), the register just below the stack top.
ST(2), the register below ST(1)ST(2), the register below ST(1)
ST(3), ST(4), ST(5), ST(6) ST(3), ST(4), ST(5), ST(6)
ST(7) the last register at the bottom of the stackST(7) the last register at the bottom of the stack
The FPU stack is virtual since values can be The FPU stack is virtual since values can be removed not only from the top of the stack ST(0)removed not only from the top of the stack ST(0)
– 4 – CSCI-2500
Floating Point Load InstructionsFloating Point Load Instructions
Fld Fld memory (real)memory (real)
FildFild memory (integer)memory (integer)
Flb Flb memory (BCD)memory (BCD)
Fld1Fld1 (none)(none)
Fldz Fldz (none)(none)
FldpiFldpi (none)(none)
Fldl2e(none)Fldl2e(none)
Fldl2tFldl2t (none)(none)
Fldlg2 (none) Fldlg2 (none)
Fldln2 (none)Fldln2 (none)
Real value pushed into stackReal value pushed into stack
Integer value converted to floatInteger value converted to float
BCD/float pushed into the stackBCD/float pushed into the stack
1.0 pushed into the stack1.0 pushed into the stack
0.0 pushed into the stack0.0 pushed into the stack
Pi pushed into the stackPi pushed into the stack
Log 2(e) pushed into the stackLog 2(e) pushed into the stack
Log 2(10) pushed into the stackLog 2(10) pushed into the stack
Log 10(2) pushed into the stackLog 10(2) pushed into the stack
Log (2) pushed into the stackLog (2) pushed into the stack
– 5 – CSCI-2500
Floating Point InstructionsFloating Point Instructions
Most floating point instructions operate over the Most floating point instructions operate over the stack top ST(0) and one other operand in another stack top ST(0) and one other operand in another floating point register or in memory.floating point register or in memory.
No floating point instruction can transfer data No floating point instruction can transfer data between an 80x86 register like EAX and a floating between an 80x86 register like EAX and a floating point register.point register.
Transfers must be made using memory locations for Transfers must be made using memory locations for intermediate storage.intermediate storage.
– 6 – CSCI-2500
Floating Point Load ExamplesFloating Point Load Examples
1.00 ST2.00 ST(1)3.00 ST(2)
ST(3)ST(4)ST(5)ST(6)ST(7)
Consider the following table for the examples
fpValue Real4 10.0
intValue Dword 20
bcdValue TBYTE 30
– 7 – CSCI-2500
Floating Point Load ExamplesFloating Point Load Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
Fld fpValueFld fpValue
10.00 ST1.00 ST(1)2.00 ST(2)3.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
– 8 – CSCI-2500
Floating Point Load ExamplesFloating Point Load Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
Fld st(2)Fld st(2)
Notice that the value stored in Notice that the value stored in register ST(2) was pushed register ST(2) was pushed again to the top of the stack but again to the top of the stack but was not removed from its was not removed from its current position.current position.
2.00 ST10.00 ST(1)1.00 ST(2)2.00 ST(3)3.00 ST(4)
ST(5)ST(6)ST(7)
– 9 – CSCI-2500
Floating Point Load InstructionsFloating Point Load Instructions
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
Fild intValueFild intValue
The integer value is The integer value is converted to a 10 byte floating converted to a 10 byte floating point value and pushed into the point value and pushed into the stack.stack.
20.00 ST2.00 ST(1)
10.00 ST(2)1.00 ST(3)2.00 ST(4)3.00 ST(5)
ST(6)ST(7)
– 10 – CSCI-2500
Floating Point Load ExamplesFloating Point Load Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
Fbld bcdValueFbld bcdValue
The bcd value is also The bcd value is also converted into a 10 byte converted into a 10 byte floating point value.floating point value.
30.00 ST20.00 ST(1)2.00 ST(2)
10.00 ST(3)1.00 ST(4)2.00 ST(5)3.00 ST(6)
ST(7)
– 11 – CSCI-2500
Floating Point Load ExamplesFloating Point Load Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
FldzFldz
Finit ; initialize the math Finit ; initialize the math processorprocessor
Finit is desirable near the Finit is desirable near the beginning of the code since it beginning of the code since it erases all the FPU registers.erases all the FPU registers.
0.00 ST30.00 ST(1)20.00 ST(2)10.00 ST(3)2.00 ST(4)1.00 ST(5)2.00 ST(6)3.00 ST(7)
– 12 – CSCI-2500
Floating Point Store InstructionsFloating Point Store Instructions
FstFst st(num)st(num)
Fstp Fstp st(num)st(num)
FstFst memory(real)memory(real)
FstpFstp memory(real)memory(real)
FistFist memory(integer)memory(integer)
FistpFistp memory(integer)memory(integer)
FbstpFbstp memory (BCD)memory (BCD)
Replaces st(num) by a copy of st, only st Replaces st(num) by a copy of st, only st num is affected.num is affected.
Replaces st(num) by a copy of st, st is Replaces st(num) by a copy of st, st is popped from the stack.popped from the stack.
Copy of ST is stored as a real value in Copy of ST is stored as a real value in mem. Stack not affected.mem. Stack not affected.
Copy of st is stores in mem. ST popped Copy of st is stores in mem. ST popped from the stack.from the stack.
Copy of ST is converted into integer and Copy of ST is converted into integer and sotred in mem.sotred in mem.
Same but ST popped.Same but ST popped.
Copy of ST converted into BCD and stored Copy of ST converted into BCD and stored in memory, st popped from stack.in memory, st popped from stack.
– 13 – CSCI-2500
Floating Point Store ExamplesFloating Point Store Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
The stack is initialized as The stack is initialized as shown.shown.
intValue Dword ?intValue Dword ?
10.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
– 14 – CSCI-2500
Floating Point Store ExamplesFloating Point Store Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
Fist intValueFist intValue
intValue obtains 0000000AintValue obtains 0000000A
10.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
– 15 – CSCI-2500
Floating Point Store ExamplesFloating Point Store Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
Fistp intValueFistp intValue
intValue 000000AintValue 000000A
20.00 ST30.00 ST(1)40.00 ST(2)
ST(3)ST(4)ST(5)ST(6)ST(7)
– 16 – CSCI-2500
Floating Point Store ExamplesFloating Point Store Examples
Assuming the FPU stack is as Assuming the FPU stack is as shown before, lets analyze the shown before, lets analyze the following commands.following commands.
The situation is more confusing The situation is more confusing when the destination is another when the destination is another floating point register.floating point register.
Fst st(2)Fst st(2)
Fstp st(2)Fstp st(2)
The first command copies the The first command copies the content of the ST into St(2)content of the ST into St(2)
The second does the same and The second does the same and then pops ST from the stack.then pops ST from the stack.
1.00 ST2.00 ST(1)3.00 ST(2)4.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
1.00 ST2.00 ST(1)1.00 ST(2)4.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
2.00 ST1.00 ST(1)4.00 ST(2)
ST(3)ST(4)ST(5)ST(6)ST(7)
Original state
Fst st(2)
Fstp st(2)
– 17 – CSCI-2500
Exchange InstructionsExchange Instructions
In addition to the load and store instructions, the In addition to the load and store instructions, the floating point unit has the fxch instruction that will floating point unit has the fxch instruction that will exchange the contents of two floating point registers exchange the contents of two floating point registers with no needed operand. with no needed operand.
Another version of the instruction lets you specify Another version of the instruction lets you specify which register you want to change the contents with which register you want to change the contents with the stack top.the stack top.
– 18 – CSCI-2500
Exchange InstructionExchange Instruction
InstructionInstruction
Fxch ; exchanges ST and ST(1) Fxch st(num) ; exchanges ST and ST(num);
Examples:Finit;Fld1;Fldz;Fxchg;
What are the contents of ST and ST(1)?
– 19 – CSCI-2500
Floating Point Addition InstructionsFloating Point Addition Instructions
The floating point unit contains special instructions The floating point unit contains special instructions for addition purposes.for addition purposes.
There are versions of theses instructions to add the There are versions of theses instructions to add the contents of ST to another register, the contents of contents of ST to another register, the contents of another register to ST, a real number from memory another register to ST, a real number from memory or even an integer number. There are no special or even an integer number. There are no special instructions for BCD numbers.instructions for BCD numbers.
The instructions that have a “p” at the end remove The instructions that have a “p” at the end remove the stack top from the floating point registers.the stack top from the floating point registers.
– 20 – CSCI-2500
Floating Point Addition InstructionsFloating Point Addition Instructions
Fadd (none)Fadd (none) ;pops both ST and ST(1), adds these values and ;pops both ST and ST(1), adds these values and pushes the sum onto the stack toppushes the sum onto the stack top
Fadd st(num), stFadd st(num), st ; adds ST(num) and ST; replaces ST(num) by the sum. ; adds ST(num) and ST; replaces ST(num) by the sum.
Fadd st, st(num)Fadd st, st(num) ; adds ST and ST(num), replaces ST by the sum. ; adds ST and ST(num), replaces ST by the sum.
Fadd memory(real)Fadd memory(real) ; adds ST and real number from memory, replaces ; adds ST and real number from memory, replaces ST by the sum.ST by the sum.
Fiadd memory (integer)Fiadd memory (integer) ; adds ST and integer from memory, replaces ; adds ST and integer from memory, replaces ST by the sum.ST by the sum.
Faddp st(num), stFaddp st(num), st ; adds ST(num) and ST; replaces ST(num) by the ; adds ST(num) and ST; replaces ST(num) by the sum and pops ST from the stack.sum and pops ST from the stack.
– 21 – CSCI-2500
Floating Point Addition ExamplesFloating Point Addition Examples
10.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
Assume we have the stack:
50.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
After executing:
Fadd st, st(3)
– 22 – CSCI-2500
Floating Point Addition ExamplesFloating Point Addition Examples
Using the results from the previous example, Using the results from the previous example, assume with run the follogin instructions:assume with run the follogin instructions:
FpValue Real4 5.0 intValue Dword 1
Fadd fpValueFiadd intValue
What is the result of the floating point stack registers?
– 23 – CSCI-2500
Floating Point Addition ExamplesFloating Point Addition Examples
50.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
Original Stack After instructions
56.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
– 24 – CSCI-2500
Floating Point Addition ExamplesFloating Point Addition Examples
56.00 ST20.00 ST(1)30.00 ST(2)40.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
Finally after the instruction:
Faddp st(2), st
20.00 ST86.00 ST(1)40.00 ST(2)
ST(3)ST(4)ST(5)ST(6)ST(7)
– 25 – CSCI-2500
Floating Point Substraction InstructionsFloating Point Substraction Instructions
Substraction instructions are very similary to the Substraction instructions are very similary to the corresponding addition instructions. We have 12 of corresponding addition instructions. We have 12 of them, divided into two different groups.them, divided into two different groups.
The first six are equivalent to the addition The first six are equivalent to the addition instructions.instructions.
The second six, are the same except that the The second six, are the same except that the operands are the opposite, since substraction is not operands are the opposite, since substraction is not commutative.commutative.
– 26 – CSCI-2500
Floating Point Substraction InstructionsFloating Point Substraction Instructions
First 6 Substraction instructionsFirst 6 Substraction instructions
Fsub (none)Fsub (none) ; pops ST and ST(1), calculates ST(1)-ST; and pushes the ; pops ST and ST(1), calculates ST(1)-ST; and pushes the difference into the stack difference into the stack
Fsub st(num), stFsub st(num), st ; calculates ST(num)-st, replaces ST(num) by the diff. ; calculates ST(num)-st, replaces ST(num) by the diff.
Fsub st, st(num)Fsub st, st(num) ; calculates ST-ST(num), replaces ST by the diff. ; calculates ST-ST(num), replaces ST by the diff.
Fsub memory(real)Fsub memory(real) ; calculates ST- real and replaces ST by the diff. ; calculates ST- real and replaces ST by the diff.
Fsub memory (int)Fsub memory (int) ; calculates ST- int and replaces ST by the diff. ; calculates ST- int and replaces ST by the diff.
Fsubp st(num), stFsubp st(num), st ; calculates ST(num)-st, replaces st(num) by the diff ; calculates ST(num)-st, replaces st(num) by the diff and pops st from the stack.and pops st from the stack.
– 27 – CSCI-2500
Floating Point Substraction InstructionsFloating Point Substraction Instructions
Substraction instructions “opposite”Substraction instructions “opposite”
Fsubr (none)Fsubr (none) ; pops ST and ST(1), calculates ST-ST(1), pushes ; pops ST and ST(1), calculates ST-ST(1), pushes difference onto the stack.difference onto the stack.
Fsubr st(num), stFsubr st(num), st ; calculates st-st(num), replaces st(num) with the diff. ; calculates st-st(num), replaces st(num) with the diff.
Fsubr st, st(num)Fsubr st, st(num) ; calculates st(num)-st, replaces st with the diff. ; calculates st(num)-st, replaces st with the diff.
Fsubr memory(real)Fsubr memory(real) ; calculates mem-st, replaces st with the diff. ; calculates mem-st, replaces st with the diff.
Fsub rmemory(int)Fsub rmemory(int) ; calculates mem-st, replaces st with the diff. ; calculates mem-st, replaces st with the diff.
Fisubr mem(int)Fisubr mem(int) ; calculates mem-st, replaces st with the diff. ; calculates mem-st, replaces st with the diff.
Fsubpr st(num), stFsubpr st(num), st ; calculates st-st(num), replaces st(num) with the ; calculates st-st(num), replaces st(num) with the difference and pops st from the stack.difference and pops st from the stack.
– 28 – CSCI-2500
Floating Point Substraction ExamplesFloating Point Substraction Examples
Assuming a source stack:
15.00 ST25.00 ST(1)35.00 ST(2)45.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
55.00
After fsub st, st(3)
-30.00 ST25.00 ST(1)35.00 ST(2)45.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
55.00
After fsub st(3), st
15.0 ST25.00 ST(1)35.00 ST(2)30.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
55.00
– 29 – CSCI-2500
Floating Point Multiplication and DivFloating Point Multiplication and Div
Multiplicating instructions have the same format as Multiplicating instructions have the same format as addition instructions.addition instructions.
Division instructions have the same format as Division instructions have the same format as substraction instructions and also contain the suffix substraction instructions and also contain the suffix “R” for the reverse operands, in this case the roles “R” for the reverse operands, in this case the roles of dividend and divisor.of dividend and divisor.
– 30 – CSCI-2500
Floating Point Multiplication and DivFloating Point Multiplication and Div
Fmul (none)Fmul (none) ; pops ST and ST(1), multiplies them and pushes to stack. ; pops ST and ST(1), multiplies them and pushes to stack.
Fmul st(num), stFmul st(num), st ; multiplies ST(num) by ST, replaces ST(num) with the ; multiplies ST(num) by ST, replaces ST(num) with the product.product.
Fmul st, st(num)Fmul st, st(num) ; multiplies ST by ST(num), replaces ST with the ; multiplies ST by ST(num), replaces ST with the product.product.
Fmul memory(real)Fmul memory(real) ; multiplies ST by a real number in memory, ; multiplies ST by a real number in memory, replaces ST with the product.replaces ST with the product.
Fimul memory(int)Fimul memory(int) ; multiplies ST and an integer in memory, replaces ; multiplies ST and an integer in memory, replaces ST by the product.ST by the product.
Fmulp st(num), stFmulp st(num), st ; multiplies ST(num) and ST, replaces ST(num) bu ; multiplies ST(num) and ST, replaces ST(num) bu the product, pops ST from the stack.the product, pops ST from the stack.
– 31 – CSCI-2500
Floating Point Multiplication and DivFloating Point Multiplication and Div Fdiv (none)Fdiv (none) ; pop ST and ST(1), calculates ST(1)/ST pushes quotient ; pop ST and ST(1), calculates ST(1)/ST pushes quotient
onto th estackonto th estack
Fdiv st(num), stFdiv st(num), st ; calculates ST(num)/ST, replaces ST(num) by quoit. ; calculates ST(num)/ST, replaces ST(num) by quoit.
Fdiv st, st(num)Fdiv st, st(num) ; calculates ST/ST(num), replaces ST by quot. ; calculates ST/ST(num), replaces ST by quot.
Fdiv mem(real)Fdiv mem(real) ; calculates ST / mem, replaces ST by quotient. ; calculates ST / mem, replaces ST by quotient.
Fdiv mem (int)Fdiv mem (int) ; calculates ST / mem, replaces ST by quotient. ; calculates ST / mem, replaces ST by quotient.
Fdivp st(num), stFdivp st(num), st ; calculates ST(num)/ST , replaces ST(num) by the ; calculates ST(num)/ST , replaces ST(num) by the quotient and pops ST from the stack.quotient and pops ST from the stack.
Fdivr (none)Fdivr (none) ; pops ST and ST(1), caculates ST/ST(1) and pushes ; pops ST and ST(1), caculates ST/ST(1) and pushes quotient onto the stack.quotient onto the stack.
Fdivr st(num), stFdivr st(num), st ; calculates ST/ST(num), replaces ST(num) by quot. ; calculates ST/ST(num), replaces ST(num) by quot.
Fdivr st,st(num)Fdivr st,st(num) ; calculates ST(num)/st, replaces ST by quot. ; calculates ST(num)/st, replaces ST by quot.
Fdivr mem(real, int)Fdivr mem(real, int) ; calc, mem/ST, replaces ST by the quotient. ; calc, mem/ST, replaces ST by the quotient.
Fdivpr st(num), stFdivpr st(num), st ; calc ST/ST(num), rep st(num) and pops st fromst. ; calc ST/ST(num), rep st(num) and pops st fromst.
– 32 – CSCI-2500
Floating Point Extra InstructionsFloating Point Extra Instructions
Some other instructions pertaining to the floating Some other instructions pertaining to the floating point unit are listed below:point unit are listed below:
Fabs (none) ; st= |st| (absolute value) Fchs (none) ; st = -st (change sign) Frndint (none) ; rounds st to an integer value Fsqrt (none) ; replaces the contents of ST by its square root
– 33 – CSCI-2500
Floating Point Compare InstructionsFloating Point Compare Instructions The floating point unit provides a collection of instructions to compare The floating point unit provides a collection of instructions to compare
the stack top ST to a second operand. It has a 16 bit status word the stack top ST to a second operand. It has a 16 bit status word (control register) which stores the results of this comparisons.(control register) which stores the results of this comparisons. Fcom (none) ; compares ST and ST(1) Fcom st(num) ; compares ST and ST(num) Fcom memory(real) ; compares ST and a real in memory Ficom memory(int); compares ST and an int in memory Ftst (none) ; compares ST and 0.0 Fcomp (none) ; compares ST and ST(1), pops stack. Fcomp st(num) ; compares ST and ST(num), pops stack. Fcomp memory(real) ; compares ST and a real in memory,
pops stack. Ficomp memory(int); compares ST and an int in memory,
pops stack. Fcompp (none) ; compares ST and ST(1), pops stack twice.
– 34 – CSCI-2500
Floating Point Compare InstructionsFloating Point Compare Instructions
The comparison instructions The comparison instructions assign values to the bits 14, 10 and assign values to the bits 14, 10 and 8 of the status word, these 8 of the status word, these “condition codes” bits are named “condition codes” bits are named C3, C2 and C0 respectively and the C3, C2 and C0 respectively and the flags are set as follows:flags are set as follows:
If the operands are not If the operands are not comparable (IEEE NAN) all bits are comparable (IEEE NAN) all bits are set to 1.set to 1.
Result of Result of comparisoncomparison
C3C3 C2C2 C0C0
ST > second ST > second operandoperand
00 00 00
ST < second ST < second operandoperand
00 00 11
ST = second ST = second operandoperand
11 00 00
– 35 – CSCI-2500
Floating Point Compare InstructionsFloating Point Compare Instructions
Status word “conditional codes” cannot be access directly by the Status word “conditional codes” cannot be access directly by the conditional jump instructions of the 80x86 processor. Special conditional jump instructions of the 80x86 processor. Special instructions must be used to copy these values to memory locations instructions must be used to copy these values to memory locations and to the AX register, they are as follows:and to the AX register, they are as follows:
Fstsw memory word ; copies status register to a memory word. Fstsw AX ; copies status register to AX. Fstcw memory word ; copies control word to memory word. Fldcw memory word ; copies memory word to control word.
Under certain circumstances a cpu might perform more than one FP Under certain circumstances a cpu might perform more than one FP operation per clock cycle.operation per clock cycle.
– 36 – CSCI-2500
Floating Point ExercisesFloating Point Exercises
9.00 ST12.00 ST(1)23.00 ST(2)24.00 ST(3)
ST(4)ST(5)ST(6)ST(7)
35.00
Given the following stack
Solve the following Exercises:
•Fld st(2)
•Fld fpValue (value = 10.0)
•Fild intValue (int = 1)
•Fldpi
•Fst st(4)
•Fstp st(4)
•Fxch st(3)
•Fadd
•Fadd st(3), st
•Fadd st, st(3)
•Faddp st(3), st
•Fsub fpValue (value = 2.00)
•Fisub intValue (int = 1)
•fsqrt
Fmul st(3), st
Fmul st, st(4)
Fmul
Fdivr
Fchs