1 / 50

Control

Control. Outline. Switch Conditional Move Suggested reading Chap 3.6.6, 3.6.7. Switch Statements. int switch_eg(int x, int n) { int result = x ; switch ( n ) { case 100: result *= 13 ; break ; case 102: result += 10 ; /* fall through */. case 103 result += 11;

ciel
Download Presentation

Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control

  2. Outline • Switch • Conditional Move • Suggested reading • Chap 3.6.6, 3.6.7

  3. Switch Statements int switch_eg(int x, int n) { int result = x ; switch ( n ) { 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 ; }

  4. Switch Construct • Properties of Switch Construct • Integer testing • Multiple outcomes (may be a large number) • Improve the readability of the source code

  5. Switch Statements int switch_eg(int x, int n) { int result = x ; switch ( n ) { 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 ; } Multiple cases Integer testing

  6. Jump Table • Efficient implementation • Avoid long sequence of if-else statement • Criteria • the number of cases and the sparcity of the case value

  7. Targ0: Code Block 0 jtab: Targ0 Targ1 Targ2 Targ1: Code Block 1 • • • Targn-1 Targ2: Code Block 2 • • • Targn-1: Code Block n–1 Jump Table Implementation Jump Table Jump Targets Approx. Translation target = JTab[op]; goto *target;

  8. Switch Statements int switch_eg(int x, int n) { int result = x ; switch ( n ) { 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 ; }

  9. Jump Table Implementation code jt[7] = {loc_a, loc_def, loc_b, loc_c, loc_d, loc_def, loc_d}; int switch_eg_goto ( int x, int n) { unsigned ni = n - 100; int result = x ; if ( ni >6 ) goto loc_def ; //default goto jt[xi]; loc_a: //100 result *= 13 ; goto done ; loc_b: //102 result += 10 ; /* fall through*/ loc_c: //103 result +=11; goto done ; loc_d: //104, 106 result *= result ; goto done ; loc_def: //default result = 0 ; done: return result ; }

  10. Jump Table • .section .rodata • .align 4 • .L7: • .long .L3 case 100: loc_a • .long .L2 case 101: loc_def • .long .L4 case 102: loc_b • .long .L5 case 103: loc_c • .long .L6 case 104: loc_d • .long .L2 case 105: loc_def • .long .L6 case 106: loc_d

  11. Jump Table Implementation • movl 8(%ebp), %edx get x • movl 12(%ebp), %eax get n • subl $100, %eax compute index = n – 100 • cmpl $6, %eax compare index:6 • ja .L2 If > , goto default • jmp *.L7(, %eax, 4) • .L2: default: • mov $0, %eax result = 0 • jmp .L8 goto done

  12. Jump Table Implementation • .L5: loc_c: // 103 • movl %edx, %eax result = x • jmp .L9 goto rest • .L3: loc_a: // 100 • leal (%edx, %edx, 2), %eax result = x * 3 • leal (%edx, %eax, 4), %eax result = x + 4 * result • jmp .L8 goto done • .L4: loc_b: // 102 • leal 10(%edx), %eax result = x + 10

  13. Jump Table Implementation • .L9: rest: // fall through • addl $11, %eax result += 11 • jmp .L8 goto done • .L6: loc_d: // 104, 106 • movl %edx, %eax result = x • imull %edx, %eax result *= x • .L8: done:

  14. Conditional Move • Original C code • 1 int absdiff(int x, int y) { • 2 return x < y ? y-x : x-y; • 3 } (b) Implementation using conditional assignment 1 int cmovdiff(int x, int y) { 2 int tval = y-x; 3 int rval = x-y; 4 int test = x < y; 5 /* Line below requires 6 single instruction: */ 7 if (test) rval = tval; 8 return rval; 9 }

  15. Conditional Move (c) Generated assembly code (x at %ebp+8, y at %ebp+12) movl 8(%ebp), %ecx Get x movl 12(%ebp), %edx Get y movl %edx, %ebx Copy y subl %ecx, %ebx Compute y-x movl %ecx, %eax Copy x subl %edx, %eax Compute x-y and set as return value cmpl %edx, %ecx Compare x:y cmovl %ebx, %eax If < , replace return value with y-x %ecx x %edx y %ebx y-x %eax x-y

  16. Invalid Situation • Conditional Move instructions suppose that “there is no side effect” int cread(int *xp) { return (xp ? *xp : 0); }

  17. Procedure Call

  18. Outline • Procedure call • Stack frame • Calling conventions • Recursive • Suggested reading • Chap 3.7

  19. Execution within Procedure/Function • Data Movement (e.g., movl $-17, (%esp)) • Arithmetic Operations (e.g., incl 8(%eax)) • Logical Operations (e.g., xorl 8(%esp), %eax) • Condition Codes (e.g., cmpl %eax, %edx) • Jump Instructions (e.g., jg .L5) How to execute cross procedures?

  20. Procedure/Function call • Another type of unconditional JUMP • SAME: • Control from one part to another • DIFF: • Return • Passing data (arguments, return values) • Local variable • Registers

  21. Basic Concept • Terminology • Caller • Callee f() call-1 g() h() call-2 • call-1 • Caller: f • Callee: g • call-2 • Caller: g • Callee: h

  22. Basic Concept • Terminology • Caller: g • Callee: f • Control Flow e.g. int g() { return 1 ; } int f() { return g() ; } int main() { g(); return f(); } g() call main() f() g() f() g()

  23. Procedure/Function Implementation • Invoke callee • Return to caller • Passing data • Registers • Local variable

  24. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller • Passing data • Registers • Local variable

  25. Invoke Callee • Instruction • call label (direct) • call *operand(indirect) • Behavior description (by hardware) • Save return address in the stack • Jump to the entryof callee call = push + jmp • push retaddr • jmpcallee

  26. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data • Registers • Local variable

  27. Return to Caller • Instruction • ret • Behavior description (by hardware) • Pop return address from stack • Jump to return addressin caller ret = pop + jmp • pop retaddr • jmpretaddr

  28. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers • Local variable

  29. %ebp %esp Stack Frame Structure • The portion of stack allocated for a procedure • A stack frame is delimited by • The frame pointer %ebp • The stack pointer %esp • The stack pointer can move when the procedure is executing (dynamic) • The frame pointer is static

  30. %ebp %esp Stack Frame Structure • call: save return address in the stack • ret: pop return address from stack • The end of caller’s stack frame

  31. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • 2. push %ebp • 3.mov %esp, %ebp • . . . • n-2. mov %ebp, %esp • n-1. pop %ebp • n. ret %ebp %esp

  32. Restore Caller %ebp • Instruction • leave • Behavior description (by hardware) • Adjust %esp to callee %ebp • Pop caller %ebp from stack leave = mov + pop • mov %ebp, %esp • pop %ebp

  33. Execution of call and ret //beginning of function sum • 08048394 <sum>: • 8048394: 55 push %ebp . . . • 80483a4: ret . . . //call to sum from main • 80483dc: e8 b3 ffffff call 8048394<sum> • 80483e1: 83 c4 14 add $0x14, %esp Executing call After call After ret

  34. Memory Layout

  35. %ebp %esp Passing Data: Arguments • Pushed by Caller • Saved in caller frame • Just upon of return address • From Nth to 1st • Used by Callee • Relative to %ebp • Offset: 4 + 4*i + %ebp

  36. %ebp %esp Passing Data: Arguments push argument N

  37. %ebp %esp Passing Data: Arguments push argument N . . . push argument 1

  38. %ebp %esp Passing Data: Arguments push argument N . . . push argument 1 call callee

  39. %ebp %esp Passing Data: Arguments push argument N . . . push argument 1 call callee push %ebp

  40. %esp / %ebp Passing Data: Arguments push argument N . . . push argument 1 call callee push %ebp mov %esp, %ebp . . .

  41. Passing Data: Return Value • Specific register to keep the return value • %eax is used to pass the result of callee to caller

  42. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers: calling convention • Local variable

  43. Calling Convention • Registers act as a single resource shared by all of the procedures • Only 1 procedure can be active • Partition registers between caller and callee • Saller-save register • Callee-save register • Only consider the registers used by the procedure

  44. Calling Convention • Caller-save registers • %eax, %edx, %ecx • Saved by caller • Callee can use these registers freely • The contents in these registers may be changed after return • Caller must restore them if it tries to use them after calling

  45. %ebp %esp Caller-save Registers push %eax push argument N . . . push argument 1 call callee

  46. Calling Convention • Callee-save registers • %ebx, %esi, %edi • Saved by callee • Caller can use these registers freely • Callee must save them before using • Callee must restore them before return

  47. %ebp %esp Callee-save Registers call callee push %ebp mov %esp, %ebp push %ebx . . .

  48. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers: calling convention • Local variable: stack

  49. Local Variable • Why not store local variables in registers ? • No enough registers • Array and structures (e.g., a[2]) • Need address (e.g., &a)

  50. %ebp %esp Local Variable • Allocation • Below saved regs or old %ebp • move/sub %esp, (e.g., subl $4, %esp) • De-allocation • move/add %esp, (e.g., addl $4, %esp) • Usage • Relative to %esp/%ebp, (e.g., movl %eax, 8(%esp))

More Related