Carnegie Mellon Bryant and OHallaron Computer Systems A
Carnegie Mellon Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon Machine-Level Programming III: Procedures 15 -213/18 -213/14 -513/15 -513: Introduction to Computer Systems 7 th Lecture, September 18, 2018 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
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 3
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 § 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 § 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 § 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]; } 7
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 } 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
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” 17
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 18
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 19
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 20
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 21
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 22
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 23
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 24
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 25
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 26
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 27
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 28
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 29
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 30
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 31
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 32
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 33
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 34
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 35
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 36
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 37
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 38
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 39
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 40
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 41
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 42
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) 43
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 44
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 45
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 46
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 47
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 48
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 49
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 50
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 51
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 52
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 53
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 54
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 55
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 56
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 57
Carnegie Mellon Quiz Time! Check out: https: //canvas. cmu. edu/courses/5835 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 58
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 59
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; } 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 %rsp Resulting Stack Structure. . . Rtn address Saved %rbx %rsp 60
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 61
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 62
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 63
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 64
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 65
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 66
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 68
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 69
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 70
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 71
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 72
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 73
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 74
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 75
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 76
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 77
Carnegie Mellon Small Exercise long add 5(long b 0, long b 1, long b 2, long b 3, long b 4) { return b 0+b 1+b 2+b 3+b 4; } long add 10(long a 0, long a 1, long a 2, long a 3, long a 4, long a 5, long a 6, long a 7, long a 8, long a 9) { return add 5(a 0, a 1, a 2, a 3, a 4)+ add 5(a 5, a 6, a 7, a 8, a 9); } ¢ ¢ ¢ Where a 0, …, a 9 passed? rdi, rsi, rdx, rcx, r 8, r 9, stack Where are b 0, …, b 4 passed? rdi, rsi, rdx, rcx, r 8 Which registers do we need to save? Ill-posed question. Need assembly. rbx, rbp, r 9 (during first call to add 5) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 78
Carnegie Mellon Small Exercise long add 5(long b 0, long b 1, long b 2, long b 3, long b 4) { return b 0+b 1+b 2+b 3+b 4; } long add 10(long a 0, long a 1, long a 2, long a 3, long a 4, long a 5, long a 6, long a 7, long a 8, long a 9) { return add 5(a 0, a 1, a 2, a 3, a 4)+ add 5(a 5, a 6, a 7, a 8, a 9); } add 10: pushq movq call movq movq call addq popq ret %rbp %rbx %r 9, %rbp add 5 %rax, %rbx 48(%rsp), %r 8 40(%rsp), %rcx 32(%rsp), %rdx 24(%rsp), %rsi %rbp, %rdi add 5 %rbx, %rax %rbp add 5: addq leaq ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsi, %rdi, %rdx, %rcx (%rcx, %r 8), %rax 79
- Slides: 78