MachineLevel Programming III Procedures 15 21318 213 Introduction

  • Slides: 73
Download presentation
Machine-Level Programming III: Procedures 15 -213/18 -213: Introduction to Computer Systems 7 th Lecture,

Machine-Level Programming III: Procedures 15 -213/18 -213: Introduction to Computer Systems 7 th Lecture, September 20, 2016 Instructor: Phil Gibbons Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

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

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]; } 2

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

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

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

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

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

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

Mechanisms in Procedures ¢ Passing control P(…) { • • y = Q(x); print(y)

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 } 6

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

Carnegie Mellon Today ¢ Procedures § 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 7

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 • Memory viewed as array of bytes. • Different regions have different purposes. • (Like ABI, a policy decision) code Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8

Carnegie Mellon x 86 -64 Stack “Bottom” ¢ Region of memory managed with stack

Carnegie Mellon x 86 -64 Stack “Bottom” ¢ Region of memory managed with stack discipline stack code Stack Pointer: %rsp Stack “Top” 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 ¢

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 Stack Grows Down § address of “top” element Stack Pointer: %rsp Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10

Carnegie Mellon x 86 -64 Stack: Push ¢ Stack “Bottom” pushq Src § Fetch

Carnegie Mellon x 86 -64 Stack: Push ¢ Stack “Bottom” pushq Src § Fetch operand at Src val § Decrement %rsp by 8 § Write operand at address given by %rsp Increasing Addresses 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 ¢ Stack “Bottom” pushq Src § Fetch

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

Carnegie Mellon x 86 -64 Stack: Pop ¢ Stack “Bottom” popq Dest § Read

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

Carnegie Mellon x 86 -64 Stack: Pop ¢ Stack “Bottom” popq Dest § Read

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

Carnegie Mellon x 86 -64 Stack: Pop ¢ Stack “Bottom” popq Dest § Read

Carnegie Mellon x 86 -64 Stack: Pop ¢ Stack “Bottom” popq Dest § Read value at address given by %rsp § Increment %rsp by 8 § Store value at Dest (usually a register) 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” 15

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

Carnegie Mellon Today ¢ Procedures § 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 16

Code Examples void multstore(long x, long y, long *dest) { long t = mult

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 17

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 18

Control Flow Example #1 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549:

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 19

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

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 20

Control Flow Example #3 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549:

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 21

Control Flow Example #4 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549:

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 22

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

Carnegie Mellon Today ¢ Procedures § Stack 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 23

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 24

void multstore (long x, long y, long *dest) { long t = mult 2(x,

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 25

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

Carnegie Mellon Today ¢ Procedures § 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 26

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 27

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 28

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 “Set-up” code § Includes push by call instruction Stack “Top” § § Deallocated when return “Finish” code § Includes pop by ret instruction § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29

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 30

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 31

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 32

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 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(…) • • 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 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(); • } 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(…) • • {

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 36

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 37

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 38

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 39

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 40

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) 41

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 42

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 43

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 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 Aside 1: movl $3000, Unused %esi %rsp call_incr: • Remember, movl -> %exx zeros out high order 32 bits. subq $16, %rsp • Why use movl instead of movq? 1 byte shorter. movq $15213, 8(%rsp) Register 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 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 %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 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 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 47

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 48

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 49

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 50

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 51

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 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? ¢ 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 53

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 54

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 55

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 56

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; } 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 57

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 58

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 saved in %rbx. • A callee saved register. 59

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 saved in %rbx. • A callee saved register. 60

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 • X Is safe in %rbx • Return result in %rax 61

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 62

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 63

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

Carnegie Mellon Today ¢ Procedures § 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 65

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 66

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 67

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 68

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 Rec. 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 69

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 70

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 71

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 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 . . . %rsp 72

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 73

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 74