– 1 – 52011 ieee floating point ieee standard 754 established in 1985 as uniform standard for...

59
– 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic formats Supported by all major CPUs Driven by Numerical Concerns Nice standards for rounding, overflow, underflow Hard to make go fast Numerical analysts predominated over hardware types in defining standard

Upload: sharleen-davis

Post on 20-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

– 3 – Frac. Binary Number Examples ValueRepresentation 5-3/ / / Observations Divide by 2 by shifting right Multiply by 2 by shifting left Numbers of form … 2 just below 1.0 1/2 + 1/4 + 1/8 + … + 1/2 i + …  1.0 Use notation 1.0 – 

TRANSCRIPT

Page 1: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 1 – 52011

IEEE Floating Point

IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic

Before that, many idiosyncratic formats Supported by all major CPUs

Driven by Numerical Concerns Nice standards for rounding, overflow, underflow

Hard to make go fastNumerical analysts predominated over hardware types in defining standard

Page 2: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 2 – 52011

Fractional Binary Numbers

Representation Bits to right of “binary point” represent fractional powers of 2

Represents rational number:

bi bi–1 b2 b1 b0 b–1 b–2 b–3 b–j• • •• • • .124

2i–1

2i

• • •

• • •1/21/41/8

2–j

bk 2kk j

i

Page 3: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 3 – 52011

Frac. Binary Number ExamplesValue Representation

5-3/4 101.112

2-7/8 10.1112

63/64 0.1111112

Observations Divide by 2 by shifting right Multiply by 2 by shifting left Numbers of form 0.111111…2 just below 1.0

1/2 + 1/4 + 1/8 + … + 1/2i + … 1.0Use notation 1.0 –

Page 4: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 4 – 52011

Representable NumbersLimitation

Can only exactly represent numbers of the form x/2k

Other numbers have repeating bit representations

Value Representation1/3 0.0101010101[01]…2

1/5 0.001100110011[0011]…2

1/10 0.0001100110011[0011]…2

Page 5: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 5 – 52011

Numerical Form –1s M 2E

Sign bit s determines whether number is negative or positive

Significand M normally a fractional value in range [1.0,2.0).

Exponent E weights value by power of two

Encoding

MSB is sign bit exp field encodes E frac field encodes M

Floating Point Representation

s exp frac

Page 6: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 6 – 52011

Encoding

MSB is sign bit exp field encodes E frac field encodes M

Sizes Single precision: 8 exp bits, 23 frac bits

32 bits total Double precision: 11 exp bits, 52 frac bits

64 bits total Extended precision: 15 exp bits, 63 frac bits

Only found in Intel-compatible machinesStored in 80 bits

1 bit wasted

Floating Point Precisions

s exp frac

Page 7: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 7 – 52011

“Normalized” Numeric ValuesCondition

 exp 000…0 and exp 111…1

Exponent coded as biased value E = Exp – Bias

Exp : unsigned value denoted by exp Bias : Bias value

Single precision: 127 (Exp: 1…254, E: -126…127) Double precision: 1023 (Exp: 1…2046, E: -1022…1023) in general: Bias = 2e-1 - 1, where e is number of exponent bits

Significand coded with implied leading 1 M = 1.xxx…x2

 xxx…x: bits of fracMinimum when 000…0 (M = 1.0)Maximum when 111…1 (M = 2.0 – )Get extra leading bit for “free”

Page 8: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 8 – 52011

Normalized Encoding ExampleValue

Float F = 15213.0; 1521310 = 111011011011012 = 1.11011011011012 X 213

SignificandM = 1.11011011011012

frac = 110110110110100000000002

ExponentE = 13Bias = 127Exp = 140 = 100011002

Floating Point Representation (Class 02):

Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000

140: 100 0110 0

15213: 1110 1101 1011 01

Page 9: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 9 – 52011

Denormalized Values

Condition  exp = 000…0

Value Exponent value E = –Bias + 1 Significand value M = 0.xxx…x2

xxx…x: bits of frac

Cases exp = 000…0, frac = 000…0

Represents value 0Note that have distinct values +0 and –0

exp = 000…0, frac 000…0Numbers very close to 0.0Lose precision as get smaller“Gradual underflow”

Page 10: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 10 – 52011

Special Values

Condition  exp = 111…1

Cases exp = 111…1, frac = 000…0

Represents value(infinity)Operation that overflowsBoth positive and negativeE.g., 1.0/0.0 = 1.0/0.0 = +, 1.0/0.0 =

exp = 111…1, frac 000…0Not-a-Number (NaN)Represents case when no numeric value can be determined

E.g., sqrt(–1),

Page 11: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 11 – 52011

Tiny Floating Point Example

8-bit Floating Point Representation the sign bit is in the most significant bit. the next four bits are the exponent, with a bias of 7.

the last three bits are the frac Same General Form as IEEE Format

normalized, denormalized representation of 0, NaN, infinity

s exp frac02367

Page 12: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 12 – 52011

Values Related to the Exponent

Exp exp E 2E

0 0000 -6 1/64 (denorms)1 0001 -6 1/642 0010 -5 1/323 0011 -4 1/164 0100 -3 1/85 0101 -2 1/46 0110 -1 1/27 0111 0 18 1000 +1 29 1001 +2 410 1010 +3 811 1011 +4 1612 1100 +5 3213 1101 +6 6414 1110 +7 12815 1111 n/a (inf, NaN)

Page 13: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 13 – 52011

Dynamic Ranges exp frac E Value

0 0000 000 -6 00 0000 001 -6 1/8*1/64 = 1/5120 0000 010 -6 2/8*1/64 = 2/512…0 0000 110 -6 6/8*1/64 = 6/5120 0000 111 -6 7/8*1/64 = 7/5120 0001 000 -6 8/8*1/64 = 8/5120 0001 001 -6 9/8*1/64 = 9/512…0 0110 110 -1 14/8*1/2 = 14/160 0110 111 -1 15/8*1/2 = 15/160 0111 000 0 8/8*1 = 10 0111 001 0 9/8*1 = 9/80 0111 010 0 10/8*1 = 10/8…0 1110 110 7 14/8*128 = 2240 1110 111 7 15/8*128 = 2400 1111 000 n/a inf

closest to zero

largest denormsmallest norm

closest to 1 below

closest to 1 above

largest norm

Denormalizednumbers

Normalizednumbers

Page 14: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 14 – 52011

Floating Point Operations

Conceptual View First compute exact result Make it fit into desired precision

Possibly overflow if exponent too largePossibly round to fit into frac

Rounding Modes (illustrate with $ rounding)$1.40 $1.60 $1.50 $2.50 –$1.50

Zero $1 $1 $1 $2 –$1 Round down (-) $1 $1 $1 $2 –$2 Round up (+) $2 $2 $2 $3 –$1 Nearest Even (default) $1 $2 $2 $2 –$2

Note:1. Round down: rounded result is close to but no greater than true result.2. Round up: rounded result is close to but no less than true result.

Page 15: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 15 – 52011

Closer Look at Round-To-Even

Default Rounding Mode Hard to get any other kind without dropping into assembly

All others are statistically biasedSum of set of positive numbers will consistently be over- or under- estimated

Applying to Other Decimal Places / Bit Positions When exactly halfway between two possible values

Round so that least significant digit is even E.g., round to nearest hundredth

1.2349999 1.23 (Less than half way)1.2350001 1.24 (Greater than half way)1.2350000 1.24 (Half way—round up)1.2450000 1.24 (Half way—round down)

Page 16: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 16 – 52011

Rounding Binary Numbers

Binary Fractional Numbers “Even” when least significant bit is 0 Half way when bits to right of rounding position = 100…2

Examples Round to nearest 1/4 (2 bits right of binary point)Value Binary Rounded Action Rounded Value

2 3/32 10.000112 10.002 (<1/2—down) 22 3/16 10.001102 10.012 (>1/2—up) 2 1/42 7/8 10.111002 11.002 (1/2—up) 32 5/8 10.101002 10.102 (1/2—down) 2 1/2

Page 17: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 17 – 52011

FP MultiplicationOperands

(–1)s1 M1 2E1 * (–1)s2 M2 2E2

Exact Result(–1)s M 2E

Sign s: s1 ^ s2 Significand M: M1 * M2 Exponent E: E1 + E2

Fixing If M ≥ 2, shift M right, increment E If E out of range, overflow Round M to fit frac precision

Implementation Biggest chore is multiplying significands

Page 18: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 18 – 52011

FP AdditionOperands

(–1)s1 M1 2E1

(–1)s2 M2 2E2

Assume E1 > E2

Exact Result(–1)s M 2E

Sign s, significand M: Result of signed align & add

Exponent E: E1

Fixing If M ≥ 2, shift M right, increment E if M < 1, shift M left k positions, decrement E by k

Overflow if E out of range Round M to fit frac precision

(–1)s1 M1

(–1)s2 M2

E1–E2

+

(–1)s M

Page 19: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 19 – 52011

Floating Point in CC Guarantees Two Levels

float single precisiondouble double precision

Conversions Casting between int, float, and double changes numeric values

Double or float to int Truncates fractional part Like rounding toward zero Not defined when out of range

Generally saturates to TMin or TMax int to double

Exact conversion, as long as int has ≤ 53 bit word size int to float

Will round according to rounding mode

Page 20: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 20 – 52011

Assembly Programmer’s View

Programmer-Visible State EIP Program Counter

Address of next instruction Register File

Heavily used program data Condition Codes

Store status information about most recent arithmetic operation

Used for conditional branching

EIP

Registers

CPU Memory

Object CodeProgram Data

OS Data

Addresses

Data

Instructions

Stack

ConditionCodes

Memory Byte addressable array Code, user data, (some)

OS data Includes stack used to

support procedures

Page 21: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 21 – 52011

text

text

binary

binary

Compiler (gcc -S)

Assembler (gcc or as)

Linker (gcc or ld)

C program (p1.c p2.c)

Asm program (p1.s p2.s)

Object program (p1.o p2.o)

Executable program (p)

Static libraries

(.a)

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p

Use optimizations (-O)Put resulting binary in file p

Page 22: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 22 – 52011

Compiling Into AssemblyC Code

int sum(int x, int y){ int t = x+y; return t;}

Generated Assemblypushl %ebpmovl %esp,%ebpmovl 12(%ebp),%eaxaddl 8(%ebp),%eaxmovl %eax, -4(%ebp)

movl -4(%ebp),%eaxleaveret

Obtain with command

gcc -O -S code.c

Produces file code.s

Page 23: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 23 – 52011

Assembly CharacteristicsMinimal Data Types

“Integer” data of 1, 2, or 4 bytesData valuesAddresses (untyped pointers)

Floating point data of 4, 8, or 10 bytes No aggregate types such as arrays or structures

Just contiguously allocated bytes in memory

Primitive Operations Perform arithmetic function on register or memory data

Transfer data between memory and registerLoad data from memory into registerStore register data into memory

Transfer controlUnconditional jumps to/from proceduresConditional branches

Page 24: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 24 – 52011

Machine Instruction ExampleC Code

Add two signed integers

Assembly Add 2 4-byte integers

Same instruction whether signed or unsigned

Operands:x: Register %eaxy: Memory M[%ebp+8]t: Register %eax

Return function value in %eax

Object Code 3-byte instruction Stored at address 0x401046

int t = x+y;

addl 8(%ebp),%eax

0x401046: 03 45 08

Similar to expression x += y

Page 25: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 25 – 52011

Moving Data

Moving Datamovl Source,Dest: Move 4-byte (“long”) word Lots of these in typical code

Operand Types Immediate: Constant integer data

Like C constant, but prefixed with ‘$’E.g., $0x400, $-533Encoded with 1, 2, or 4 bytes

Register: One of 8 integer registersBut %esp and %ebp reserved for special useOthers have special uses for particular instructions

Memory: 4 consecutive bytes of memoryVarious “address modes”

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

Page 26: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 26 – 52011

movl Operand Combinations

Cannot do memory-memory transfers with single instruction

movl

Imm

Reg

Mem

RegMem

RegMem

Reg

Source Destination

movl $0x4,%eax

movl $-147,(%eax)

movl %eax,%edx

movl %eax,(%edx)

movl (%eax),%edx

C Analog

temp = 0x4;

*p = -147;

temp2 = temp1;

*p = temp;

temp = *p;

Page 27: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 27 – 52011

Simple Addressing Modes

Normal (R) Mem[Reg[R]] Register R specifies memory addressmovl (%ecx),%eax

Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offsetmovl 8(%ebp),%edx

Page 28: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 28 – 52011

Look at sum againint sum(int x, int y){ int t = x+y; return t;}

pushl %ebpmovl %esp,%ebp //handle stack movl 12(%ebp),%eax // %eax = yaddl 8(%ebp),%eax // %eax = y+xmovl %eax, -4(%ebp) // t = %eax

movl -4(%ebp),%eax // return tleaveret

Page 29: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 29 – 52011

Using Simple Addressing Modes

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

swap:pushl %ebpmovl %esp,%ebppushl %ebx

movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)

movl -4(%ebp),%ebxreturnret

Body

SetUp

Finish

Page 30: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 30 – 52011

Understanding Swap

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

Stack

RegisterVariable%ecx yp%edx xp%eax t1%ebx t0

ypxp

Rtn adrOld %ebp %ebp 0

4 8 12

Offset

•••

Old %ebx-4

Page 31: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 31 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

123456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104

Page 32: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 32 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

123456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x120

0x104

Page 33: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 33 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

123456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x124

0x120

0x104

Page 34: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 34 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

123456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

0x104

Page 35: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 35 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

123456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 36: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 36 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

456456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 37: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 37 – 52011

Understanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x1200x124Rtn adr

%ebp 0 4 8 12

Offset

-4

456123

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

ypxp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 38: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 38 – 52011

Indexed Addressing ModesMost General Form

D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant “displacement” 1, 2, or 4 bytes Rb: Base register: Any of 8 integer registers Ri: Index register: Any, except for %esp

Unlikely you’d use %ebp, either S: Scale: 1, 2, 4, or 8

Special Cases(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D](Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

Page 39: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 39 – 52011

Address Computation Examples

%edx

%ecx

0xf000

0x100

Expression Computation Address

0x8(%edx) 0xf000 + 0x8 0xf008

(%edx,%ecx) 0xf000 + 0x100 0xf100

(%edx,%ecx,4) 0xf000 + 4*0x100 0xf400

0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080

Page 40: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 40 – 52011

Address Computation Instruction

leal Src,Dest Src is address mode expression Set Dest to address denoted by expression

Uses Computing address without doing memory reference

E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y

k = 1, 2, 4, or 8.

Page 41: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 41 – 52011

Some Arithmetic Operations

Format ComputationTwo Operand Instructions

addl Src,Dest Dest = Dest + Srcsubl Src,Dest Dest = Dest - Srcimull Src,Dest Dest = Dest * Srcsall Src,Dest Dest = Dest << Src Also called shll

sarl Src,Dest Dest = Dest >> SrcArithmetic

shrl Src,Dest Dest = Dest >> Src Logicalxorl Src,Dest Dest = Dest ^ Srcandl Src,Dest Dest = Dest & Srcorl Src,Dest Dest = Dest | Src

Page 42: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 42 – 52011

Some Arithmetic Operations

Format ComputationOne Operand Instructions

incl Dest Dest = Dest + 1decl Dest Dest = Dest - 1negl Dest Dest = - Destnotl Dest Dest = ~ Dest

Page 43: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 43 – 52011

Using leal for Arithmetic Expressions

int arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

arith:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxmovl 12(%ebp),%edxleal (%edx,%eax),%ecxleal (%edx,%edx,2),%edxsall $4,%edxaddl 16(%ebp),%ecxleal 4(%edx,%eax),%eaximull %ecx,%eax

leaveret

Body

SetUp

Finish

Page 44: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 44 – 52011

Understanding arithint arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)

yx

Rtn adrOld %ebp %ebp 0

4 8 12

OffsetStack

•••

z16

Page 45: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 45 – 52011

Understanding arith

int arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

# eax = xmovl 8(%ebp),%eax

# edx = ymovl 12(%ebp),%edx

# ecx = x+y (t1)leal (%edx,%eax),%ecx

# edx = 3*yleal (%edx,%edx,2),%edx

# edx = 48*y (t4)sall $4,%edx

# ecx = z+t1 (t2)addl 16(%ebp),%ecx

# eax = 4+t4+x (t5)leal 4(%edx,%eax),%eax

# eax = t5*t2 (rval)imull %ecx,%eax

And now some live action!

Page 46: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 46 – 52011

Condition CodesSingle Bit Registers

CF Carry Flag SF Sign FlagZF Zero Flag OF Overflow Flag

Implicitly Set By Arithmetic Operationsaddl Src,DestC analog: t = a + b CF set if carry out from most significant bit

Used to detect unsigned overflow ZF set if t == 0 SF set if t < 0 OF set if two’s complement overflow

(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

Not Set by leal instruction

Page 47: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 47 – 52011

Setting Condition Codes (cont.)

Explicit Setting by Compare Instructioncmpl Src2,Src1 cmpl b,a like computing a-b without setting destination

CF set if carry out from most significant bitUsed for unsigned comparisons

ZF set if a == b SF set if (a-b) < 0 OF set if two’s complement overflow

(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

Page 48: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 48 – 52011

Setting Condition Codes (cont.)

Explicit Setting by Test instructiontestl Src2,Src1 Sets condition codes based on value of Src1 & Src2

Useful to have one of the operands be a mask testl b,a like computing a&b without setting destination

ZF set when a&b == 0 SF set when a&b < 0

Page 49: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 49 – 52011

Reading Condition Codes

SetX Condition Descriptionsete ZF Equal / Zerosetne ~ZF Not Equal / Not Zerosets SF Negativesetns ~SF Nonnegativesetg ~(SF^OF)&~ZF Greater (Signed)setge ~(SF^OF) Greater or Equal (Signed)setl (SF^OF) Less (Signed)setle (SF^OF)|ZF Less or Equal (Signed)seta ~CF&~ZF Above (unsigned)setb CF Below (unsigned)

SetX Instructions Set single byte based on combinations of condition codes

Page 50: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 50 – 52011

Reading Condition Codes (Cont.)SetX Instructions

Set single byte based on combinations of condition codes

One of 8 addressable byte registers

Embedded within first 4 integer registers

Does not alter remaining 3 bytes

Typically use movzbl to finish job

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

%al%ah

%dl%dh

%cl%ch

%bl%bh

int gt (int x, int y){ return x > y;}

movl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x : ysetg %al # al = x > ymovzbl %al,%eax # Zero rest of %eax

Note inverted ordering!

Body

Page 51: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 51 – 52011

Jumping

jX Condition Descriptionjmp 1 Unconditionalje ZF Equal / Zerojne ~ZF Not Equal / Not Zerojs SF Negativejns ~SF Nonnegativejg ~(SF^OF)&~ZF Greater (Signed)jge ~(SF^OF) Greater or Equal (Signed)jl (SF^OF) Less (Signed)jle (SF^OF)|ZF Less or Equal (Signed)ja ~CF&~ZF Above (unsigned)jb CF Below (unsigned)

jX Instructions Jump to different part of code depending on condition codes

Page 52: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 52 – 52011

Conditional Branch Example

int max(int x, int y){ if (x > y) return x; else return y;}

_max:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%edxmovl 12(%ebp),%eaxcmpl %eax,%edxjle L9movl %edx,%eax

L9:

leaveret

Body

SetUp

Finish

Page 53: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 53 – 52011

Conditional Branch Example (Cont.)

movl 8(%ebp),%edx # edx = xmovl 12(%ebp),%eax # eax = ycmpl %eax,%edx # x : yjle L9 # if <= goto L9movl %edx,%eax # eax = x

L9: # Done:

int goto_max(int x, int y){ int rval = y; int ok = (x <= y); if (ok) goto done; rval = x;done: return rval;}

Skipped when x y

C allows “goto” as means of transferring control

Closer to machine-level programming style

Generally considered bad coding style

Page 54: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 54 – 52011

C Codeint fact_do (int x){ int result = 1; do { result *= x; x = x-1; } while (x > 1); return result;}

Goto Versionint fact_goto(int x)

{ int result = 1;loop: result *= x; x = x-1; if (x > 1) goto loop; return result;}

“Do-While” Loop Example

Use backward branch to continue looping Only take branch when “while” condition holds

Page 55: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 55 – 52011

Goto Versionint fact_goto

(int x){ int result = 1;loop: result *= x; x = x-1; if (x > 1) goto loop; return result;}

“Do-While” Loop Compilation

Registers%edx x%eax result

_fact_goto:pushl %ebp # Setupmovl %esp,%ebp # Setupmovl $1,%eax # eax = 1movl 8(%ebp),%edx # edx = x

L11:imull %edx,%eax # result *= xdecl %edx # x--cmpl $1,%edx # Compare x : 1jg L11 # if > goto loop

movl %ebp,%esp # Finishpopl %ebp # Finishret # Finish

Assembly

Page 56: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 56 – 52011

C Codeint fact_while (int x){ int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;}

First Goto Versionint fact_while_goto

(int x){ int result = 1;loop: if (!(x > 1)) goto done; result *= x; x = x-1; goto loop;done: return result;}

“While” Loop Example #1

Is this code equivalent to the do-while version?

Must jump out of loop if test fails

Page 57: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 57 – 52011

C Codeint fact_while(int x){ int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;}

Second Goto Versionint fact_while_goto2

(int x){ int result = 1; if (!(x > 1)) goto done; loop: result *= x; x = x-1; if (x > 1) goto loop;done: return result;}

Actual “While” Loop Translation

Uses same inner loop as do-while version

Guards loop entry with extra test

Page 58: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 58 – 52011

C Codewhile (Test) Body

Do-While Version

if (!Test) goto done; do Body while(Test);done:

General “While” Translation

Goto Version if (!Test) goto done;loop: Body if (Test) goto loop;done:

Page 59: – 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating…

– 59 – 52011

Switch Statement Example

Setup:

movl 8(%ebp),%eax // eax = op movl %eax, -4(%ebp) // movl 8(%ebp), %eax // movl %eax, -8(%ebp) //

cmpl $2,-8(%ebp) # Compare op je .L4 cmpl $2, -8(%ebp) jg .L7 cmpl $1, -8(%ebp) je .L3 jmp .L6L7: cmpl $3 -8(%ebp) je .L5 jmp .L6L3: movl $3, -4(%ebp) jmp .L2L4: movl $5, -4(%ebp) jmp .L2L5: movl $9, -4(%ebp)L6: movl $7, -4(%ebp)L2: leave

int swich_ex(int a){ int b=a; switch(a){ case 1: b=3; break; case 2: b=5; break; case 3: b=9; default: b=7;}}

C code