Presentation is loading. Please wait.

Presentation is loading. Please wait.

– 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic.

Similar presentations


Presentation on theme: "– 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic."— Presentation transcript:

1 – 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

2 – 2 – 52011 Fractional Binary Numbers Representation Bits to right of “binary point” represent fractional powers of 2 Represents rational number: bibi b i–1 b2b2 b1b1 b0b0 b –1 b –2 b –3 b–jb–j. 1 2 4 2 i–1 2i2i 1/2 1/4 1/8 2–j2–j

3 – 3 – 52011 Frac. Binary Number Examples ValueRepresentation 5-3/4 101.11 2 2-7/8 10.111 2 63/64 0.111111 2 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/2 i + …  1.0 Use notation 1.0 – 

4 – 4 – 52011 Representable Numbers Limitation Can only exactly represent numbers of the form x/2 k Other numbers have repeating bit representations ValueRepresentation 1/3 0.0101010101[01]… 2 1/5 0.001100110011[0011]… 2 1/10 0.0001100110011[0011]… 2

5 – 5 – 52011 Numerical Form – 1 s M 2 E 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 sexpfrac

6 – 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 machines Stored in 80 bits  1 bit wasted Floating Point Precisions sexpfrac

7 – 7 – 52011 “Normalized” Numeric Values Condition 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 = 2 e-1 - 1, where e is number of exponent bits Significand coded with implied leading 1 M = 1.xxx … x 2 xxx … x : bits of frac Minimum when 000 … 0 (M = 1.0) Maximum when 111 … 1 (M = 2.0 –  ) Get extra leading bit for “free”

8 – 8 – 52011 Normalized Encoding Example Value Float F = 15213.0; 15213 10 = 11101101101101 2 = 1.1101101101101 2 X 2 13 Significand M = 1.1101101101101 2 frac= 11011011011010000000000 2 Exponent E = 13 Bias = 127 Exp = 140 = 10001100 2 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

9 – 9 – 52011 Denormalized Values Condition exp = 000 … 0 Value Exponent value E = –Bias + 1 Significand value M = 0.xxx … x 2 xxx … x : bits of frac Cases exp = 000 … 0, frac = 000 … 0 Represents value 0 Note that have distinct values +0 and –0 exp = 000 … 0, frac  000 … 0 Numbers very close to 0.0 Lose precision as get smaller “Gradual underflow”

10 – 10 – 52011 Special Values Condition exp = 111 … 1 Cases exp = 111 … 1, frac = 000 … 0 Represents value   (infinity) Operation that overflows Both positive and negative E.g., 1.0/0.0 =  1.0/  0.0 = + , 1.0/  0.0 =   exp = 111 … 1, frac  000 … 0 Not-a-Number (NaN) Represents case when no numeric value can be determined E.g., sqrt(–1), 

11 – 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 expfrac 0 2367

12 – 12 – 52011 Values Related to the Exponent ExpexpE2 E 00000-6 1/64(denorms) 10001-61/64 20010-51/32 30011-41/16 40100-31/8 50101-21/4 60110-11/2 70111 01 81000+12 91001+24 101010+38 111011+416 121100+532 131101+664 141110+7128 151111n/a(inf, NaN)

13 – 13 – 52011 Dynamic Range s exp frac EValue 0 0000 000-60 0 0000 001-61/8*1/64 = 1/512 0 0000 010-62/8*1/64 = 2/512 … 0 0000 110-66/8*1/64 = 6/512 0 0000 111-67/8*1/64 = 7/512 0 0001000-68/8*1/64 = 8/512 0 0001 001 -69/8*1/64 = 9/512 … 0 0110 110-114/8*1/2 = 14/16 0 0110 111-115/8*1/2 = 15/16 0 0111 00008/8*1 = 1 0 0111 00109/8*1 = 9/8 0 0111 010010/8*1 = 10/8 … 0 1110110714/8*128 = 224 0 1110 111715/8*128 = 240 0 1111 000n/ainf closest to zero largest denorm smallest norm closest to 1 below closest to 1 above largest norm Denormalized numbers Normalized numbers

14 – 14 – 52011 Floating Point Operations Conceptual View First compute exact result Make it fit into desired precision Possibly overflow if exponent too large Possibly 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.

15 – 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 biased Sum 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.23499991.23(Less than half way) 1.23500011.24(Greater than half way) 1.23500001.24(Half way—round up) 1.24500001.24(Half way—round down)

16 – 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) ValueBinaryRoundedActionRounded Value 2 3/32 10.00011 2 10.00 2 (<1/2—down)2 2 3/16 10.00110 2 10.01 2 (>1/2—up)2 1/4 2 7/8 10.11100 2 11.00 2 (1/2—up)3 2 5/8 10.10100 2 10.10 2 (1/2—down)2 1/2

17 – 17 – 52011 FP Multiplication Operands (– 1) s1 M1 2 E1 * (– 1) s2 M2 2 E2 Exact Result (– 1) s M 2 E 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

18 – 18 – 52011 FP Addition Operands (– 1) s1 M1 2 E1 (– 1) s2 M2 2 E2 Assume E1 > E2 Exact Result (– 1) s M 2 E 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

19 – 19 – 52011 Floating Point in C C Guarantees Two Levels float single precision double 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

20 – 20 – 52011 Assembly Programmer’s View Programmer-Visible State EIPProgram 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 EIPEIP Registers CPU Memory Object Code Program Data OS Data Addresses Data Instructions Stack Condition Codes Memory Byte addressable array Code, user data, (some) OS data Includes stack used to support procedures

21 – 21 – 52011 text 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

22 – 22 – 52011 Compiling Into Assembly C Code int sum(int x, int y) { int t = x+y; return t; } Generated Assembly pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax movl %eax, -4(%ebp) movl -4(%ebp),%eax leave ret Obtain with command gcc -O -S code.c Produces file code.s

23 – 23 – 52011 Assembly Characteristics Minimal Data Types “Integer” data of 1, 2, or 4 bytes Data values Addresses (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 register Load data from memory into register Store register data into memory Transfer control Unconditional jumps to/from procedures Conditional branches

24 – 24 – 52011 Machine Instruction Example C Code Add two signed integers Assembly Add 2 4-byte integers Same instruction whether signed or unsigned Operands: x :Register %eax y :MemoryM[ %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

25 – 25 – 52011 Moving Data movl 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, $-533 Encoded with 1, 2, or 4 bytes Register: One of 8 integer registers But %esp and %ebp reserved for special use Others have special uses for particular instructions Memory: 4 consecutive bytes of memory Various “address modes” %eax %edx %ecx %ebx %esi %edi %esp %ebp

26 – 26 – 52011 movl Operand Combinations Cannot do memory-memory transfers with single instruction movl Imm Reg Mem Reg Mem Reg Mem Reg SourceDestination 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;

27 – 27 – 52011 Simple Addressing Modes Normal(R)Mem[Reg[R]] Register R specifies memory address movl (%ecx),%eax DisplacementD(R)Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl 8(%ebp),%edx

28 – 28 – 52011 Look at sum again int sum(int x, int y) { int t = x+y; return t; } pushl %ebp movl %esp,%ebp //handle stack movl 12(%ebp),%eax // %eax = y addl 8(%ebp),%eax // %eax = y+x movl %eax, -4(%ebp) // t = %eax movl -4(%ebp),%eax // return t leave ret

29 – 29 – 52011 Using Simple Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx return ret Body Set Up Finish

30 – 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 = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx Stack RegisterVariable %ecxyp %edxxp %eaxt1 %ebxt0 yp xp Rtn adr Old % ebp %ebp 0 4 8 12 Offset Old % ebx -4

31 – 31 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp0x104

32 – 32 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x120 0x104

33 – 33 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x124 0x120 0x104

34 – 34 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 0x104

35 – 35 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 123 0x104

36 – 36 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 123 0x104

37 – 37 – 52011 Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 456 123 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 123 0x104

38 – 38 – 52011 Indexed Addressing Modes Most 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]]

39 – 39 – 52011 Address Computation Examples %edx %ecx 0xf000 0x100 ExpressionComputationAddress 0x8(%edx)0xf000 + 0x80xf008 (%edx,%ecx)0xf000 + 0x1000xf100 (%edx,%ecx,4)0xf000 + 4*0x1000xf400 0x80(,%edx,2)2*0xf000 + 0x800x1e080

40 – 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.

41 – 41 – 52011 Some Arithmetic Operations FormatComputation Two Operand Instructions addl Src,DestDest = Dest + Src subl Src,DestDest = Dest - Src imull Src,DestDest = Dest * Src sall Src,DestDest = Dest << SrcAlso called shll sarl Src,DestDest = Dest >> SrcArithmetic shrl Src,DestDest = Dest >> SrcLogical xorl Src,DestDest = Dest ^ Src andl Src,DestDest = Dest & Src orl Src,DestDest = Dest | Src

42 – 42 – 52011 Some Arithmetic Operations FormatComputation One Operand Instructions incl DestDest = Dest + 1 decl DestDest = Dest - 1 negl DestDest = - Dest notl DestDest = ~ Dest

43 – 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 %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax leave ret Body Set Up Finish

44 – 44 – 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; } movl 8(%ebp),%eax# eax = x movl 12(%ebp),%edx# edx = y leal (%edx,%eax),%ecx# ecx = x+y (t1) leal (%edx,%edx,2),%edx# edx = 3*y sall $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) y x Rtn adr Old % ebp %ebp 0 4 8 12 Offset Stack z 16

45 – 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 = x movl 8(%ebp),%eax # edx = y movl 12(%ebp),%edx # ecx = x+y (t1) leal (%edx,%eax),%ecx # edx = 3*y leal (%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!

46 – 46 – 52011 Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Implicitly Set By Arithmetic Operations addl Src,Dest C 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) Not Set by leal instruction

47 – 47 – 52011 Setting Condition Codes (cont.) Explicit Setting by Compare Instruction cmpl Src2,Src1 cmpl b,a like computing a-b without setting destination CF set if carry out from most significant bit Used 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)

48 – 48 – 52011 Setting Condition Codes (cont.) Explicit Setting by Test instruction testl 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

49 – 49 – 52011 Reading Condition Codes SetX Instructions Set single byte based on combinations of condition codes

50 – 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 = y cmpl %eax,8(%ebp)# Compare x : y setg %al# al = x > y movzbl %al,%eax# Zero rest of %eax Note inverted ordering! Body

51 – 51 – 52011 Jumping jX Instructions Jump to different part of code depending on condition codes

52 – 52 – 52011 Conditional Branch Example int max(int x, int y) { if (x > y) return x; else return y; } _max: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax cmpl %eax,%edx jle L9 movl %edx,%eax L9: leave ret Body Set Up Finish

53 – 53 – 52011 Conditional Branch Example (Cont.) movl 8(%ebp),%edx# edx = x movl 12(%ebp),%eax# eax = y cmpl %eax,%edx# x : y jle L9# if <= goto L9 movl %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

54 – 54 – 52011 C Code int fact_do (int x) { int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; } Goto Version int 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

55 – 55 – 52011 Goto Version int 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 %edxx %eaxresult _fact_goto: pushl %ebp# Setup movl %esp,%ebp# Setup movl $1,%eax# eax = 1 movl 8(%ebp),%edx# edx = x L11: imull %edx,%eax# result *= x decl %edx# x-- cmpl $1,%edx# Compare x : 1 jg L11# if > goto loop movl %ebp,%esp# Finish popl %ebp# Finish ret# Finish Assembly

56 – 56 – 52011 C Code int fact_while (int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } First Goto Version int 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

57 – 57 – 52011 C Code int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } Second Goto Version int 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

58 – 58 – 52011 C Code while ( 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:

59 – 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.L6 L7: cmpl $3 -8(%ebp) je.L5 jmp.L6 L3: movl $3, -4(%ebp) jmp.L2 L4: movl $5, -4(%ebp) jmp.L2 L5: 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


Download ppt "– 1 – 52011 IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic."

Similar presentations


Ads by Google