Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.

Similar presentations


Presentation on theme: "1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon."— Presentation transcript:

1 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon Slides adapted from Bryant and O’Hallaron

2 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today: How control flow is done Condition codes Conditional branches Loops Switch Statements

3 3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Processor State (x86-64, Partial) Registers Condition codes (single bit registers) %rip CFZFSFOF %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp

4 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Condition Codes (implicit setting) Implicitly set by arithmetic operations Example: addq Src, Dest  t = a + b CF (Carry Flag) set if unsigned overflow ZF (Zero Flag) set if t == 0 SF (Sign Flag) set if t < 0 (as signed) OF (OverFlow Flag) set if signed overflow (a>0 && b>0 && t =0) Not set by leaq

5 5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Condition Codes (Explicit Setting: Compare) Explicit set by Compare Instruction  cmpq b,a like computing a-b without setting destination  CF set if unsigned overflow  ZF set if a == b  SF set if (a-b) < 0 (as signed)  OF set if two’s-complement (signed) overflow (a>0 && b 0 && (a-b)>0)

6 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Condition Codes (Explicit Setting: Test) Explicit set by Test instruction  testq b, a (like computing a&b without setting destination)  ZF set when a&b == 0  SF set when a&b < 0

7 7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Reading Condition Codes SetX Instructions  Set low-order byte of destination register to 0/1 based on condition codes  Does not alter remaining 7 bytes SetXConditionDescription seteZF Equal / Zero setne~ZF Not Equal / Not Zero setsSF Negative setns~SF Nonnegative setg~(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) setbCF Below (unsigned)

8 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp x86-64 Integer Registers %al %bl %cl %dl %sil %dil %spl %bpl %r8b %r9b %r10b %r11b %r12b %r13b %r14b %r15b %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp

9 9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %eax # Zero rest of %eax ret Carnegie Mellon Reading Condition Codes (Cont.) SetX Example: int gt (long x, long y) { return x > y; } int gt (long x, long y) { return x > y; } RegisterUse(s) %rdi Argument x %rsi Argument y %eax Return value

10 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today Control: Condition codes Conditional branches Loops Switch Statements

11 11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Jumping jX Instructions: jump to different part of code jXConditionDescription jmp1 Unconditional jeZF Equal / Zero jne~ZF Not Equal / Not Zero jsSF Negative jns~SF Nonnegative jg~(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) jbCF Below (unsigned)

12 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Conditional Branch Example (Old Style) long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } absdiff: cmpq %rsi, %rdi # x:y jle.L4 movq %rdi, %rax subq %rsi, %rax ret.L4: # x <= y movq %rsi, %rax subq %rdi, %rax ret gcc –Og -S –fno-if-conversion control.c RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

13 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Equivalent goto code long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } Machine code is essentially doing control flow with goto statement long absdiff_j (long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } long absdiff_j (long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } Do not write this normally

14 14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Another way: Using Conditional Moves Conditional Move Instructions  cmovX instruction if (Test) Dest  Src  Supported in post-1995 x86 processors Why?  Branches (control transfer) can slow down execution  Conditional moves do not require control transfer

15 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Conditional Move Example absdiff: movq %rdi, %rax # x subq %rsi, %rax # result = x-y movq %rsi, %rdx subq %rdi, %rdx # rdx = y-x cmpq %rsi, %rdi # x:y cmovle %rdx, %rax # if <=, result = rdx ret long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

16 16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Expensive Computations Bad Cases for Conditional Move Both values get computed Only makes sense when computations are very simple val = Test(x) ? Hard1(x) : Hard2(x); Risky Computations Both values get computed May have undesirable effects val = p ? *p : 0; Computations with side effects Both values get computed Must be side-effect free val = x > 0 ? x*=7 : x+=3;

17 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today Control: Condition codes Conditional branches Loops Switch Statements

18 18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon C Code long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } Goto Version long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } “Do-While” Loop Example Count number of 1’s in argument x (“popcount”) Use conditional branch to either continue looping or to exit loop

19 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Goto Version “Do-While” Loop Compilation movl $0, %eax# result = 0.L2:# loop: movq %rdi, %rdx andl $1, %edx# t = x & 0x1 addq %rdx, %rax# result += t shrq %rdi# x >>= 1 jne.L2# if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } RegisterUse(s) %rdi Argument x %raxresult

20 20 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon C Code do … while ( Test ); do … while ( Test ); Goto Version loop: … if ( Test ) goto loop loop: … if ( Test ) goto loop General “Do-While” Translation

21 21 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon While version while ( Test ) Body while ( Test ) Body General “While” Translation #1 “Jump-to-middle” translation Used with -Og Goto Version goto test; loop: Body test: if ( Test ) goto loop; done: goto test; loop: Body test: if ( Test ) goto loop; done:

22 22 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon While version while ( Test ) Body while ( Test ) Body Do-While Version if (! Test ) goto done; do Body while( Test ); done: if (! Test ) goto done; do Body while( Test ); done: General “While” Translation #2 “Do-while” conversion Used with –O1 Goto Version if (! Test ) goto done; loop: Body if ( Test ) goto loop; done: if (! Test ) goto done; loop: Body if ( Test ) goto loop; done:

23 23 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon “For” Loop for ( Init ; Test ; Update ) Body long pcount_for(unsigned long x) { long result = 0; for (int i = 0; i < 8*sizeof(int); i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } long pcount_for(unsigned long x) { long result = 0; for (int i = 0; i < 8*sizeof(int); i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; }

24 24 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon “For” Loop  While Loop for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version

25 25 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Summary Today  Control: Condition codes  Conditional branches & conditional moves  Loops  Switch statements


Download ppt "1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon."

Similar presentations


Ads by Google