Carnegie Mellon Ithaca College MachineLevel Programming IV Control

  • Slides: 19
Download presentation
Carnegie Mellon Ithaca College Machine-Level Programming IV Control Comp 21000: Introduction to Computer Organization

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

Ithaca College Today Control: Condition codes ¢ Conditional branches ¢ § If-then-else statements §

Ithaca College Today Control: Condition codes ¢ Conditional branches ¢ § If-then-else statements § Conditional moves Loops ¢ Switch Statements ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

Ithaca College Using Conditional Moves ¢ Conditional Move Instructions § Instruction supports: if (Test)

Ithaca College Using Conditional Moves ¢ Conditional Move Instructions § Instruction supports: if (Test) Dest Src § Supported in post-1995 x 86 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 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition C Code val = Test ? Then_Expr : Else_Expr; Goto Version result = Then_Expr; eval = Else_Expr; nt = !Test; if (nt) result = eval; return result; 3

Ithaca College Conditional Move * source/destination may be 16, 32, or 64 bits (not

Ithaca College Conditional Move * source/destination may be 16, 32, or 64 bits (not 8). No size suffix: assembler infers the operand length based on destination regi ¢ cmov. X Instructions § Jump to different part of code depending on condition codes j. X Synonym Condition Description cmove S*, R* cmovz ZF Equal / Zero cmovne S, R cmovnz ~ZF Not Equal / Not Zero cmovs S, R SF Negative cmovns S, R ~SF Nonnegative cmovg S, R cmovnle ~(SF^OF)&~ZF Greater (Signed) cmovge S, R cmovnl ~(SF^OF) Greater or Equal (Signed) cmovl S, R cmovnge (SF^OF) Less (Signed) cmovle S, R cmovng (SF^OF)|ZF Less or Equal (Signed) cmova S, R cmovnbe ~CF&~ZF Above (unsigned) cmovae S, R cmovnb ~CF Above or equal (unsigned) cmovb S, R cmovnae CF Below (unsigned) cmovbe S, R cmovna CF | ZF Below or equal (unsigned) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4

Ithaca College Conditional Move Example long absdiff (long x, long y) { long result;

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

Ithaca College Bad Cases for Conditional Move Expensive Computations val = Test(x) ? Hard

Ithaca College Bad Cases for Conditional Move Expensive Computations val = Test(x) ? Hard 1(x) : Hard 2(x); ¢ ¢ Both values get computed Only makes sense when computations are very simple In general, gcc only uses conditional moves when the two Risky Computations expressions can be computed very easily, e. g. , single instructions. val = p ? *p : 0; Both values get computed ¢ May have undesirable effects (always dereference p but it may be null!) Computations with side effects ¢ val = x > 0 ? x*=7 : x+=3; Both values get computed ¢ Must be side-effect free Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ¢ 6

Ithaca College Practice Problem ¢ Generation long test (long x, long y) { long

Ithaca College Practice Problem ¢ Generation long test (long x, long y) { long val = _____; if (_____){ val = ____; else val = ____; } else if (____) val = ____; return val; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value test: leaq testq jle movq subq movq andq cmpq cmovge ret. L 4: addq cmpq cmovle ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 0(, %rdi, 8), %rax %rsi, %rsi test %rsi, %rsi. L 4 Does %rsi & %rsi, %rax %rdi, %rdx %rsi, %rdi %rdx, %rax %rsi, %rdi %-2, %rsi %rdi, %rax 7

Ithaca College Condition Codes (Explicit Setting: Test) ¢ Explicit Setting by Test instruction §testq

Ithaca College Condition Codes (Explicit Setting: Test) ¢ Explicit Setting by Test instruction §testq Src 2, Src 1 §testq b, a like computing a&b without setting destination §Sets condition codes based on value of Src 1 & Src 2 §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 also testl, testw and testb instructions. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8

Ithaca College Reading Condition Codes ¢ Set. X Instructions § Set low-order byte of

Ithaca College Reading Condition Codes ¢ Set. X Instructions § Set low-order byte of destination to 0 or 1 based on combinations of condition codes § Does not alter remaining 7 bytes Set. X sete setne sets setns setge setle seta setb Condition ZF ~ZF SF ~(SF^OF)&~ZF ~(SF^OF)|ZF ~CF&~ZF CF Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Description Equal / Zero Not Equal / Not Zero Negative Nonnegative Greater (Signed) Greater or Equal (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned) 9

x 86 -64 Integer Registers %rax %al %r 8 b %rbx %bl %r 9

x 86 -64 Integer Registers %rax %al %r 8 b %rbx %bl %r 9 b %rcx %cl %r 10 b %rdx %dl %r 11 b %rsi %sil %r 12 b %rdi %dil %r 13 b %rsp %spl %r 14 b %rbp %bpl %r 15 b § Can reference low-order byte Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10

Reading condition codes ¢ Consider: setl ¢ ¢ D (SF^OF) Less (Signed <) D

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 set. X to set a register with the result of the test cmpq setl %rax, %rdx %al puts the result in byte register %al Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11

Reading condition codes (cont) Assume a is in %rax and b in %rdx cmpq

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 ¢ setl D ; D (SF^OF) ¢ 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 0000 or 00000001 depending on the value of (SF^OF) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12

Reading condition codes (cont) Example: assume %rax holds 20 and %rdx holds 50 cmpq

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

Reading condition codes (cont) ¢ Example: assume %rax holds 20 and %rdx holds 0

Reading condition codes (cont) ¢ Example: assume %rax holds 20 and %rdx holds 0 x 8000001 cmpq %rax, %rdx (-2147483647) - 20 , SF 0, OF 1 0 x 80000001 + 0 x. FFFFFFEC = 0 x 7 FFFFFED (note that 7 = 0111) setl %al 0 ^ 1 = 00000001 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition setq gives true; -2147483647 is less than 20 14

Setg Greater (Signed) ~(SF^OF)&~ZF First do an arith, logical, cmp or test. Then use

Setg Greater (Signed) ~(SF^OF)&~ZF First do an arith, logical, cmp or test. Then use the flags ~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? CF ZF SF OF Condition codes Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15

Ithaca College Reading Condition Codes (Cont. ) ¢ Set. X Instructions: § Set single

Ithaca College Reading Condition Codes (Cont. ) ¢ Set. X 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; } cmpq %rsi, %rdi setg %al movzbl %al, %rax ret Register Use(s) %rdi Argument x %rsi Argument y %rax Return value # Compare x: y # Set when > # Zero rest of %rax Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16

Ithaca College Reading Condition Codes (Cont. ) Register Use(s) %rdi Argument x %rsi Argument

Ithaca College Reading Condition Codes (Cont. ) Register Use(s) %rdi Argument x %rsi Argument y %rax %ah %al %rdx %dh %dl All 0’s: Return value 0000000000000 %rcx int gt (long x, long y) { return x > y; } cmpq %rsi, %rdi setg %al movzbl %al, %rax ret Either 0000 or 00000001 %rbx %ch %cl %bh %bl %rsi %rdi %rsp %rbp # Compare x: y # Set when > # Zero rest of %rax Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17

Ithaca College Summarizing ¢ C Control § § ¢ Assembler Control § § ¢

Ithaca College Summarizing ¢ C Control § § ¢ Assembler Control § § ¢ if-then-else do-while, for switch 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-else) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18

Ithaca College Summary ¢ Today § Control: Condition codes § Conditional branches & conditional

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