controlling program flow
play

Controlling Program Flow Conditionals (If-statement) Loops - PowerPoint PPT Presentation

Controlling Program Flow Conditionals (If-statement) Loops (while, do-while, for-loops) Switch Statements New Instructions JMP CMP Conditional jumps (branches) Conditional MOV instruction 1 Conditional statements 2


  1. Controlling Program Flow • Conditionals (If-statement) • Loops (while, do-while, for-loops) • Switch Statements • New Instructions JMP CMP Conditional jumps (branches) Conditional MOV instruction 1

  2. Conditional statements 2

  3. Condition Code Register 3

  4. Condition Codes 4

  5. Condition Codes 5

  6. Condition Codes 6

  7. Jump Instructions 7

  8. Jump Instructions 8 Overflow flips result

  9. Jump Instructions 9

  10. Conditional Branch Example Register ¡ Use(s) ¡ long absdiff Argument ¡ x %rdi (long x, long y) Argument ¡ y %rsi { Return ¡value %rax long result; if (x > y) result = x-y; absdiff: else cmpq %rsi, %rdi # x:y result = y-x; jle .L4 return result; movq %rdi, %rax } subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax ret 10

  11. Expressing with Goto Code if (condition) { if (not condition) goto Else; Then Statements Then Statements Then Statements Then Statements Then Statements Then Statements } else { goto Done; Else Statements Else: Else Statements Else Statements Else Statements Else Statements } Else Statements Done: 11

  12. Expressing with Goto Code long absdiff_j (long x, long y) long absdiff { (long x, long y) long result; { int ntest = x <= y; long result; if (ntest) goto Else; if (x > y) result = x-y; result = x-y; goto Done; else Else: result = y-x; result = y-x; return result; Done: } return result; } 12

  13. The SetXX Instructions Set low-order byte of destination to 0x00 or 0x01 based on combinations of condition codes Does not alter remaining 7 bytes. SetX ¡ Condi4on ¡ Descrip4on ¡ Equal ¡/ ¡Zero ¡ sete ZF Not ¡Equal ¡/ ¡Not ¡Zero ¡ setne ~ZF Nega4ve ¡ sets SF Nonnega4ve ¡ setns ~SF Greater ¡(Signed) ¡ setg ~(SF^OF)&~ZF Greater ¡or ¡Equal ¡(Signed) ¡ setge ~(SF^OF) Less ¡(Signed) ¡ setl (SF^OF) Less ¡or ¡Equal ¡(Signed) ¡ setle (SF^OF)|ZF Above ¡(unsigned) ¡ seta ~CF&~ZF Below ¡(unsigned) ¡ setb CF 13

  14. The SetXX Instructions Set low-order byte of destination to 0x00 or 0x01 based on combinations of condition codes Does not alter remaining 7 bytes. int gt (long x, long y) { return x > y; } Typically use movzbl to finish job (The 32-bit instructions also zero out the upper 32-bits.) gt: cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %eax # Zero rest of %rax ret 14

  15. Conditional Expressions An ¡expression ¡operator ¡in ¡“C” ¡ ( ¡Test ? Then_Expr : Else_Expr ¡) Example ¡ val = x>y ? x-y : y-x; Transla4on, ¡using ¡goto ¡code: ¡ Create separate code regions for ntest = ! Test ; if (ntest) goto Else; then & else expressions val = Then_Expr ; Execute the appropriate one goto Done; Else: val = Else_Expr ; Done: . . . 15

  16. The Conditional Move Instructions C ¡Code ¡ result = Test ? Then_Expr : Else_Expr ; Goto ¡Version ¡ result = Then_Expr ; temp = Else_Expr ; nt = ! Test ; if (nt) result = temp; 16

  17. The Conditional Move Instructions long absdiff (long x, long y) { long result; if (x > y) Register ¡ Use(s) ¡ result = x-y; Argument ¡ x %rdi else Argument ¡ y result = y-x; %rsi return result; Return ¡value %rax } 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 17

  18. Bad Cases for Conditional Move Expensive ¡Computa6ons ¡ val = Test(x) ? Hard1(x) : Hard2(x); Risky ¡Computa6ons ¡ val = p ? *p : 0; Computa6ons ¡with ¡side ¡effects ¡ val = x > 0 ? x*=7 : x+=3; 18

  19. Loops do-while while-do do { while (test-expr) { body-statements body-statements } while (test-expr); } goto version goto version t = test-expr if (not t) goto exit loop: loop: body-statements body-statements t = test-expr t = test-expr if (t) goto loop if (t) goto loop exit: 19

  20. Loops do-while while-do do { while (test-expr) { body-statements body-statements } while (test-expr); } goto version goto version goto test loop: loop: body-statements body-statements test: t = test-expr t = test-expr if (t) goto loop if (t) goto loop 20

  21. C examples int factorial_do(int x) int factorial_goto(int x) { { int result = 1; int result = 1; do { loop: result *= x; result *= x; x = x-1; x = x-1; } while (x > 1); if (x > 1) goto loop; return result; return result; } } factorial_goto: movl $1, %eax ; eax = result = 1 .L2: imull %edi, %eax ; result = result*x subl $1,%edi ; x-- cmpl $1,%edi ; if x > 1 jg .L2 ; goto .L2 rep ret ; return 21

  22. “do-while” example revisited C code: do-while while-do int factorial_do(int x) int factorial_while(int x) { { int result = 1; int result = 1; do { while (x > 1) { result *= x; result *= x; x = x-1; x = x-1; } while (x > 1); } return result; return result; } } Are these equivalent? 22

  23. “do-while” example revisited while-do Assembly: do-while factorial_do: factorial_while: movl $1,%eax movl $1,%eax cmpl $1,%edi jle .L6 .L2: .L2: imull %edi, %eax imull %edi, %eax subl $1,%edi subl $1,%edi cmpl $1,%edi cmpl $1,%edi jg .L2 jg .L2 .L6: rep ret rep ret 23

  24. “For” Loop Example int factorial_for(int x) { int result; for (result=1; x > 1; x=x-1) { result *= x; } return result; } Init Test Update result = 1 x > 1 x = x - 1 { result *= x; Body } Is this code equivalent to the do-while version or the while-do version? 24

  25. “For” Loop Example General Form for ( Init ; Test ; Update ) int factorial_for(int x) Body { int result; Init; for (result=1; x > 1; x=x-1) { if (not Test) goto exit; result *= x; loop: } Body; return result; Update; } if ( Test ) goto loop; exit: Init Test Update result = 1 x > 1 x = x - 1 { result *= x; Body } Is this code equivalent to the do-while version or the while-do version? 25

  26. “For” Loop Example factorial_for: movl $1,%eax cmpl $1,%edi Init; jle .L6 if (not Test) goto exit; .L2: imull %edi, %eax loop: subl $1,%edi Body; cmpl $1,%edi Update; jg .L2 if ( Test ) goto loop; .L6: exit: rep ret 26

  27. “For” Loop Example factorial_for: factorial_while: movl $1,%eax movl $1,%eax cmpl $1,%edi cmpl $1,%edi jle .L6 jle .L6 .L2: .L2: imull %edi, %eax imull %edi, %eax subl $1,%edi subl $1,%edi cmpl $1,%edi cmpl $1,%edi jg .L2 jg .L2 .L6: .L6: rep ret rep ret 27

  28. Reverse Engineer This! %edi %esi %edx loop: int loop(int x, int y, int z) subl $1, %edx { js .L18 int result=0; int i; imull %edi, %esi for (i = ____ ; i ____ ; i = ____ ) movl $0, %eax { .L17: result += ___ ; addl %esi, %eax } subl %edi, %edx return result; jns .L17 } rep ret .L18: movl $0, %eax ret What registers hold result and i ? What is the initial value of i ? What is the test condition on i ? How is i updated? What instructions increment result? 28

  29. Reverse Engineer This! %edi %esi %edx loop: int loop(int x, int y, int z) subl $1, %edx { js .L18 int result=0; int i; imull %edi, %esi for (i = z-1 ; i >= 0 ; i = i-x ) movl $0, %eax { .L17: result += y*x ; addl %esi, %eax } subl %edi, %edx return result; jns .L17 } rep ret .L18: movl $0, %eax ret What registers hold result and i ? %eax = result, %edx = i What is the initial value of i ? i = z-1 What is the test condition on i ? i >= 0 How is i updated? i = i - x What instructions increment result? addl (x*y) 29

  30. int switch_eg (int x) { C Switch Statements int result = x; switch (x) { case 100: result *= 13; break; case 102: result += 10; /* Fall through */ case 103: result += 11; break; case 104: case 106: result *= result; break; default: result = 0; } return result; } 30

  31. C Switch Statements 31

  32. C Switch Statements .L0 Code for cases 1, 5 0: 1: switch (x) { .L1 2: case 1: Code for cases 2,3 3: case 5: code at L0 4: case 2: 5: .L2 case 3: code at L1 Code for default case default: code at L2 Check that 0 ≤ x ≤ 5 } if not, goto .L2 %rax = .L3 + (4 *x) jmp * %rax .L3: .quad .L2 .quad .L0 .quad .L1 .quad .L1 .quad .L2 .quad .L0 32

Recommend


More recommend