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

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, Jan 29, 2015 Carnegie.
MACHINE-LEVEL PROGRAMMING II: ARITHMETIC & CONTROL.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, May 28, 2015 Carnegie.
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control : Introduction to Computer Systems 5 th Lecture, Sep. 7, 2010 Carnegie Mellon.
1 Seoul National University Machine-Level Programming II: Arithmetic & Control.
II:1 x86 Assembly - Control. II:2 Alternate reference source Go to the source: Intel 64 and IA32 
1 Machine-Level Programming III: Arithmetic & Condition Codes Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
Controlling Program Flow. – 2 – Control Flow Computers execute instructions in sequence. Except when we change the flow of control Jump and Call instructions.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, Sep. 11, 2014 Carnegie.
תרגול 5 תכנות באסמבלי, המשך
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
University of Washington x86 Programming II The Hardware/Software Interface CSE351 Winter 2013.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
University of Washington This week Lab 1 due 11pm today  Frustrating? Awesome? Both? Neither? Lab 2 out later today, due 11pm Wednesday July 17  Disassembly,
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Lecture 7 Flow of Control Topics Finish Lecture 6 slides Lab 02 = datalab comments Assembly Language flow of control Test 1 – a week from wednesday February.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Carnegie Mellon Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Carnegie Mellon.
1 Binghamton University Machine-Level Programming II: Arithmetic & Control CS220: Computer Systems II.
Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming 2 Control Flow
Samira Khan University of Virginia Feb 2, 2017
Conditional Branch Example
Homework Reading Labs PAL, pp
Assembly Programming III CSE 410 Winter 2017
Assembly Programming III CSE 351 Spring 2017
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II: Control
Machine-Level Programming II: Control Flow
x86 Programming II CSE 351 Autumn 2016
x86-64 Programming II CSE 351 Autumn 2017
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Controlling Program Flow
x86-64 Programming II CSE 351 Winter 2018
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Assembly Programming III CSE 351 Spring 2017
Instructor: David Ferry
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
Carnegie Mellon Ithaca College
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
x86-64 Programming II CSE 351 Summer 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming II CSE 351 Autumn 2018
Machine-Level Programming II: Control Flow
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Credits and Disclaimers
Presentation transcript:

1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming IV Control Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* Ithaca College

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

3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Processor State (x86-64, Partial) Information about currently executing program  Temporary data ( %rax, … )  Location of runtime stack ( %rsp )  Location of current code control point ( %rip, … )  Status of recent tests ( CF, ZF, SF, OF ) %rip Registers Current stack top Instruction pointer CFZFSFOF Condition codes %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Ithaca College

4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes (Implicit Setting) Single bit registers  CF Carry Flag (for unsigned)SF Sign Flag (for signed)  ZF Zero FlagOF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmetic operations Example: addq Src,Dest ↔ t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b>0 && t =0) Not set by leaq instruction Ithaca College

5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes Examples: assume %rax contains 20 addl $10, %raxCF  0 ZF  0 SF  0 OF  0 subl $50, %raxCF  0 ZF  0 SF  1 OF  0 Note that these flags are set just like we talked about in chapter 2!

6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Also implicitly Set By logical Operations xorl Src,Dest C analog: t = a ^ b(a = Src, b = Dest)  CF set to 0  ZF set if t == 0  SF set if t < 0  OF set to 0  For shift operations:  CF set to last bit shifted out  OF set to 0

7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes (Implicit Setting) There are no Boolean types in assembly language. There is no concept of Boolean operators in assembly language. So what is there?  Bits  All we can do is look at bits. What those bits mean is determined by the context of the program.  Instructions set flags.  Other instructions take action based on the bits in those flags.  Don’t think about it as comparison as in the C language sense.

8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes (Explicit Setting: Compare) Explicit Setting by Compare Instruction  cmpq Src2, Src1  cmpq 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 (as signed)  OF set if two’s-complement (signed) overflow (a>0 && b 0 && (a-b)>0) Note: notice that the operands are in reverse order; b is the first operand, but we compute a – b! Note 2: there are also cmpw and cmpb instructions. Ithaca College

9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Example cmpl %rax, %rcx do %rcx - %rax then set condition codes NO other action taken! %rax 0000 %rcx %rdx 0000 %rbx 0000 %rsi %rdi %rsp %rbp CFZFSFOF Condition codes ??

10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Example cmpl %rax, %rcx do %rcx - %rax then set condition codes NO other action taken! %rax 0000 %rcx %rdx 0000 %rbx 0000 %rsi %rdi %rsp %rbp CFZFSFOF Condition codes 0000

11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes (Explicit Setting: Test) Explicit Setting by Test instruction  testq Src2, Src1  testq b,a like computing a&b without setting destination  Sets condition codes based on value of Src1 & Src2  Useful to have one of the operands be a mask  CF set to 0  ZF set when a&b == 0  SF set when a&b < 0  OF set to 0 Note: typically the same operand is repeated to test whether it is negative, zero, or positive: testl %rax, %rax sets the condition codes depending on the value in %rax Note 2: there are also testl, testw and testb instructions. Ithaca College

12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Reading Condition Codes SetX Instructions  Set low-order byte of destination to 0 or 1 based on combinations of 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) Ithaca College

13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp x86-64 Integer Registers  Can reference low-order byte %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 %al

14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Reading condition codes Consider: setl D (SF^OF)Less (Signed <)D  (SF^OF ) First compare two numbers, a and b where both are in 2’s complement form using an arithmetic, logical, test or cmp instruction Then use setX to set a register with the result of the test cmpq%rax, %rdx setl %al puts the result in byte register %al

15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Reading condition codes (cont) Assume a is in %rax and b in %rdx cmpq%rax, %rdx setl %al puts the result in byte register %al First instruction sets the condition code.  cmpq computes b – a  If b < a then b – a < 0 If there is no overflow, this is indicated by SF  If there is positive overflow (b – a is large), we have b – a < 0 but OF is set  If there is negative overflow (b – a is very small), we have b – a > 0 but OF is set  In either case the sign flag will indicate the opposite of the sign of the true difference. Hence, use exclusive-or of the OF and SF Second instruction sets the %al register to or depending on the value of (SF^OF) setl D ; D  (SF^OF )

16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Reading condition codes (cont) Example: assume %rax holds 20 and %rdx holds 50 cmpq%rax, %rdx 50 – 20, SF  0, OF  0 setl %al %al  0 ^ 0 = Example: assume %rax holds 0x and %rdx holds 20 cmpq%rax, %rdx 20 – ( ), SF  1, OF  1 setl %al %al  1 ^ 1 = setq gives false; 20 is not less than setl D ; D  (SF^OF )

17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Reading condition codes (cont) Example: assume %rax holds 20 and %rdx holds 0x cmpq%rax, %rdx ( ) - 20, SF  0, OF  1 0x xFFFFFFEC = 0x7FFFFFED (note that 7 = 0111) setl %al %al  0 ^ 1 = setq gives true; is less than 20

18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Setg Greater (Signed) ~(SF^OF)&~ZF CFZFSFOF Condition codes ~SF&~OF If this is zero, can’t be > If the overflow flag is set, can’t be > if we did a cmpl or testl as the previous instruction. Why? First do an arith, logical, cmp or test. Then use the flags

19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %rax # Zero rest of %rax ret Reading Condition Codes (Cont.) SetX Instructions:  Set single byte based on combination of condition codes One of addressable byte registers  Does not alter remaining bytes  Typically use movzbl to finish job  32-bit instructions also set upper 32 bits to 0 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 %rax Return value Ithaca College

20 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %rax # Zero rest of %rax ret Reading Condition Codes (Cont.) 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 %rax Return value %rax %rdx %rcx %rbx %rsi %rdi %rsp %rbp %al%ah %dl%dh %cl%ch %bl%bh All 0’s: Either or Ithaca College

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

22 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Jumping A jump instruction causes execution to jump to a new address  Really just puts the argument of the jump into the EIP  Jump destinations are usually indicated by a label  A label marks a place in code 1xorq%rax, %rax 2jmp.L1 3movq(%rax), %rdx 4.L1: popq %rdx  The instruction jmp.L1 causes program to skip line 3  jmp is an unconditional jump instruction  Note that we wouldn’t really use the code this way!

23 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Jumping A jump instruction has two types of arguments  Direct jumps. Target is part of the instruction  Use a label  Indirect jumps. Target read from a register or memory.  Use a ‘*’ followed by an operand specifier jmp *%eax or jmp *(%eax)  Any addressing mode can be used  Note that jmp is an unconditional jump instruction

24 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Jumping Other jump instructions are conditional  Either jump or continue executing at next instruction in the code  Depends on some combination of the condition codes  Names and conditions match the set instructions.  Conditional jumps can only be direct Assembler changes labels into actual memory addresses  See section of the book  Not very important now; will be very important in chapter 7

25 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Jumping jX Instructions  Jump to different part of code depending on condition codes 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) Ithaca College

26 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition jg Greater (Signed) ~(SF^OF)&~ZF CFZFSFOF Condition codes ~SF&~OF If this is zero, can’t be > If the overflow flag is set, can’t be > if we did a cmpq or testq as the previous instruction. Why? First do an arith, logical, cmp or test. Then use the flags

27 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 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 Generation nori> gcc –Og -S –fno-if-conversion control.c RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value Ithaca College

28 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Expressing with 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; } C allows goto statement Jump to position designated by label 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; } Ithaca College Generally considered bad coding style

29 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Expressing with Goto Code C allows goto statement Jump to position designated by label 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; } Ithaca College 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 RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

30 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Expressing with Goto Code C allows goto statement Jump to position designated by label 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; } Ithaca College 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 RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

31 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Expressing with Goto Code C allows goto statement Jump to position designated by label 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; } Ithaca College 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 RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

32 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Expressing with Goto Code C allows goto statement Jump to position designated by label 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; } Ithaca College 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 RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

33 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Example 2 Download ifStmt.c gcc –O0 –o ifStmt –fno-if-conversion ifStmt.c gdb ifStmt In gdb: disass testIf1 int testIf1(int x, int y) { int result; if (x < y) result = x * 4; else result = y * 4; return result; }

34 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Example 2 continued… pushq %rbp movq %rsp, %rbp movq %rdi, -8(%rbp) movq %rsi, -16(%rbp) movq -8(%rbp), %rsi cmpq -16(%rbp), %rsi jge LBB0_2 movq -8(%rbp), %rax shlq $2, %rax movq %rax, -24(%rbp) jmp LBB0_3 LBB0_2: movq -16(%rbp), %rax shlq $2, %rax movq %rax, -24(%rbp) LBB0_3: movq -24(%rbp), %rax popq %rbp retq Setting up the runtime stack if x - y >= 0 jump if x - y >= 0 jump else calculate x=x*4 and jump else calculate x=x*4 and jump Calculate y=y*4 Set up the return value RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value int testIf1(int x, int y) { int result; if (x < y) result = x * 4; else result = y * 4; return result; }

35 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition C Code val = Test ? Then_Expr : Else_Expr ; Goto Version ntest = ! Test ; if (ntest) goto Else; val = Then_Expr ; goto Done; Else: val = Else_Expr ; Done:... ntest = ! Test ; if (ntest) goto Else; val = Then_Expr ; goto Done; Else: val = Else_Expr ; Done:... General Conditional Expression Translation (Using Branches)  Test Is an expression returning an integer  = 0 interpreted as false  ≠ 0 interpreted as true  Create separate code regions for then & else expressions  Execute appropriate one val = x>y ? x-y : y-x; Ithaca College

36 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition C Code val = Test ? Then_Expr : Else_Expr ; val = Test ? Then_Expr : Else_Expr ; Goto Version result = Then_Expr ; eval = Else_Expr ; nt = ! Test ; if (nt) result = eval; return result; result = Then_Expr ; eval = Else_Expr ; nt = ! Test ; if (nt) result = eval; return result; Using Conditional Moves Conditional Move Instructions  Instruction supports: if (Test) Dest  Src  Supported in post-1995 x86 processors  GCC tries to use them  But, only when known to be safe Why?  Branches are very disruptive to instruction flow through pipelines  Conditional moves do not require control transfer Ithaca College

37 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Conditional Move cmovX Instructions  Jump to different part of code depending on condition codes jXSynonymConditionDescription cmove S*,R*cmovzZF Equal / Zero cmovne S,Rcmovnz~ZF Not Equal / Not Zero cmovs S,RSF Negative cmovns S,R~SF Nonnegative cmovg S,Rcmovnle~(SF^OF)&~ZF Greater (Signed) cmovge S,Rcmovnl~(SF^OF) Greater or Equal (Signed) cmovl S,Rcmovnge(SF^OF) Less (Signed) cmovle S,Rcmovng(SF^OF)|ZF Less or Equal (Signed) cmova S,Rcmovnbe~CF&~ZF Above (unsigned) cmovae S,Rcmovnb~CF Above or equal (unsigned) cmovb S,RcmovnaeCF Below (unsigned) cmovbe S,RcmovnaCF | ZF Below or equal (unsigned) Ithaca College * source/destination may be 16, 32, or 64 bits (not 8). No size suffix: assembler infers the operand length based on destination register

38 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Conditional Move Example absdiff: movq %rdi, %rax # x subq %rsi, %rax # result = x-y movq %rsi, %rdx subq %rdi, %rdx # eval = y-x cmpq %rsi, %rdi # x:y cmovle %rdx, %rax # if <=, result = eval 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 Ithaca College

39 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 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 (always dereference p but it may be null!) val = p ? *p : 0; Computations with side effects Both values get computed Must be side-effect free val = x > 0 ? x*=7 : x+=3; Ithaca College In general, gcc only uses conditional moves when the two expressions can be computed very easily, e.g., single instructions.

40 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Practice Problem long test (long x, long y) { long val = _________; if (_________){ if (__________){ val = ________; else val = ________; } else if (________) val = ________; return val; } long test (long x, long y) { long val = _________; if (_________){ if (__________){ val = ________; else val = ________; } else if (________) val = ________; return val; } test: leaq0(,%rdi,8), %rax testq%rsi, %rsi jle.L4 movq %rsi, %rax subq %rdi, %rax movq%rdi, %rdx andq %rsi, %rdx cmpq %rsi, %rdi cmovge%rdx, %rax ret.L4: # x <= y addq%rsi, %rdi cmpq%-2, %rsi cmovle%rdi, %rax ret Generation RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value Ithaca College

41 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Practice Problem long test (long x, long y) { long val = 8*x; if (y > 0){ if (x < y){ val = y - x; else val = x & y; } else if (y <= -2) val = x + y; return val; } long test (long x, long y) { long val = 8*x; if (y > 0){ if (x < y){ val = y - x; else val = x & y; } else if (y <= -2) val = x + y; return val; } test: leaq0(,%rdi,8), %rax testq%rsi, %rsi jle.L4 movq %rsi, %rax subq %rdi, %rax movq%rdi, %rdx andq %rsi, %rdx cmpq %rsi, %rdi cmovge%rdx, %rax ret.L4: # x <= y addq%rsi, %rdi cmpq%-2, %rsi cmovle%rdi, %rax ret Generation RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value Ithaca College

42 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Summarizing C Control  if-then-else  do-while  while, for  switch Assembler Control  Conditional jump  Conditional move  Indirect jump (via jump tables)  Compiler generates code sequence to implement more complex control Standard Techniques  Loops converted to do-while or jump-to-middle form  Large switch statements use jump tables  Sparse switch statements may use decision trees (if-elseif-elseif-else) Ithaca College

43 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Summary Today  Control: Condition codes  Conditional branches & conditional moves Next Time  Loops  Switch statements  Stack  Call / return  Procedure call discipline Ithaca College