Carnegie Mellon MachineLevel Programming III Procedures 15 21318

  • Slides: 89
Download presentation
Carnegie Mellon Machine-Level Programming III: Procedures 15 -213/18 -213/14 -513/15 -513: Introduction to Computer

Carnegie Mellon Machine-Level Programming III: Procedures 15 -213/18 -213/14 -513/15 -513: Introduction to Computer Systems 7 th Lecture, February 4 th 2020 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing control § Passing data § Managing local data § Illustration of Recursion § ¢ Finish Up Control Flow § Switch Statement Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code § Back to return point ¢ Passing data § Procedure arguments § Return value ¢ Memory management § Allocate during procedure execution § Deallocate upon return Mechanisms all implemented with machine instructions ¢ x 86 -64 implementation of a procedure uses only those mechanisms required ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition P(…) { • • y = Q(x); print(y) • } int Q(int i) { int t = 3*i; int v[10]; • • return v[t]; } 3

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code § Back to return point ¢ Passing data § Procedure arguments § Return value ¢ Memory management § Allocate during procedure execution § Deallocate upon return Mechanisms all implemented with machine instructions ¢ x 86 -64 implementation of a procedure uses only those mechanisms required ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition P(…) { • • y = Q(x); print(y) • } int Q(int i) { int t = 3*i; int v[10]; • • return v[t]; } 4

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code § Back to return point ¢ Passing data § Procedure arguments § Return value ¢ Memory management § Allocate during procedure execution § Deallocate upon return Mechanisms all implemented with machine instructions ¢ x 86 -64 implementation of a procedure uses only those mechanisms required ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition P(…) { • • y = Q(x); print(y) • } int Q(int i) { int t = 3*i; int v[10]; • • return v[t]; } 5

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code

Carnegie Mellon Mechanisms in Procedures ¢ Passing control § To beginning of procedure code § Back to return point ¢ Passing data § Procedure arguments § Return value ¢ Memory management § Allocate during procedure execution § Deallocate upon return Mechanisms all implemented with machine instructions ¢ x 86 -64 implementation of a procedure uses only those mechanisms required ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition P(…) { • • y = Q(x); print(y) • } int Q(int i) { int t = 3*i; int v[10]; • • return v[t]; } 6

Carnegie Mellon Mechanisms in Procedures ¢ Passing control P(…) { • • y =

Carnegie Mellon Mechanisms in Procedures ¢ Passing control P(…) { • • y = Q(x); print(y) • } Machine instructions implement the mechanisms, but the choices are ¢ Passing data determined by designers. These choices § Procedure arguments §make Return value up the Application Binary Interface int Q(int i) ¢ Memory management (ABI). { § To beginning of procedure code § Back to return point § Allocate during procedure execution § Deallocate upon return Mechanisms all implemented with machine instructions ¢ x 86 -64 implementation of a procedure uses only those mechanisms required int t = 3*i; int v[10]; • • return v[t]; ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition } 7

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing control § Passing data § Managing local data § Illustration of Recursion § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8

Carnegie Mellon x 86 -64 Stack ¢ Region of memory managed with stack discipline

Carnegie Mellon x 86 -64 Stack ¢ Region of memory managed with stack discipline § § § Memory viewed as array of bytes. Different regions have different purposes. (Like ABI, a policy decision) stack code Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition m e m o r y 9

Carnegie Mellon x 86 -64 Stack ¢ Region of memory managed with stack discipline

Carnegie Mellon x 86 -64 Stack ¢ Region of memory managed with stack discipline Stack “Bottom” stack code Stack Pointer: %rsp Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10

Carnegie Mellon x 86 -64 Stack Region of memory managed with stack discipline ¢

Carnegie Mellon x 86 -64 Stack Region of memory managed with stack discipline ¢ Grows toward lower addresses ¢ ¢ Stack “Bottom” Increasing Addresses Register %rsp contains lowest stack address § address of “top” element Stack Grows Down Stack Pointer: %rsp Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11

Carnegie Mellon x 86 -64 Stack: Push ¢ pushq Src § Fetch operand at

Carnegie Mellon x 86 -64 Stack: Push ¢ pushq Src § Fetch operand at Src val § Decrement %rsp by 8 § Write operand at address given by %rsp Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer: %rsp Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12

Carnegie Mellon x 86 -64 Stack: Push ¢ pushq Src § Fetch operand at

Carnegie Mellon x 86 -64 Stack: Push ¢ pushq Src § Fetch operand at Src val § Decrement %rsp by 8 § Write operand at address given by %rsp Stack Pointer: %rsp Stack “Bottom” Increasing Addresses Stack Grows Down -8 Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13

Carnegie Mellon x 86 -64 Stack: Pop ¢ popq Dest § Read value at

Carnegie Mellon x 86 -64 Stack: Pop ¢ popq Dest § Read value at address given by %rsp § Increment %rsp by 8 § Store value at Dest (usually a register) Stack Pointer: %rsp Stack “Bottom” Increasing Addresses Stack Grows Down val Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14

Carnegie Mellon x 86 -64 Stack: Pop ¢ popq Dest § Read value at

Carnegie Mellon x 86 -64 Stack: Pop ¢ popq Dest § Read value at address given by %rsp § Increment %rsp by 8 § Store value at Dest (usually a register) Stack “Bottom” Increasing Addresses val Stack Pointer: %rsp Stack Grows Down +8 Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15

Carnegie Mellon x 86 -64 Stack: Pop ¢ popq Dest § Read value at

Carnegie Mellon x 86 -64 Stack: Pop ¢ popq Dest § Read value at address given by %rsp § Increment %rsp by 8 § Store value at Dest (usually a register) Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer: %rsp val (The memory doesn’t change, only the value of %rsp) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Stack “Top” 16

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing control § Passing data § Managing local data § Illustration of Recursion § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17

Carnegie Mellon Code Examples void multstore(long x, long y, long *dest) { long t

Carnegie Mellon Code Examples void multstore(long x, long y, long *dest) { long t = mult 2(x, y); *dest = t; } 00000400540 <multstore>: 400540: 400541: 400544: 400549: 40054 c: 40054 d: push mov callq mov pop retq %rbx %rdx, %rbx 400550 <mult 2> %rax, (%rbx) %rbx long mult 2(long a, long b) { 00000400550 <mult 2>: long s = a * b; 400550: mov %rdi, %rax return s; 400553: imul %rsi, %rax } 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition # # # Save %rbx Save dest mult 2(x, y) Save at dest Restore %rbx Return # a * b # Return 18

Carnegie Mellon Procedure Control Flow Use stack to support procedure call and return ¢

Carnegie Mellon 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 ¢ Procedure return: ret § Pop address from stack § Jump to address Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19

Carnegie Mellon Control Flow Example #1 00000400540 <multstore>: • • 400544: callq 400550 <mult

Carnegie Mellon Control Flow Example #1 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 %rsp 0 x 120 %rip 0 x 400544 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20

Carnegie Mellon Control Flow Example #2 00000400540 <multstore>: • • 400544: callq 400550 <mult

Carnegie Mellon Control Flow Example #2 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 0 x 118 0 x 400549 %rsp 0 x 118 %rip 0 x 400550 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 21

Carnegie Mellon Control Flow Example #3 00000400540 <multstore>: • • 400544: callq 400550 <mult

Carnegie Mellon Control Flow Example #3 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 0 x 118 0 x 400549 %rsp 0 x 118 %rip 0 x 400557 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 22

Carnegie Mellon Control Flow Example #4 00000400540 <multstore>: • • 400544: callq 400550 <mult

Carnegie Mellon Control Flow Example #4 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 %rsp 0 x 120 %rip 0 x 400549 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23

Carnegie Mellon Today ¢ Procedures § Mechanisms § tack Structure § Calling Conventions Passing

Carnegie Mellon Today ¢ Procedures § Mechanisms § tack Structure § Calling Conventions Passing control § Passing data § Managing local data § Illustrations of Recursion & Pointers § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 24

Carnegie Mellon Procedure Data Flow Registers ¢ First 6 arguments Stack • • •

Carnegie Mellon Procedure Data Flow Registers ¢ First 6 arguments Stack • • • %rdi %rsi Arg n %rdx • • • %rcx ¢ %r 8 Arg 8 %r 9 Arg 7 Return value %rax Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ¢ Only allocate stack space when needed 25

Carnegie Mellon void multstore (long x, long y, long *dest) { long t =

Carnegie Mellon void multstore (long x, long y, long *dest) { long t = mult 2(x, y); *dest = t; } Data Flow Examples 00000400540 # x in %rdi, y • • • 400541: mov 400544: callq # t in %rax 400549: mov • • • long mult 2 (long a, long b) { long s = a * b; return s; } <multstore>: in %rsi, dest in %rdx, %rbx 400550 <mult 2> # Save dest # mult 2(x, y) %rax, (%rbx) # Save at dest 00000400550 <mult 2>: # a in %rdi, b in %rsi 400550: mov %rdi, %rax 400553: imul %rsi, %rax # s in %rax 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition # a * b # Return 26

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing control § Passing data § Managing local data § Illustration of Recursion § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 27

Carnegie Mellon Stack-Based Languages ¢ Languages that support recursion § e. g. , C,

Carnegie Mellon 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 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 28

Carnegie Mellon Call Chain Example yoo(…) { • • who(); • • } Example

Carnegie Mellon Call Chain Example yoo(…) { • • who(); • • } Example Call Chain yoo who(…) { • • • am. I(); • • • } who am. I(…) { • • am. I(); • • } am. I Procedure am. I() is recursive Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29

Carnegie Mellon Stack Frames ¢ Previous Frame Contents § Return information § Local storage

Carnegie Mellon Stack Frames ¢ Previous Frame Contents § Return information § Local storage (if needed) § Temporary space (if needed) Frame Pointer: %rbp (Optional) x Frame for proc Stack Pointer: %rsp ¢ Management § Space allocated when enter procedure Stack “Top” “Set-up” code § Includes push by call instruction § § Deallocated when return “Finish” code § Includes pop by ret instruction § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 30

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo %rbp

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo %rbp yoo who am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who();

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %rbp am. I %rsp who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %rbp am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); am. I(…) • • { • • • am. I(); • • } • am. I(); • } yoo yoo who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition am. I %rbp am. I %rsp 34

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon 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 %rbp am. I %rsp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); am. I(…) • • { • • • am. I(); • • } • am. I(); • } yoo yoo who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition am. I %rbp am. I %rsp 36

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %rbp am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who();

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %rbp am. I %rsp who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 38

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %rbp am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 39

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who();

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %rbp am. I %rsp who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 40

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo %rbp

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo %rbp yoo who am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 41

Carnegie Mellon x 86 -64/Linux Stack Frame ¢ Current Stack Frame (“Top” to Bottom)

Carnegie Mellon x 86 -64/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 %rbp § Saved register context (Optional) § Old frame pointer (optional) ¢ Caller Stack Frame § Return address Pushed by call instruction § Arguments for this call § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Stack pointer %rsp Arguments 7+ Return Addr Old %rbp Saved Registers + Local Variables Argument Build (Optional) 42

Carnegie Mellon Example: incr long incr(long *p, long val) { long x = *p;

Carnegie Mellon Example: incr long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x; } incr: movq addq movq ret (%rdi), %rax, %rsi, (%rdi) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Register Use(s) %rdi Argument p %rsi Argument val, y %rax x, Return value 43

Carnegie Mellon Example: Calling incr #1 Initial Stack Structure long call_incr() { long v

Carnegie Mellon Example: Calling incr #1 Initial Stack Structure long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret . . . Rtn address Resulting Stack Structure. . . Rtn address 15213 Unused Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp+8 %rsp 44

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213;

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } Stack Structure. . . Rtn address %rsp+8 15213 call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp Unused Register Use(s) %rdi &v 1 %rsi 3000 45

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213;

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } Stack Structure. . . Rtn address %rsp+8 15213 Aside 1: movl $3000, Unused %esi %rsp call_incr: • Note: movl -> %exx zeros out high order 32 bits. subq $16, %rsp • Why use movl instead of movq? Register 1 byte shorter. movq $15213, 8(%rsp) Use(s) movl leaq call addq ret $3000, %esi 8(%rsp), %rdi incr 8(%rsp), %rax $16, %rsp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rdi &v 1 %rsi 3000 46

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213;

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } Stack Structure. . . Rtn address %rsp+8 15213 %rsp Unused call_incr: Aside 2: leaq 8(%rsp), %rdi subq $16, %rsp • Computes %rsp+8 movq $15213, 8(%rsp) Register • Actually, used %esi for what it is meant! %rdi movl $3000, leaq 8(%rsp), %rdi %rsi call incr addq 8(%rsp), %rax addq $16, %rsp ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Use(s) &v 1 3000 47

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213;

Carnegie Mellon Example: Calling incr #2 long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } Stack Structure. . . Rtn address %rsp+8 15213 call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp Unused Register Use(s) %rdi &v 1 %rsi 3000 48

Carnegie Mellon Example: Calling incr #3 long call_incr() { long v 1 = 15213;

Carnegie Mellon Example: Calling incr #3 long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } Stack Structure. . . Rtn address %rsp+8 18213 call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp Unused Register Use(s) %rdi &v 1 %rsi 3000 49

Carnegie Mellon Example: Calling incr #4 long call_incr() { long v 1 = 15213;

Carnegie Mellon Example: Calling incr #4 long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } Stack Structure . . . Rtn address 18213 Unused call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp+8 %rsp Register Use(s) %rax Return value 50

Carnegie Mellon Example: Calling incr #5 a Stack Structure long call_incr() { long v

Carnegie Mellon Example: Calling incr #5 a Stack Structure long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } . . . Rtn address 18213 Unused call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret %rsp Register Use(s) %rax Return value Updated Stack Structure. . . Rtn address Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp+8 %rsp 51

Carnegie Mellon Example: Calling incr #5 b long call_incr() { long v 1 =

Carnegie Mellon Example: Calling incr #5 b long call_incr() { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return v 1+v 2; } call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Updated Stack Structure. . . Rtn address %rsp Register Use(s) %rax Return value Final Stack Structure. . . %rsp 52

Carnegie Mellon Register Saving Conventions ¢ When procedure yoo calls who: § yoo is

Carnegie Mellon Register Saving Conventions ¢ When procedure yoo calls who: § yoo is the caller § who is the callee ¢ Can register be used for temporary storage? yoo: • • • movq $15213, %rdx call who addq %rdx, %rax • • • ret who: • • • subq $18213, %rdx • • • ret § Contents of register %rdx overwritten by who § This could be trouble ➙ something should be done! § Need some coordination Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 53

Carnegie Mellon Register Saving Conventions ¢ When procedure yoo calls who: § yoo is

Carnegie Mellon 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 Saved” Caller saves temporary values in its frame before the call § “Callee Saved” § Callee saves temporary values in its frame before using § Callee restores them before returning to caller § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 54

Carnegie Mellon x 86 -64 Linux Register Usage #1 ¢ %rax § Return value

Carnegie Mellon x 86 -64 Linux Register Usage #1 ¢ %rax § Return value § Also caller-saved § Can be modified by procedure ¢ Return value %rdi, . . . , %r 9 Arguments § Also caller-saved § Can be modified by procedure ¢ %r 10, %r 11 § Caller-saved § Can be modified by procedure Caller-saved temporaries Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rax %rdi %rsi %rdx %rcx %r 8 %r 9 %r 10 %r 11 55

Carnegie Mellon x 86 -64 Linux Register Usage #2 ¢ %rbx, %r 12, %r

Carnegie Mellon x 86 -64 Linux Register Usage #2 ¢ %rbx, %r 12, %r 13, %r 14 § Callee-saved § Callee must save & restore ¢ %rbp § § ¢ Callee-saved Temporaries Callee-saved Callee must save & restore May be used as frame pointer Can mix & match Special %rbx %r 12 %r 13 %r 14 %rbp %rsp § Special form of callee save § Restored to original value upon exit from procedure Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 56

Carnegie Mellon Quiz Time! Check out: https: //canvas. cmu. edu/courses/13182 Bryant and O’Hallaron, Computer

Carnegie Mellon Quiz Time! Check out: https: //canvas. cmu. edu/courses/13182 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 57

Carnegie Mellon Callee-Saved Example #1 Initial Stack Structure long call_incr 2(long x) { long

Carnegie Mellon Callee-Saved Example #1 Initial Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } . . . Rtn address %rsp • X comes in register %rdi. • We need %rdi for the call to incr. • Where should be put x, so we can use it after the call to incr? Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 58

Carnegie Mellon Callee-Saved Example #2 Initial Stack Structure long call_incr 2(long x) { long

Carnegie Mellon Callee-Saved Example #2 Initial Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } Overwritten: call_incr 2: Need to save pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address %rsp Resulting Stack Structure. . . Rtn address Saved %rbx %rsp 59

Carnegie Mellon Callee-Saved Example #3 Initial Stack Structure long call_incr 2(long x) { long

Carnegie Mellon Callee-Saved Example #3 Initial Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } call_incr 2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address Saved %rbx %rsp Resulting Stack Structure. . . Rtn address Saved %rbx %rsp+8 %rsp 60

Carnegie Mellon Callee-Saved Example #4 Stack Structure long call_incr 2(long x) { long v

Carnegie Mellon Callee-Saved Example #4 Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } call_incr 2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address Saved %rbx %rsp+8 %rsp • x is saved in %rbx, a callee saved register 61

Carnegie Mellon Callee-Saved Example #5 Stack Structure long call_incr 2(long x) { long v

Carnegie Mellon Callee-Saved Example #5 Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } call_incr 2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address Saved %rbx 15213 Unused %rsp+8 %rsp • x is saved in %rbx, a callee saved register 62

Carnegie Mellon Callee-Saved Example #6 Stack Structure long call_incr 2(long x) { long v

Carnegie Mellon Callee-Saved Example #6 Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } call_incr 2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address Saved %rbx 18213 Unused %rsp+8 %rsp Upon return from incr: • x is safe in %rbx • Return result v 2 is in %rax • Compute x+v 2 63

Carnegie Mellon Callee-Saved Example #7 Stack Structure long call_incr 2(long x) { long v

Carnegie Mellon Callee-Saved Example #7 Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } call_incr 2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address Saved %rbx 18213 %rsp Unused • Return result in %rax 64

Carnegie Mellon Callee-Saved Example #8 Initial Stack Structure long call_incr 2(long x) { long

Carnegie Mellon Callee-Saved Example #8 Initial Stack Structure long call_incr 2(long x) { long v 1 = 15213; long v 2 = incr(&v 1, 3000); return x+v 2; } call_incr 2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition . . . Rtn address Saved %rbx 18213 %rsp Unused final Stack Structure. . . Rtn address Saved %rbx 18213 Unused %rsp 65

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing

Carnegie Mellon Today ¢ Procedures § Mechanisms § Stack Structure § Calling Conventions Passing control § Passing data § Managing local data § Illustration of Recursion § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 67

Carnegie Mellon Recursive Function /* Recursive popcount */ long pcount_r(unsigned long x) { if

Carnegie Mellon Recursive Function /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret 68

Carnegie Mellon Recursive Function Terminal Case /* Recursive popcount */ long pcount_r(unsigned long x)

Carnegie Mellon Recursive Function Terminal Case /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Register Use(s) Type %rdi x Argument %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret 69

Carnegie Mellon Recursive Function Register Save /* Recursive popcount */ long pcount_r(unsigned long x)

Carnegie Mellon Recursive Function Register Save /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Register Use(s) Type %rdi x Argument Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret . . . Rtn address Saved %rbx %rsp 70

Carnegie Mellon Recursive Function Call Setup /* Recursive popcount */ long pcount_r(unsigned long x)

Carnegie Mellon Recursive Function Call Setup /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Register Use(s) Type %rdi x >> 1 Recursive argument %rbx x & 1 Callee-saved Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret 71

Carnegie Mellon Recursive Function Call /* Recursive popcount */ long pcount_r(unsigned long x) {

Carnegie Mellon Recursive Function Call /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Register Use(s) Type %rbx x & 1 Callee-saved %rax Recursive call return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret 72

Carnegie Mellon Recursive Function Result /* Recursive popcount */ long pcount_r(unsigned long x) {

Carnegie Mellon Recursive Function Result /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Register Use(s) Type %rbx x & 1 Callee-saved %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret 73

Carnegie Mellon Recursive Function Completion /* Recursive popcount */ long pcount_r(unsigned long x) {

Carnegie Mellon Recursive Function Completion /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } Register Use(s) Type %rax Return value pcount_r: movl $0, %eax testq %rdi, %rdi je. L 6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx. L 6: rep; ret . . . Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp 74

Carnegie Mellon Observations About Recursion ¢ Handled Without Special Consideration § Stack frames mean

Carnegie Mellon Observations About Recursion ¢ Handled Without Special Consideration § Stack frames mean that each function call has private storage Saved registers & local variables § Saved return pointer § Register saving conventions prevent one function call from corrupting another’s data § Unless the C code explicitly does so (e. g. , buffer overflow in Lecture 9) § Stack discipline follows call / return pattern § If P calls Q, then Q returns before P § Last-In, First-Out § ¢ Also works for mutual recursion § P calls Q; Q calls P Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 75

Carnegie Mellon x 86 -64 Procedure Summary ¢ Important Points § Stack is the

Carnegie Mellon x 86 -64 Procedure Summary ¢ Important Points § Stack is the right data structure for procedure call/return § ¢ If P calls Q, then Q returns before P Recursion (& mutual recursion) handled by normal calling conventions § Can safely store values in local stack frame and in callee-saved registers § Put function arguments at top of stack § Result return in %rax ¢ Caller Frame %rbp (Optional) Pointers are addresses of values § On stack or global %rsp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Arguments 7+ Return Addr Old %rbp Saved Registers + Local Variables Argument Build 76

Carnegie Mellon long my_switch (long x, long y, long z) { long w =

Carnegie Mellon long my_switch (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; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Switch Statement Example ¢ Multiple case labels § Here: 5 & 6 ¢ Fall through cases § Here: 2 ¢ Missing cases § Here: 4 77

Carnegie Mellon Jump Table Structure Switch Form switch(x) { case val_0: Block 0 case

Carnegie Mellon Jump Table Structure Switch Form switch(x) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n– 1 } Jump Table jtab: Targ 0 Targ 1 Targ 2 • • • Targn-1 Translation (Extended C) goto *JTab[x]; Jump Targets Targ 0: Code Block 0 Targ 1: Code Block 1 Targ 2: Code Block 2 • • • Targn-1: Code Block n– 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 78

Carnegie Mellon Switch Statement Example long my_switch(long x, long y, long z) { long

Carnegie Mellon Switch Statement Example long my_switch(long x, long y, long z) { long w = 1; switch(x) {. . . } return w; } Setup my_switch: movq cmpq ja jmp %rdx, %rcx $6, %rdi # x: 6. L 8 *. L 4(, %rdi, 8) What range of values takes Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax Return value Note that w not initialized here 79

Carnegie Mellon Switch Statement Example long my_switch(long x, long y, long z) { long

Carnegie Mellon Switch Statement Example long my_switch(long x, long y, long z) { long w = 1; switch(x) {. . . } return w; } Setup my_switch: movq cmpq ja jmp %rdx, %rcx $6, %rdi # x: 6. L 8 # use default *. L 4(, %rdi, 8) # goto *Jtab[x] Jump table. section. rodata. align 8. L 4: . quad. L 8 # x = 0. quad. L 3 # x = 1. quad. L 5 # x = 2. quad. L 9 # x = 3. quad. L 8 # x = 4. quad. L 7 # x = 5. quad. L 7 # x = 6 Indirect jump Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 80

Carnegie Mellon Assembly Setup Explanation ¢ Table Structure § Each target requires 8 bytes

Carnegie Mellon Assembly Setup Explanation ¢ Table Structure § Each target requires 8 bytes § Base address at. L 4 ¢ Jumping § Direct: jmp. L 8 § Jump target is denoted by label. L 8 § § Jump table. section. rodata. align 8. L 4: . quad. L 8 # x = 0. quad. L 3 # x = 1. quad. L 5 # x = 2. quad. L 9 # x = 3. quad. L 8 # x = 4. quad. L 7 # x = 5. quad. L 7 # x = 6 Indirect: jmp *. L 4(, %rdi, 8) Start of jump table: . L 4 Must scale by factor of 8 (addresses are 8 bytes) Fetch target from effective Address. L 4 + x*8 § Only for 0 ≤ x ≤ 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 81

Carnegie Mellon Jump Table Jump table. section. rodata. align 8. L 4: . quad.

Carnegie Mellon Jump Table Jump table. section. rodata. align 8. L 4: . quad. L 8 # x = 0. quad. L 3 # x = 1. quad. L 5 # x = 2. quad. L 9 # x = 3. quad. L 8 # x = 4. quad. L 7 # x = 5. quad. L 7 # x = 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition switch(x) { case 1: //. L 3 w = y*z; break; case 2: //. L 5 w = y/z; /* Fall Through */ case 3: //. L 9 w += z; break; case 5: case 6: //. L 7 w -= z; break; default: //. L 8 w = 2; } 82

Carnegie Mellon Code Blocks (x == 1) switch(x) { case 1: //. L 3

Carnegie Mellon Code Blocks (x == 1) switch(x) { case 1: //. L 3 w = y*z; break; . . L 3: movq imulq ret %rsi, %rax %rdx, %rax # y*z } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 83

Carnegie Mellon Handling Fall-Through long w = 1; . . . switch(x) {. .

Carnegie Mellon Handling Fall-Through long w = 1; . . . switch(x) {. . . case 2: w = y/z; /* Fall Through */ case 3: w += z; break; . . . } case 2: w = y/z; goto merge; case 3: w = 1; merge: w += z; Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 84

Carnegie Mellon Code Blocks (x == 2, x == 3). L 5: movq cqto

Carnegie Mellon Code Blocks (x == 2, x == 3). L 5: movq cqto long w = 1; . . . switch(x) {. . . idivq case 2: jmp w = y/z; . L 9: /* Fall Through */ movl case 3: . L 6: w += z; addq break; ret. . . } Register # Case 2 %rsi, %rax # # %rcx. L 6 sign extend rax to rdx: rax # y/z # goto merge # Case 3 $1, %eax # w = 1 # merge: %rcx, %rax # w += z Use(s) %rdi Argument x %rsi Argument y %rcx z %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 85

Carnegie Mellon Code Blocks (x == 5, x == 6, default) switch(x) {. .

Carnegie Mellon Code Blocks (x == 5, x == 6, default) switch(x) {. . . case 5: //. L 7 case 6: //. L 7 w -= z; break; default: //. L 8 w = 2; . L 7: movl subq ret. L 8: movl ret # Case 5, 6 $1, %eax # w = 1 %rdx, %rax # w -= z $2, %eax # Default: # 2 } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 86

Carnegie Mellon Summarizing ¢ C Control § § ¢ Assembler Control § § ¢

Carnegie Mellon Summarizing ¢ C Control § § ¢ Assembler Control § § ¢ if-then-else do-while, for switch Conditional jump Conditional move Indirect jump (via jump tables) Compiler generates code sequence to implement more complex control Standard Techniques § Loops converted to do-while or jump-to-middle form § Large switch statements use jump tables § Sparse switch statements may use decision trees (if-elseif-else) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 87

Carnegie Mellon Finding Jump Table in Binary 000004005 e 0 <switch_eg>: 4005 e 0:

Carnegie Mellon Finding Jump Table in Binary 000004005 e 0 <switch_eg>: 4005 e 0: 48 89 d 1 4005 e 3: 48 83 ff 06 4005 e 7: 77 2 b 4005 e 9: ff 24 fd f 0 07 40 00 4005 f 0: 48 89 f 0 4005 f 3: 48 0 f af c 2 4005 f 7: c 3 4005 f 8: 48 89 f 0 4005 fb: 48 99 4005 fd: 48 f 7 f 9 400600: eb 05 400602: b 8 01 00 00 00 400607: 48 01 c 8 40060 a: c 3 40060 b: b 8 01 00 00 00 400610: 48 29 d 0 400613: c 3 400614: b 8 02 00 00 00 400619: c 3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition mov cmp ja jmpq mov imul retq mov cqto idiv jmp mov add retq mov sub retq mov retq %rdx, %rcx $0 x 6, %rdi 400614 <switch_eg+0 x 34> *0 x 4007 f 0(, %rdi, 8) %rsi, %rax %rdx, %rax %rsi, %rax %rcx 400607 <switch_eg+0 x 27> $0 x 1, %eax %rcx, %rax $0 x 1, %eax %rdx, %rax $0 x 2, %eax 89

Carnegie Mellon Finding Jump Table in Binary (cont. ) 000004005 e 0 <switch_eg>: .

Carnegie Mellon Finding Jump Table in Binary (cont. ) 000004005 e 0 <switch_eg>: . . . 4005 e 9: ff 24 fd f 0 07 40 00. . . % gdb switch (gdb) x /8 xg 0 x 4007 f 0: 0 x 00000400614 0 x 400800: 0 x 000004005 f 8 0 x 400810: 0 x 00000400614 0 x 400820: 0 x 0000040060 b (gdb) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition jmpq *0 x 4007 f 0(, %rdi, 8) 0 x 000004005 f 0 0 x 00000400602 0 x 0000040060 b 0 x 2 c 646 c 25203 d 2078 90

Carnegie Mellon Finding Jump Table in Binary (cont. ) % gdb switch (gdb) x

Carnegie Mellon Finding Jump Table in Binary (cont. ) % gdb switch (gdb) x /8 xg 0 x 4007 f 0: 0 x 00000400614 0 x 400800: 0 x 000004005 f 8 0 x 400810: 0 x 00000400614 0 x 400820: 0 x 0000040060 b. . . 4005 f 0: 4005 f 3: 4005 f 7: 4005 f 8: 4005 fb: 4005 fd: 400600: 400602: 400607: 40060 a: 40060 b: 400610: 400613: 400614: 400619: 48 48 c 3 48 48 48 eb b 8 48 c 3 b 8 c 3 89 f 0 0 f af c 2 89 99 f 7 05 01 01 f 0 f 9 00 00 00 c 8 01 00 00 00 29 d 0 02 00 00 00 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 0 x 000004005 f 0 0 x 00000400602 0 x 0000040060 b 0 x 2 c 646 c 25203 d 2078 mov imul retq mov cqto idiv jmp mov add retq mov sub retq mov retq %rsi, %rax %rdx, %rax %rsi, %rax %rcx 400607 <switch_eg+0 x 27> $0 x 1, %eax %rcx, %rax $0 x 1, %eax %rdx, %rax $0 x 2, %eax 91