68
Assembly Language for x86 Assembly Language for x86 Processors Processors 7th Edition 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed. Slide show prepared by the author Revised by Zuoliu Ding at Fullerton College, 09/2014 Kip R. Irvine

Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Embed Size (px)

Citation preview

Page 1: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Assembly Language for x86 Processors Assembly Language for x86 Processors 7th Edition7th Edition

Chapter 12: Floating-Point Processing and Instruction Encoding

(c) Pearson Education, 2015. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.

Slide show prepared by the author

Revised by Zuoliu Ding at Fullerton College, 09/2014

Kip R. Irvine

Page 2: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 2

Chapter OverviewChapter Overview

• Floating-Point Binary Representation• Floating-Point Unit• x86 Instruction Encoding

Page 3: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 3

Floating-Point Binary RepresentationFloating-Point Binary Representation

• IEEE Floating-Point Binary Reals• The Exponent• Normalized Binary Floating-Point Numbers• Creating the IEEE Representation• Converting Decimal Fractions to Binary Reals

Page 4: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 4

IEEE Floating-Point Binary RealsIEEE Floating-Point Binary Reals

• Types• Single Precision

• 32 bits: 1 bit for the sign, 8 bits for the exponent, and 23 bits for the fractional part of the significand.

• Double Precision• 64 bits: 1 bit for the sign, 11 bits for the exponent, and

52 bits for the fractional part of the significand.

• Double Extended Precision• 80 bits: 1 bit for the sign, 15 bits for the exponent, and

64 bits for the fractional part of the significand. • http://en.wikipedia.org/wiki/Extended_precision

Page 5: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015, Edited by Zuoliu Ding 5

Single-Precision FormatSingle-Precision Format

Approximate normalized range: 2-126 to 2127. Also called a short real.

C/C++ data type: float Range: 10-37.8 to 1038.1 Significand: 7 digits: 8388608 = 223

Page 6: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 6

Components of a Single-Precision RealComponents of a Single-Precision Real

• Sign• 1 = negative, 0 = positive

• Significand• decimal digits to the left & right of decimal point

• weighted positional notation

• Example:123.154 = (1 x 102) + (2 x 101) + (3 x 100) + (1 x 10–1)

+ (5 x 10–2) + (4 x 10–3)

• Exponent• unsigned integer

• integer bias (127 for single precision)

Page 7: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 7

Decimal Fractions vs Binary Floating-PointDecimal Fractions vs Binary Floating-Point

1/223

Page 8: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 8

The ExponentThe Exponent• Sample Exponents represented in Binary• Add 127 to actual exponent to produce the biased

exponent

• Notice: No 11111111 and 00000000 (-128 and -127)

Page 9: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 9

Normalizing Binary Floating-Point NumbersNormalizing Binary Floating-Point Numbers

• Mantissa is normalized when a single 1 appears to the left of the binary point

• Unnormalized: shift binary point until exponent is zero• Examples

Page 10: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 10

Real-Number EncodingsReal-Number Encodings

• Normalized finite numbers• all the nonzero finite values that can be encoded in a

normalized real number between zero and infinity

• Positive and Negative Infinity• NaN (not a number)

• bit pattern that is not a valid FP value

• Two types:• quiet• Signaling

• IEEE Standard 754 Floating Point Numbers• http://steve.hollasch.net/cgindex/coding/ieeefloat.html

Page 11: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 11

Real-Number EncodingsReal-Number Encodings (cont) (cont)

• Specific encodings (single precision):

0x7fffffff, // 1.#QNAN0xff800000, // -1.#INF0xff800001, // -1.#QNAN(SNaN?)0x7f800000, // 1.#INF0x80000000 // -0

Page 12: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Specific Encoding CPP Example:Specific Encoding CPP Example:• Cast 32-bit long to float to generate specific encoding

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 12

unsigned long nan[]={0x7fffffff, // 1.#QNAN0xff800000, // -1.#INF0xff800001, // -1.#QNAN(SNaN?)0x7f800000, // 1.#INF0x80000000 // -0

};

float g, v = 30232.35465; for (int i=0; i<sizeof(nan)/sizeof(unsigned long); i++) {

g = *( float* )(nan+i);if ( g <= v ) printf( " %g <= %g \n", g, v);else if ( g > v) printf( " %g > %g \n", g, v);else printf( "Can't compare %g and %g\n", g, v );

}

Can't compare 1.#QNAN and 30232.4 -1.#INF <= 30232.4Can't compare -1.#QNAN and 30232.4 1.#INF > 30232.4 -0 <= 30232.4

Page 13: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 13

Examples (Single Precision)Examples (Single Precision)• Order: sign bit, exponent bits, and fractional (mantissa)

• Example: -1101.101 (13.75d)• Normalized to -1.101101 X 23

• Sign bit 1, exponent 3 +127 = 130 10000010• Fraction 10110100000000000000000• Encoded: 1 10000010 10110100000000000000000

Page 14: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Denormalized finite numbersDenormalized finite numbers

• FPU can’t shift the binary point to normalized position, given limitation posted by the exponent range

• Example: 1.0101111 X 2-129

• 1.010 1111 0000 0000 0000 1111 X 2-129

• 0.101 0111 1000 0000 0000 0111 X 2-128

• 0.010 1011 1100 0000 0000 0011 X 2-127

• 0.001 0101 1110 0000 0000 0001 X 2-126

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 14

Page 15: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 15

Converting Fractions to Binary RealsConverting Fractions to Binary Reals

• Express as a sum of fractions having denominators that are powers of 2

• Examples

Page 16: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 16

Converting to Binary RealsConverting to Binary Reals

• Using Binary Long Division: (see Text)• 0.5d = 5d/10d = 0101b / 1010b = 0.1b

• 0.2d = 2d/10d = 0010b / 1010b = 0.00110011… …

• Using Multiplication:• 0.3125d : 0.3125 X2 = 0.625 <1 0.0

0.625 X2 = 1.25 >=1 0.01

0.25 X2 = 0.5 <1 0.010

0.5 X2 = 1.0 >=1 0.0101b

• 0.2d: 0.2 X2 = 0.4 <1 0.0 0.4 X2 = 0.8 <1 0.00

0.8 X2 = 1.6 >=1 0.001 0.6 X2 = 1.2 >=1 0.0011

0.2 X2 = 0.4 <1 0.00110 … …

0.00110011… …

Page 17: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 17

Converting Single-Precision to DecimalConverting Single-Precision to Decimal

1. If the MSB is 1, the number is negative; otherwise, it is positive.2. The next 8 bits represent the exponent. Subtract binary

01111111 (decimal 127), producing the unbiased exponent. Convert the unbiased exponent to decimal.

3. The next 23 bits represent the significand. Notate a “1.”, followed by the significand bits. Trailing zeros can be ignored. Create a floating-point binary number, using the significand, the sign determined in step 1, and the exponent calculated in step 2.

4. Unnormalize the binary number produced in step 3. (Shift the binary point the number of places equal to the value of the exponent. Shift right if the exponent is positive, or left if the exponent is negative.)

5. From left to right, use weighted positional notation to form the decimal sum of the powers of 2 represented by the floating-point binary number.

Page 18: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 18

ExampleExample

Convert 0 10000010 0101100000000000000000 to Decimal

1. The number is positive.

2. The unbiased exponent is binary 00000011, or decimal 3.

3. Combining the sign, exponent, and significand, the binary number is +1.01011 X 23.

4. The unnormalized binary number is +1010.11.

5. The decimal value is +10 3/4, or +10.75.

Page 19: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 19

What's NextWhat's Next

• Floating-Point Binary Representation• Floating-Point Unit• x86 Instruction Encoding

Page 20: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 20

Floating Point UnitFloating Point Unit

• FPU Register Stack• Rounding• Floating-Point Exceptions• Floating-Point Instruction Set• Arithmetic Instructions• Comparing Floating-Point Values• Reading and Writing Floating-Point Values• Exception Synchronization• Mixed-Mode Arithmetic• Masking and Unmasking Exceptions

Page 21: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Review Postfix ExpressionReview Postfix Expression

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 21

Infix Postfix

a+b ab+

(a+b)/c ab+c/

(a+b)*(c-d) ab+cd-*

5*6-4 5 6 * 4 -

5 ST(0)

5 6 ST(1) ST(0)

5 6 * ST(0)

5 6 * 4 ST(1) ST(0)

5 6 * 4 - ST(0)

5

• Stack status in evaluating 5 6 * 4 - :

5

6

30

30

4

26

Page 22: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 22

FPU Register StackFPU Register Stack• Eight individually addressable 80-bit data registers named R0

through R7 • Three-bit field named TOP in the FPU status word identifies

the register number that is currently the top of stack.

• Reference: SIMPLY FPU at MASM Forum

Page 23: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

FPU Register, AdvancedFPU Register, Advanced

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007, Add by Zuoliu Ding. 23

• LOAD a value: turn the barrel clockwise by one notch and load the value in the top compartment. The first value loaded immediately after the initialized FPU goes into R7. (Barrel Compartment)

• Values only can be loaded to or popped from the TOP compartment

Rule #1: An register compartment MUST be free (empty) in order to load a value into it.Rule #2: The programmer must keep track of the relative location of the existing register values while other values may be loaded to or popped from the TOP register.

Page 24: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 24

Special-Purpose RegistersSpecial-Purpose Registers

• Opcode register: stores opcode of last noncontrol instruction executed

• Control register: controls precision and rounding method for calculations

• Status register: top-of-stack pointer, condition codes, exception warnings

• Tag register: indicates content type of each register in the register stack

• Last instruction pointer register: pointer to last non-control executed instruction

• Last data (operand) pointer register: points to data operand used by last executed instruction

Page 25: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Tag Word (0FFFFh at FINIT ), AdvancedTag Word (0FFFFh at FINIT ), Advanced

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Added by Zuoliu Ding 25

• Each pair of bits means that the FPU register : 00 = contains a valid non-zero value 01 = contains a value equal to 0 10 = contains a special value (NAN, infinity, or denormal) 11 = is empty

• If a valid non-zero value is first loaded, it goes into BC7:0011111111111111b (3FFFh)

• If a second value zero (0) is then loaded, goes into BC6:0001111111111111b (1FFFh)

Page 26: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 26

RoundingRounding• FPU attempts to round an infinitely accurate result

from a floating-point calculation• may be impossible because of storage limitations

• Example• suppose 3 fractional bits can be stored, and a

calculated value equals +1.0111.

• rounding up by adding .0001 produces 1.100

• rounding down by subtracting .0001 produces 1.011

Round Method Real Rounded

To nearest even 1.0111 1.100

Down to negative infinity 1.0111 1.011

Up to positive infinity 1.0111 1.100

Toward zero 1.0111 1.011

Real Rounded

-1.0111 -1.100

-1.0111 -1.100

-1.0111 -1.011

-1.0111 -1.011

Page 27: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Control Word (037Fh at FINIT), AdvancedControl Word (037Fh at FINIT), Advanced

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Added by Zuoliu Ding 27

• The RC field (bits 11 and 10) or Rounding Control: 00 = Round to nearest, or to even if equidistant (default) 01 = Round down (toward -infinity) 10 = Round up (toward +infinity) 11 = Truncate (toward 0)

• Bits 5-0 are the interrupt masks:PM (bit 5) or Precision Mask UM (bit 4) or Underflow Mask OM (bit 3) or Overflow Mask ZM (bit 2) or Zero divide Mask DM (bit 1) or Denormalized operand Mask IM (bit 0) or Invalid operation Mask

Page 28: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 28

Floating-Point ExceptionsFloating-Point Exceptions

• Six types of exception conditions• Invalid operation #I

• Divide by zero #Z

• Denormalized operand #D

• Numeric overflow #O

• Numeric underflow #U

• Inexact precision #P

• Each has a corresponding mask bit• if set when an exception occurs, the exception is handled

automatically by FPU• if clear when an exception occurs, a software exception

handler is invoked

Page 29: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Status Word (0000h at FINIT), AdvancedStatus Word (0000h at FINIT), Advanced

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Added by Zuoliu Ding 29

• TOP (bits 13-11): FPU keeps track of which BC at the TOP

• IR (bit 7): Interrupt Request, set (1) while an exception handled and reset (0) when the exception handling completed

• SF (bit6): Stack Fault exception is set to either load a value into a register which is not free (then C1=1) or pop a value from a register which is free (then C1=0). (Such SF also an invalid operation I =1)

• P,U,O,Z,D,I (bit 5 – 0) Invalid operation #I Divide by zero #Z Denormalized operand #D Numeric overflow #O Numeric underflow #U Inexact precision #P

Page 30: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 30

FPU Instruction SetFPU Instruction Set

• Instruction mnemonics begin with letter F• Second letter identifies data type of memory operand

• B = bcd

• I = integer

• no letter: floating point

• Examples• FLBD load binary coded decimal

• FISTP store integer and pop stack

• FMUL multiply floating-point operands

Page 31: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 31

FPU Instruction SetFPU Instruction Set

• Operands• zero, one, or two

• no immediate operands

• no general-purpose registers (EAX, EBX, ...)

• integers must be loaded from memory onto the stack and converted to floating-point before being used in calculations

• if an instruction has two operands, one must be a FPU register

Page 32: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 32

FP Instruction SetFP Instruction Set

• Data Types

Page 33: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 33

Load Floating-Point ValueLoad Floating-Point Value

• FLD• copies floating point operand from memory into the

top of the FPU stack, ST(0)

• Example

Page 34: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 34

Store Floating-Point ValueStore Floating-Point Value

• FST• copies floating point operand from the top of the FPU

stack into memory

• FSTP • pops the stack after copying

• Example:

fst dbl3 ; 10.1fst dbl4 ; 10.1

fstp dbl3 ; 10.1fstp dbl4 ; 234.56

Page 35: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 35

Arithmetic InstructionsArithmetic Instructions

• Same operand types as FLD and FST

Page 36: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 36

Floating-Point AddFloating-Point Add

• FADD• adds source to destination

• No-operand version pops the FPU stack after addition, but FADDP does

• Examples:

FADDFADD m32/64fpFADD ST(0), ST(i)FADD ST(i), ST(0)FADDP ST(i), ST(0)FIADD m16/32int

Page 37: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 37

Floating-Point SubtractFloating-Point Subtract

• FSUB• subtracts source from destination.

• No-operand version pops the FPU stack after subtracting

• Example:

Page 38: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 38

Floating-Point MultiplyFloating-Point Multiply

The no-operand versions of FMUL and FDIV pop the stack after multiplying or dividing.

• FMUL• Multiplies source by destination,

stores product in destination

• FMULP pops the stack

• FDIV• Divides destination by source,

stores quotient in destination

• FDIVP pops the stack

Page 39: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 39

Comparing FP ValuesComparing FP Values

• FCOM instruction• Operands:

Page 40: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

40

FCOMFCOM

• Condition codes set by FPU in the Status Word, similar to CPU status flags

SF ZF AF PF CF

7 6 4 0

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Added by Zuoliu Ding

Page 41: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 41

Branching after FCOMBranching after FCOM

• Required steps:1. Use the FNSTSW instruction to move the FPU status

word into AX.

2. Use the SAHF instruction to copy AH into the EFLAGS register.

3. Use JA, JB, etc to do the branching.

Fortunately, the FCOMI instruction does steps 1 and 2 for you.

fcomi ST(0), ST(1)

jnb Label1

Page 42: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 42

Comparing for EqualityComparing for Equality• Calculate the absolute value of the difference

between two floating-point values

.dataepsilon REAL8 1.0E-12 ; difference valueval2 REAL8 0.0 ; value to compareval3 REAL8 1.001E-13 ; considered equal to val2

.code; if( val2 == val3 ), display "Values are equal".

fld epsilonfld val2fsub val3fabsfcomi ST(0),ST(1)ja skipmWrite <"Values are equal",0dh,0ah>

skip:

What values in ST(0),ST(1)?

Page 43: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 43

Floating-Point I/OFloating-Point I/O

• Irvine32 library procedures• ReadFloat

• reads FP value from keyboard, pushes it on the FPU stack

• WriteFloat• writes value from ST(0) to the console window in

exponential format

• ShowFPUStack• displays contents of FPU stack

Page 44: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

44

FPU I/O Example: floatTest32.asmFPU I/O Example: floatTest32.asm

.datafirst REAL8 123.456second REAL8 10.0

.codefld firstfld second

mWrite “Enter a real number: "call ReadFloatmWrite “Enter a real number: "call ReadFloat

fmul ST(0),ST(1)mWrite "Their product is: "call WriteFloatcall ShowFPUStack

Enter a real number: 3.5Enter a real number: 4e1Their product is: +1.4000000E+002

------ FPU Stack ------ST(0): +1.4000000E+002ST(1): +3.5000000E+000ST(2): +1.0000000E+001ST(3): +1.2345600E+002

What are FPU Stack values?

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Added by Zuoliu Ding

Page 45: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 45

Exception SynchronizationException Synchronization

• Main CPU and FPU can execute instructions concurrently• if an unmasked exception occurs, the current FPU

instruction is interrupted and the FPU signals an exception• But the main CPU does not check for pending FPU

exceptions. It might use a memory value that the interrupted FPU instruction was supposed to set.

• Example:

.data

intVal DWORD 25

.code

fild intVal ; load integer into ST(0)

inc intVal ; increment the integer

Page 46: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 46

Exception SynchronizationException Synchronization

• (continued)

• For safety, insert a fwait instruction, which tells the CPU to wait for the FPU's exception handler to finish:

.data

intVal DWORD 25

.code

fild intVal ; load integer into ST(0)

fwait ; wait for pending exceptions

inc intVal ; increment the integer

Page 47: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 47

FPU Code ExampleFPU Code Example• Expression: valD = –valA + (valB * valC)..data

valA REAL8 1.5

valB REAL8 2.5

valC REAL8 3.0

valD REAL8 ? ; will be +6.0

.code

fld valA ; ST(0) = valA

fchs ; change sign of ST(0)

fld valB ; load valB into ST(0)

fmul valC ; ST(0) *= valC

fadd ; ST(0) += ST(1)

fstp valD ; store ST(0) to valD

Q: Is FPU Register Stack empty now?

Page 48: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 48

FPU Code Example 2FPU Code Example 2• Sum of an ArrayARRAY_SIZE = 20

.data

sngArray REAL8 ARRAY_SIZE DUP(1.5)

.code

finit

mov esi,0 ; array index

fldz ; push 0.0 on stack

mov ecx,ARRAY_SIZE

L1:

fld sngArray[esi]

fadd

add esi,TYPE REAL8

loop L1

Q: How many FPU Registers are used?

; add memory into ST(0); add ST(0), ST(1), pop

Page 49: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 49

Mixed-Mode ArithmeticMixed-Mode Arithmetic

• Combining integers and reals. • Integer arithmetic instructions such as ADD and MUL cannot

handle reals• FPU has instructions that promote integers to reals and load

the values onto the floating point stack.• Example: Z = N + X.dataN SDWORD 20X REAL8 3.5Z REAL8 ?.codefild N ; load integer into ST(0)fwait ; wait for exceptionsfadd X ; add mem to ST(0)fstp Z ; store ST(0) to mem

Page 50: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 50

Changing Rounding Mode: Changing Rounding Mode: Z = (int) (N + X)Z = (int) (N + X)

.dataN SDWORD 20X REAL8 3.5Z SDWORD ?ctrlWord WORD ?

fild Nfadd Xfist Z mov eax,Z ; 24

Why Z=24?

fstcw ctrlWord

or ctrlWord, 110000000000bfldcw ctrlWord

fild Nfadd Xfist Z

fstcw ctrlWordand ctrlWord, 001111111111bfldcw ctrlWord

mov eax,Z ; 23

Default RC: 00 nearest even

Let’s try RC: 11, Truncate

Page 51: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 51

Masking and Unmasking ExceptionsMasking and Unmasking Exceptions

• Exceptions are masked by default• Divide by zero just generates infinity, without halting the

program

• If you unmask an exception• processor executes an appropriate exception handler• Unmask the divide by zero exception by clearing bit 2:

.data

ctrlWord WORD ?

.code

fstcw ctrlWord ; get the control word

and ctrlWord,1111111111111011b ; unmask divide by zero

fldcw ctrlWord ; load it back into FPU

Page 52: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Exception Example: Divide by ZeroException Example: Divide by Zero

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 52

.datactrlWord WORD ?val1 DWORD 1val2 REAL8 0.0

.code fstcw ctrlWord ; get control word and ctrlWord,1111111111111011b ; unmask Divide by 0 fldcw ctrlWord ; load it back into FPU

fild val1 fdiv val2 ; divide by zero,

; if masked, ST0 = 1#INF, no exception fst val2 ; When this comes,

; exception handler is invoked

fstcw ctrlWord ; get control word or ctrlWord,100b ; mask Divide by 0 fldcw ctrlWord ; load it back into FPU

Page 53: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Look into All FPU Internals? AdvancedLook into All FPU Internals? Advanced

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 53

FPU_ENVIRON STRUCTcontrolWord WORD ?ALIGN DWORDstatusWord WORD ?ALIGN DWORDtagWord WORD ?ALIGN DWORDinstrPointerOffset DWORD ?instrPointerSelector DWORD ?operandPointerOffset DWORD ?operandPointerSelector WORD ?WORD ? ; not used

FPU_ENVIRON ENDS

.dataenvironment FPU_ENVIRON <>

fstenv environmentMov dx,environment.tagWordmov ax,environment.statusWord... ...

You will know more about it when try Programming Exercise 7 !

Page 54: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 54

What's NextWhat's Next

• Floating-Point Binary Representation• Floating-Point Unit• x86 Instruction Encoding

Page 55: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 55

x86 Instruction Encodingx86 Instruction Encoding

• x86 Instruction Format• Single-Byte Instructions• Move Immediate to Register• Register-Mode Instructions• x86 Processor Operand-Size Prefix• Memory-Mode Instructions

Page 56: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 56

x86 Instruction Formatx86 Instruction Format

• Fields• Instruction prefix byte (operand size)

• opcode

• Mod R/M byte (addressing mode & operands)

• scale index byte (for scaling array index)

• address displacement

• immediate data (constant)

• Only the opcode is required

Page 57: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 57

x86 Instruction Formatx86 Instruction Format

Page 58: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 58

Single-Byte InstructionsSingle-Byte Instructions

• Only the opcode is used• Zero operands

• Example: AAA

• One implied operand• Example: INC DX

Page 59: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 59

Move Immediate to RegisterMove Immediate to Register

• Op code, followed by immediate value• Example: move immediate to register• Encoding format: B8+rw dw

• (B8 = opcode, +rw is a register number, dw is the immediate operand)

• register number added to B8 to produce a new opcode

Page 60: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 60

Register-Mode InstructionsRegister-Mode Instructions

• Mod R/M byte contains a 3-bit register number for each register operand• bit encodings for register numbers:

• Example: MOV AX, BX

Page 61: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 61

x86 Operand Size Prefixx86 Operand Size Prefix

• Overrides default segment attribute (16-bit or 32-bit)• Special value recognized by processor: 66h• Intel ran out of opcodes for x86 processors

• needed backward compatibility with 8086

• On x86 system, prefix byte used when 16-bit operands are used

Page 62: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 62

x86 Operand Size Prefixx86 Operand Size Prefix

• Sample encoding for 16-bit target:

• Encoding for 32-bit target:

overrides default operand size

Page 63: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 63

Memory-Mode InstructionsMemory-Mode Instructions

• Wide variety of operand types (addressing modes)• 256 combinations of operands possible

• determined by Mod R/M byte

• Mod R/M encoding:• mod = addressing mode

• reg = register number

• r/m = register or memory indicator

Page 64: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 64

MOV Instruction ExamplesMOV Instruction Examples

• Selected formats for 8-bit and 16-bit MOV instructions:

Page 65: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 65

Sample MOV InstructionsSample MOV Instructions

Assume that myWord is located at offset 0102h.

Page 66: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 66

SummarySummary

• Binary floating point number contains a sign, significand, and exponent• single precision, double precision, extended precision

• Not all significands between 0 and 1 can be represented correctly• example: 0.2 creates a repeating bit sequence

• Special types• Normalized finite numbers

• Positive and negative infinity

• NaN (not a number)

Page 67: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 67

SummarySummary - 2 - 2

• Floating Point Unit (FPU) operates in parallel with CPU• register stack: top is ST(0)

• arithmetic with floating point operands

• conversion of integer operands

• floating point conversions

• intrinsic mathematical functions

• x86 Instruction set• complex instruction set, evolved over time

• backward compatibility with older processors

• encoding and decoding of instructions

Page 68: Assembly Language for x86 Processors 7th Edition Chapter 12: Floating-Point Processing and Instruction Encoding (c) Pearson Education, 2015. All rights

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. 68

The EndThe End