500 likes | 702 Views
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;
E N D
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; break ; case 104: case 106: result *= result ; break ; default: result = 0 ; } return result ; }
Switch Construct • Properties of Switch Construct • Integer testing • Multiple outcomes (may be a large number) • Improve the readability of the source code
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
Jump Table • Efficient implementation • Avoid long sequence of if-else statement • Criteria • the number of cases and the sparcity of the case value
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;
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 ; }
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 ; }
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
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
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
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:
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 }
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
Invalid Situation • Conditional Move instructions suppose that “there is no side effect” int cread(int *xp) { return (xp ? *xp : 0); }
Outline • Procedure call • Stack frame • Calling conventions • Recursive • Suggested reading • Chap 3.7
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?
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
Basic Concept • Terminology • Caller • Callee f() call-1 g() h() call-2 • call-1 • Caller: f • Callee: g • call-2 • Caller: g • Callee: h
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()
Procedure/Function Implementation • Invoke callee • Return to caller • Passing data • Registers • Local variable
Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller • Passing data • Registers • Local variable
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
Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data • Registers • Local variable
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
Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers • Local variable
%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
%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
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
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
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
%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
%ebp %esp Passing Data: Arguments push argument N
%ebp %esp Passing Data: Arguments push argument N . . . push argument 1
%ebp %esp Passing Data: Arguments push argument N . . . push argument 1 call callee
%ebp %esp Passing Data: Arguments push argument N . . . push argument 1 call callee push %ebp
%esp / %ebp Passing Data: Arguments push argument N . . . push argument 1 call callee push %ebp mov %esp, %ebp . . .
Passing Data: Return Value • Specific register to keep the return value • %eax is used to pass the result of callee to caller
Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers: calling convention • Local variable
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
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
%ebp %esp Caller-save Registers push %eax push argument N . . . push argument 1 call callee
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
%ebp %esp Callee-save Registers call callee push %ebp mov %esp, %ebp push %ebx . . .
Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers: calling convention • Local variable: stack
Local Variable • Why not store local variables in registers ? • No enough registers • Array and structures (e.g., a[2]) • Need address (e.g., &a)
%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))