


























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























































th
Stack Structure Calling Conven;ons Illustra;ons of Recursion & Pointers
Code Block 0
Code Block 1
Code Block 2
Code Block n–
Targ Targ Targ Targn-
target = JTab[x]; goto *target; switch(x) { case val_0: Block 0 case val_1: Block 1
long switch_eg(long x, long y, long z) { long w = 1; switch(x) {
... } return w; } switch_eg: pushl %ebp # Setup movl %esp, %ebp # Setup movl 8(%ebp), %eax # %eax = x cmpl $6, %eax # Compare x: ja .L2 # If unsigned > goto default jmp *.L7(,%eax,4) # Goto *JTab[x]
Each target requires 4 bytes Base address at .L
Direct: jmp .L Jump target is denoted by label .L Indirect: jmp .L7(,%eax,4) Start of jump table: .L Must scale by factor of 4 (labels have 32-‐bits = 4 Bytes on IA32) Fetch target from effec;ve Address .L7 + eax
.section .rodata .align 4 .L7: .long .L2 # x = 0 .long .L3 # x = 1 .long .L4 # x = 2 .long .L5 # x = 3 .long .L2 # x = 4 .long .L6 # x = 5 .long .L6 # x = 6
.section .rodata .align 4 .L7: .long .L2 # x = 0 .long .L3 # x = 1 .long .L4 # x = 2 .long .L5 # x = 3 .long .L2 # x = 4 .long .L6 # x = 5 .long .L6 # x = 6
switch(x) { case 1: // .L w = yz; break; case 2: // .L w = y/z; / Fall Through */ case 3: // .L w += z; break; case 5: case 6: // .L w -= z; break; default: // .L w = 2; }
.L2: # Default movl $2, %eax # w = 2 jmp .L8 # Goto done .L5: # x == 3 movl $1, %eax # w = 1 jmp .L9 # Goto merge .L3: # x == 1 movl 16(%ebp), %eax # z imull 12(%ebp), %eax # w = yz jmp .L8 # Goto done switch(x) { case 1: // .L w = yz; break;
... case 3: // .L w += z; break; ... default: // .L w = 2; }
.L4: # x == 2 movl 12(%ebp), %edx movl %edx, %eax sarl $31, %edx idivl 16(%ebp) # w = y/z .L9: # merge: addl 16(%ebp), %eax # w += z jmp .L8 # goto done .L6: # x == 5, 6 movl $1, %eax # w = 1 subl 16(%ebp), %eax # w = 1-z switch(x) {
... case 2: // .L w = y/z; /* Fall Through */ merge: // .L w += z; break; case 5: case 6: // .L w -= z; break; }
.section .rodata .align 8 .L7: .quad .L2 # x = 0 .quad .L3 # x = 1 .quad .L4 # x = 2 .quad .L5 # x = 3 .quad .L2 # x = 4 .quad .L6 # X = 5 .quad .L6 # x = 6
movq %rdx, %rax imulq %rsi, %rax ret switch(x) { case 1: // .L w = y*z; break;
... }
Label .L2 becomes address 0x Label .L7 becomes address 0x 08048410 <switch_eg>:
... 8048419: 77 07 ja 8048422 <switch_eg+0x12> 804841b: ff 24 85 60 86 04 08 jmp *0x8048660(,%eax,4) switch_eg: ... ja .L2 # If unsigned > goto default jmp *.L7(,%eax,4) # Goto *JTab[x]
0x8048660: 0x08048422 0x08048432 0x0804843b 0x 0x8048670: 0x08048422 0x0804844b 0x0804844b Address Value x 0x8048660 0x8048422 0 0x8048664 0x8048432 1 0x8048668 0x804843b 2 0x804866c 0x8048429 3 0x8048670 0x8048422 4 0x8048674 0x804844b 5 0x8048678 0x804844b 6
8048422: b8 02 00 00 00 mov $0x2,%eax 8048427: eb 2a jmp 8048453 <switch_eg+0x43> 8048429: b8 01 00 00 00 mov $0x1,%eax 804842e: 66 90 xchg %ax,%ax # noop 8048430: eb 14 jmp 8048446 <switch_eg+0x36> 8048432: 8b 45 10 mov 0x10(%ebp),%eax 8048435: 0f af 45 0c imul 0xc(%ebp),%eax 8048439: eb 18 jmp 8048453 <switch_eg+0x43> 804843b: 8b 55 0c mov 0xc(%ebp),%edx 804843e: 89 d0 mov %edx,%eax 8048440: c1 fa 1f sar $0x1f,%edx 8048443: f7 7d 10 idivl 0x10(%ebp) 8048446: 03 45 10 add 0x10(%ebp),%eax 8048449: eb 08 jmp 8048453 <switch_eg+0x43> 804844b: b8 01 00 00 00 mov $0x1,%eax 8048450: 2b 45 10 sub 0x10(%ebp),%eax 8048453: 5d pop %ebp 8048454: c3 ret
if-‐then-‐else do-‐while while, for switch
Condi;onal jump Condi;onal move Indirect jump Compiler generates code sequence to implement more complex control
Loops converted to do-‐while form Large switch statements use jump tables Sparse switch statements may use decision trees
Stack Structure Calling Conven;ons Illustra;ons of Recursion & Pointers