1
play

1 2/26/2020 void multstore Today Code Examples (long x, long y, - PDF document

2/26/2020 Mechanisms in Procedures P( ) { Passing control To beginning of procedure code Machine-Level Programming III: y = Q(x); Back to return point print(y) Procedures Passing data } Procedure


  1. 2/26/2020 Mechanisms in Procedures P( … ) {  Passing control • •  To beginning of procedure code Machine-Level Programming III: y = Q(x);  Back to return point print(y) Procedures •  Passing data }  Procedure arguments  Return value CSci 2021: Machine Architecture and Organization int Q(int i)  Memory management February 26th-28th, 2020 { Your instructor: Stephen McCamant  Allocate during procedure execution int t = 3*i;  Deallocate upon return int v[10]; • Based on slides originally by:  Mechanisms all implemented with • Randy Bryant, Dave O’Hallaron machine instructions return v[t]; }  x86-64 implementation of a procedure uses only those mechanisms required Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2 These Slides x86-64 Stack Stack “Bottom”  Procedures  Region of memory managed  Stack Structure with stack discipline  Calling Conventions Increasing  Grows toward lower addresses Addresses  Passing control  Passing data  Register %rsp contains  Managing local data lowest in-use stack address  Illustration of Recursion  address of “top” element Stack Grows Down Stack Pointer: %rsp Stack “Top” 3 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition x86-64 Stack: Push x86-64 Stack: Pop Stack “Bottom” Stack “Bottom”  pushq Src  popq Des Dest  Fetch operand at Src  Read value at address given by %rsp  Decrement %rsp by 8 Increasing  Increment %rsp by 8 Increasing Addresses Addresses  Write operand at address given by %rsp  Store value at Dest (usually a register) Stack Stack Grows Grows Down Down Stack Pointer: %rsp Stack Pointer: %rsp +8 -8 Stack “Top” Stack “Top” 5 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1

  2. 2/26/2020 void multstore Today Code Examples (long x, long y, long *dest) { long t = mult2(x, y);  Procedures *dest = t; }  Stack Structure  Calling Conventions 0000000000400540 <multstore>:  Passing control 400540: push %rbx # Save %rbx 400541: mov %rdx,%rbx # Save dest  Passing data 400544: callq 400550 <mult2> # mult2(x,y)  Managing local data 400549: mov %rax,(%rbx) # Save at dest  Illustration of Recursion 40054c: pop %rbx # Restore %rbx 40054d: retq # Return long mult2 0000000000400550 <mult2>: (long a, long b) 400550: mov %rdi,%rax # a { 400553: imul %rsi,%rax # a * b long s = a * b; 400557: retq # Return return s; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8 Procedure Control Flow Control Flow Example #1 • • 0x130 0000000000400540 <multstore>:  Use stack to support procedure call and return • 0x128 •  Procedure call: call label • 0x120 400544: callq 400550 <mult2>  Push return address on stack 400549: mov %rax,(%rbx)  Jump to label • • %rsp 0x120  Return address:  Address of the next instruction right after call %rip 0x400544  Example from disassembly 0000000000400550 <mult2>:  Procedure return: ret 400550: mov %rdi,%rax •  Pop address from stack •  Jump to address 400557: retq 9 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Control Flow Example #2 Control Flow Example #3 • • • • 0x130 0x130 0000000000400540 <multstore>: 0000000000400540 <multstore>: • • 0x128 0x128 • • • • 0x120 0x120 400544: callq 400550 <mult2> 400544: callq 400550 <mult2> 0x118 0x400549 0x118 0x400549 400549: mov %rax,(%rbx) 400549: mov %rax,(%rbx) • • %rsp 0x118 %rsp 0x118 • • %rip 0x400550 %rip 0x400557 0000000000400550 <mult2>: 0000000000400550 <mult2>: 400550: mov %rdi,%rax 400550: mov %rdi,%rax • • • • 400557: retq 400557: retq 11 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

  3. 2/26/2020 Control Flow Example #4 Today • • 0x130 0000000000400540 <multstore>:  Procedures • 0x128 •  Stack Structure • 0x120 400544: callq 400550 <mult2>  Calling Conventions 400549: mov %rax,(%rbx)  Passing control • %rsp 0x120 •  Passing data  Managing local data %rip 0x400549  Illustrations of Recursion & Pointers 0000000000400550 <mult2>: 400550: mov %rdi,%rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14 void multstore Data Flow (long x, long y, long *dest) Procedure Data Flow { Examples long t = mult2(x, y); *dest = t; } Registers Stack  First 6 arguments 0000000000400540 <multstore>: • • • # x in %rdi, y in %rsi, dest in %rdx %rdi “ Di ane’s • • • 400541: mov %rdx,%rbx # Save dest %rsi Arg n silk 400544: callq 400550 <mult2> # mult2(x,y) %rdx dress • • • # t in %rax %rcx 400549: mov %rax,(%rbx) # Save at dest costs • • • %r8 Arg 8 $8 9 ” %r9 Arg 7 long mult2 0000000000400550 <mult2>: -- Geoff Kuenning, HMC (long a, long b) # a in %rdi, b in %rsi  Return value { 400550: mov %rdi,%rax # a long s = a * b; 400553: imul %rsi,%rax # a * b %rax  Only allocate stack space return s; # s in %rax } 400557: retq # Return when needed 15 16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Overview: GDB without source code  Procedures  GDB can also be used just at the instruction level  Stack Structure Source-level GDB Binary-level GDB  Calling Conventions step / next stepi / nexti  Passing control  Passing data break <line number> break * <address>  Interlude: binary-level GDB list disas  Managing local data  Illustrations of Recursion & Pointers print <variable> print with registers & casts print <data structure> examine info local info reg software watch hardware watch 17 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3

Recommend


More recommend