Seoul National University MachineLevel Programming III Switch Statements

  • Slides: 47
Download presentation
Seoul National University Machine-Level Programming III: Switch Statements and IA 32 Procedures

Seoul National University Machine-Level Programming III: Switch Statements and IA 32 Procedures

Seoul National University Outline Switch statements ¢ IA 32 Procedures ¢ § Stack Structure

Seoul National University Outline Switch statements ¢ IA 32 Procedures ¢ § Stack Structure § Calling Conventions 2

Seoul National University long switch_eg (long x, long y, long z) { long w

Seoul National University long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; } Switch Statement Example ¢ Multiple case labels § Here: 5 & 6 ¢ Fall through cases § Here: 2 ¢ Missing cases § Here: 4 3

Seoul National University Jump Table Structure Switch Form switch(x) { case val_0: Block 0

Seoul National University Jump Table Structure Switch Form switch(x) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n– 1 } Approximate Translation target = JTab[x]; goto *target; Jump Table jtab: Targ 0 Targ 1 Targ 2 • • • Targn-1 Jump Targets Targ 0: Code Block 0 Targ 1: Code Block 1 Targ 2: Code Block 2 • • • Targn-1: Code Block n– 1 4

Seoul National University Switch Statement Example (IA 32) long switch_eg(long x, long y, long

Seoul National University Switch Statement Example (IA 32) long switch_eg(long x, long y, long z) { long w = 1; switch(x) {. . . } return w; } What range of values takes default? Setup: switch_eg: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax cmpl $6, %eax ja. L 2 jmp *. L 7(, %eax, 4) # # # Setup %eax = x Compare x: 6 If unsigned > goto default Goto *JTab[x] Note that w not initialized here 5

Seoul National University Switch Statement Example (IA 32) long switch_eg(long x, long y, long

Seoul National University Switch Statement Example (IA 32) long switch_eg(long x, long y, long z) { long w = 1; switch(x) {. . . } return w; } Setup: switch_eg: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax cmpl $6, %eax ja. L 2 Indirect jmp *. L 7(, %eax, 4) jump # # # Jump table. section. align 4. L 7: . long . rodata. L 2 #. L 3 #. L 4 #. L 5 #. L 2 #. L 6 # x x x x Setup eax = x Compare x: 6 If unsigned > goto default Goto *JTab[x] = = = = 0 1 2 3 4 5 6 6

Seoul National University Assembly Setup Explanation ¢ Table Structure § Each target requires 4

Seoul National University Assembly Setup Explanation ¢ Table Structure § Each target requires 4 bytes § Base address at. L 7 ¢ Jumping § Direct: jmp. L 2 § Jump target is denoted by label. L 2 § § Jump table. section. rodata. align 4. L 7: . long. L 2 # x = 0. long. L 3 # x = 1. long. L 4 # x = 2. long. L 5 # x = 3. long. L 2 # x = 4. long. L 6 # x = 5. long. L 6 # x = 6 Indirect: jmp *. L 7(, %eax, 4) Start of jump table: . L 7 Must scale by factor of 4 (labels have 32 -bits = 4 Bytes on IA 32) Fetch target from effective Address. L 7 + eax*4 § Only for 0 ≤ x ≤ 6 7

Seoul National University Jump Table Jump table. section. rodata. align 4. L 7: .

Seoul National University Jump Table Jump table. section. rodata. align 4. L 7: . long. L 2 # x = 0. long. L 3 # x = 1. long. L 4 # x = 2. long. L 5 # x = 3. long. L 2 # x = 4. long. L 6 # x = 5. long. L 6 # x = 6 switch(x) { case 1: //. L 3 w = y*z; break; case 2: //. L 4 w = y/z; /* Fall Through */ case 3: //. L 5 w += z; break; case 5: case 6: //. L 6 w -= z; break; default: //. L 2 w = 2; } 8

Seoul National University Handling Fall-Through long w = 1; . . . switch(x) {.

Seoul National University Handling Fall-Through long w = 1; . . . switch(x) {. . . case 2: w = y/z; /* Fall Through */ case 3: w += z; break; . . . } case 3: w = 1; goto merge; case 2: w = y/z; merge: w += z; 9

Seoul National University Code Blocks (Partial) switch(x) { case 1: //. L 3 w

Seoul National University Code Blocks (Partial) switch(x) { case 1: //. L 3 w = y*z; break; . . . case 3: //. L 5 w += z; break; . . . default: //. L 2 w = 2; } . L 2: # Default movl $2, %eax # w = 2 jmp. L 8 # Goto done. L 5: # x == 3 movl $1, %eax # w = 1 jmp. L 9 # Goto merge. L 3: movl 16(%ebp), imull 12(%ebp), jmp. L 8 # x == 1 %eax # z %eax # w = y*z # Goto done 10

Seoul National University Code Blocks (Rest) switch(x) {. . . case 2: //. L

Seoul National University Code Blocks (Rest) switch(x) {. . . case 2: //. L 4 w = y/z; /* Fall Through */ merge: //. L 9 w += z; break; case 5: case 6: //. L 6 w -= z; break; } . L 4: # x == 2 movl 12(%ebp), %edx movl %edx, %eax sarl $31, %edx idivl 16(%ebp) # w = y/z. L 9: # merge: addl 16(%ebp), %eax # w += z jmp. L 8 # goto done. L 6: # x == 5, 6 movl $1, %eax # w = 1 subl 16(%ebp), %eax # w = 1 -z 11

Seoul National University x 86 -64 Switch Implementation Same general idea, adapted to 64

Seoul National University x 86 -64 Switch Implementation Same general idea, adapted to 64 -bit code ¢ Table entries 64 bits (pointers) ¢ Cases use revised code ¢ Jump Table switch(x) { case 1: //. L 3 w = y*z; break; . . . }. L 3: movq imulq ret %rdx, %rax %rsi, %rax . section. align 8. L 7: . quad . rodata. L 2. L 3. L 4. L 5. L 2. L 6 # # # # x x x X x = = = = 0 1 2 3 4 5 6 13

Seoul National University Summary ¢ C Control § § ¢ Assembler Control § §

Seoul National University Summary ¢ C Control § § ¢ Assembler Control § § ¢ if-then-else do-while, for switch Conditional jump Conditional move Indirect jump Compiler generates code sequence to implement more complex control Standard Techniques § Loops converted to do-while form § Large switch statements use jump tables § Sparse switch statements may use decision trees 14

Seoul National University Outline Switch statements ¢ IA 32 Procedures ¢ § Stack Structure

Seoul National University Outline Switch statements ¢ IA 32 Procedures ¢ § Stack Structure § Calling Conventions 15

Seoul National University IA 32 Stack Region of memory managed with stack discipline ¢

Seoul National University IA 32 Stack Region of memory managed with stack discipline ¢ Grows toward lower addresses Stack “Bottom” ¢ ¢ Increasing Addresses Register %esp contains lowest stack address Stack Grows Down § address of “top” element Stack Pointer: %esp Stack “Top” 16

Seoul National University IA 32 Stack: Push ¢ Stack “Bottom” pushl Src § Fetch

Seoul National University IA 32 Stack: Push ¢ Stack “Bottom” pushl Src § Fetch operand at Src § Decrement %esp by 4 § Write operand at address given by %esp Stack Pointer: %esp Increasing Addresses Stack Grows Down -4 Stack “Top” 17

Seoul National University IA 32 Stack: Pop Stack “Bottom” Increasing Addresses Stack Pointer: %esp

Seoul National University IA 32 Stack: Pop Stack “Bottom” Increasing Addresses Stack Pointer: %esp +4 Stack Grows Down Stack “Top” 18

Seoul National University Procedure Control Flow Use stack to support procedure call and return

Seoul National University Procedure Control Flow Use stack to support procedure call and return ¢ Procedure call: call label ¢ § Push return address on stack § Jump to label ¢ Return address: § Address of the next instruction right after call § Example from disassembly 804854 e: 8048553: e 8 3 d 06 00 00 50 call pushl 8048 b 90 <main> %eax § Return address = 0 x 8048553 ¢ Procedure return: ret § Pop address from stack § Jump to address 19

Seoul National University Procedure Call Example 804854 e: 8048553: e 8 3 d 06

Seoul National University Procedure Call Example 804854 e: 8048553: e 8 3 d 06 00 00 50 call pushl 8048 b 90 <main> %eax call 8048 b 90 0 x 110 0 x 10 c 0 x 108 %esp 123 0 x 108 %eip 0 x 804854 e %eip: program counter 0 x 108 123 0 x 104 0 x 8048553 %esp 0 x 104 %eip 0 x 8048 b 90 20

Seoul National University Procedure Return Example 8048591: c 3 ret %eip: program counter 0

Seoul National University Procedure Return Example 8048591: c 3 ret %eip: program counter 0 x 110 0 x 10 c 0 x 108 123 0 x 104 0 x 8048553 %esp 0 x 104 %esp 0 x 108 %eip 0 x 8048591 %eip 0 x 8048553 21

Seoul National University Stack-Based Languages ¢ Languages that support recursion § e. g. ,

Seoul National University Stack-Based Languages ¢ Languages that support recursion § e. g. , C, Pascal, Java § Code must be “Reentrant” Multiple simultaneous instantiations of single procedure § Need some place to store state of each instantiation § Arguments § Local variables § Return pointer § ¢ Stack discipline § State for given procedure needed for limited time From when called to when return § Callee returns before caller does § ¢ Stack allocated in Frames § state for single procedure instantiation 22

Seoul National University Call Chain Example yoo(…) { • • who(); • • }

Seoul National University Call Chain Example yoo(…) { • • who(); • • } Example Call Chain yoo who(…) { • • • am. I(); • • • } who am. I(…) { • • am. I(); • • } am. I Procedure am. I() is recursive 23

Seoul National University Stack Frames ¢ Previous Frame Contents § Local variables § Return

Seoul National University Stack Frames ¢ Previous Frame Contents § Local variables § Return information § Temporary space Frame Pointer: %ebp Frame for proc Stack Pointer: %esp ¢ Management § Space allocated when enter procedure Stack “Top” “Set-up” code § Deallocated when return § “Finish” code § 24

Seoul National University Stack Example yoo(…) { • • who(); • • } yoo

Seoul National University Stack Example yoo(…) { • • who(); • • } yoo %ebp yoo who am. I %esp am. I 25

Seoul National University Stack Example yoo(…) { who(…) • { • • am. I();

Seoul National University Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %ebp am. I %esp who am. I 26

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • •

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %ebp am. I %esp am. I 27

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • •

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); am. I(…) • • { • • • am. I(); • • } • am. I(); • } yoo yoo who am. I %ebp am. I %esp 28

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • •

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); am. I(…) • • { • • • am. I(); • am. I(…) • • am. I(); • • { } • am. I(); • • • } • am. I(); • } yoo yoo who am. I %ebp am. I %esp 29

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • •

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); am. I(…) • • { • • • am. I(); • • } • am. I(); • } yoo yoo who am. I %ebp am. I %esp 30

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • •

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %ebp am. I %esp am. I 31

Seoul National University Stack Example yoo(…) { who(…) • { • • am. I();

Seoul National University Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %ebp am. I %esp who am. I 32

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • •

Seoul National University Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %ebp am. I %esp am. I 33

Seoul National University Stack Example yoo(…) { who(…) • { • • am. I();

Seoul National University Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %ebp am. I %esp who am. I 34

Seoul National University Stack Example yoo(…) { • • who(); • • } yoo

Seoul National University Stack Example yoo(…) { • • who(); • • } yoo %ebp yoo who am. I %esp am. I 35

Seoul National University IA 32/Linux Stack Frame ¢ Current Stack Frame (“Top” to Bottom)

Seoul National University IA 32/Linux Stack Frame ¢ Current Stack Frame (“Top” to Bottom) § “Argument build: ” Caller Frame Parameters for function about to call § Local variables If can’t keep in registers Frame pointer %ebp § Saved register context § Old frame pointer ¢ Caller Stack Frame § Return address Pushed by call instruction § Arguments for this call § Stack pointer %esp Arguments Return Addr Old %ebp Saved Registers + Local Variables Argument Build 36

Seoul National University Revisiting swap Calling swap from call_swap int course 1 = 15213;

Seoul National University Revisiting swap Calling swap from call_swap int course 1 = 15213; int course 2 = 18243; void call_swap() { swap(&course 1, &course 2); } void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } call_swap: • • • subl movl call • • • $8, %esp $course 2, 4(%esp) $course 1, (%esp) swap Resulting Stack %esp &course 2 subl &course 1 %esp Rtn adr %esp call 37

Seoul National University Revisiting swap void swap(int *xp, int *yp) { int t 0

Seoul National University Revisiting swap void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } swap: pushl %ebp movl %esp, %ebp pushl %ebx movl movl 8(%ebp), %edx 12(%ebp), %ecx (%edx), %ebx (%ecx), %eax, (%edx) %ebx, (%ecx) popl ret %ebx %ebp Set Up Body Finish 38

Seoul National University swap Setup #1 Entering Stack Resulting Stack %ebp • • •

Seoul National University swap Setup #1 Entering Stack Resulting Stack %ebp • • • &course 2 yp &course 1 xp Rtn adr %esp Rtn adr Old %ebp %esp swap: pushl %ebp movl %esp, %ebp pushl %ebx 39

Seoul National University swap Setup #2 Entering Stack Resulting Stack %ebp • • •

Seoul National University swap Setup #2 Entering Stack Resulting Stack %ebp • • • &course 2 yp &course 1 xp Rtn adr %esp Rtn adr Old %ebp %esp swap: pushl %ebp movl %esp, %ebp pushl %ebx 40

Seoul National University swap Setup #3 Entering Stack Resulting Stack %ebp • • •

Seoul National University swap Setup #3 Entering Stack Resulting Stack %ebp • • • &course 2 yp &course 1 xp Rtn adr %esp Rtn adr Old %ebp Old %ebx %esp swap: pushl %ebp movl %esp, %ebp pushl %ebx 41

Seoul National University swap Body Entering Stack Resulting Stack %ebp • • • Offset

Seoul National University swap Body Entering Stack Resulting Stack %ebp • • • Offset relative to %ebp • • • &course 2 12 yp &course 1 8 xp 4 Rtn adr %esp movl 8(%ebp), %edx movl 12(%ebp), %ecx. . . Old %ebp Old %ebx %esp # get xp # get yp 42

Seoul National University swap Finish Stack Before Finish Resulting Stack %ebp • • •

Seoul National University swap Finish Stack Before Finish Resulting Stack %ebp • • • popl %ebx %ebp • • • yp yp xp xp Rtn adr Old %ebp Old %ebx %esp ¢ %esp Observation § Saved and restored register %ebx § Not so for %eax, %ecx, %edx 43

Seoul National University Disassembled swap 08048384 <swap>: 8048384: 55 8048385: 89 e 5 8048387:

Seoul National University Disassembled swap 08048384 <swap>: 8048384: 55 8048385: 89 e 5 8048387: 53 8048388: 8 b 55 08 804838 b: 8 b 4 d 0 c 804838 e: 8 b 1 a 8048390: 8 b 01 8048392: 89 02 8048394: 89 19 8048396: 5 b 8048397: 5 d 8048398: c 3 push mov mov pop ret %ebp %esp, %ebp %ebx 0 x 8(%ebp), %edx 0 xc(%ebp), %ecx (%edx), %ebx (%ecx), %eax, (%edx) %ebx, (%ecx) %ebx %ebp Calling Code 80483 b 4: 80483 bc: 80483 c 3: 80483 c 8: 80483 c 9: movl call leave ret $0 x 8049658, 0 x 4(%esp) # $0 x 8049654, (%esp) # 8048384 <swap> # # # Copy &course 2 Copy &course 1 Call swap Prepare to return Return 44

Seoul National University Outline ¢ ¢ Switch statements IA 32 Procedures § Stack Structure

Seoul National University Outline ¢ ¢ Switch statements IA 32 Procedures § Stack Structure § Calling Conventions 45

Seoul National University Register Saving Conventions ¢ When procedure yoo calls who: § yoo

Seoul National University Register Saving Conventions ¢ When procedure yoo calls who: § yoo is the caller § who is the callee ¢ Can register be used for temporary storage? yoo: • • • movl $15213, %edx call who addl %edx, %eax • • • ret who: • • • movl 8(%ebp), %edx addl $18243, %edx • • • ret § Contents of register %edx overwritten by who § This could be trouble ➙ something should be done! § Need some coordination 46

Seoul National University Register Saving Conventions ¢ When procedure yoo calls who: § yoo

Seoul National University Register Saving Conventions ¢ When procedure yoo calls who: § yoo is the caller § who is the callee Can register be used for temporary storage? ¢ Conventions ¢ § “Caller Save” Caller saves temporary values in its frame before the call § “Callee Save” § Callee saves temporary values in its frame before using § 47

Seoul National University IA 32/Linux+Windows Register Usage ¢ %eax, %edx, %ecx § Caller saves

Seoul National University IA 32/Linux+Windows Register Usage ¢ %eax, %edx, %ecx § Caller saves prior to call if values are used later ¢ %eax § also used to return integer value ¢ Callee-Save Temporaries %ebx, %esi, %edi § Callee saves if wants to use them ¢ Caller-Save Temporaries Special %eax %edx %ecx %ebx %esi %edi %esp %ebp %esp, %ebp § special form of callee save § Restored to original values upon exit from procedure 48