12 22 2016
play

12/22/2016 Functions A unit of code that we can call Also referred - PDF document

12/22/2016 Functions A unit of code that we can call Also referred to as a procedure, method, or subroutine Procedures (Functions) A function call is kind of like a jump, except it can return Must support passing data as function


  1. 12/22/2016 Functions A unit of code that we can call Also referred to as a procedure, method, or subroutine Procedures (Functions)  A function call is kind of like a jump, except it can return  Must support passing data as function arguments and return values Before we continue, we first have to understand how a stack works… – 2 – x86-64 stack Stack Pushing Stack “Bottom” Stack “Bottom” Region of memory managed with Pushing last-in, first-out discipline  pushq Src  Grows toward lower addresses  Fetch operand at Src  Register %rsp indicates top Increasing  Decrement %rsp by 8 Increasing Addresses Addresses element of stack  Write operand at address given by %rsp  Top element has lowest address  e.g. pushq %rax The stack is essential for function subq $8, %rsp movq %rax,(%rsp) calls  Function arguments  Return address Stack Grows Stack Grows Stack Stack Down Down  Prior stack frame information Pointer Pointer -8 %rsp %rsp  Local variables Stack “Top” Stack “Top” – 3 – – 4 – 1

  2. 12/22/2016 Stack Popping Stack Operation Examples Stack “Bottom” Popping Initially pushq %rax popq %rdx  popq Dest  Read operand at address given by %rsp 0x118 0x118 0x118  Write to Dest Increasing Addresses  Increment %rsp by 8 0x110 0x110 0x110  e.g. popq %rax 0x108 123 0x108 123 0x108 123 movq (%rsp),%rax Top 0x100 213 0x100 213 addq $8,%rsp Top Top Stack Grows Stack %rax Down 213 %rax 213 %rax 213 Pointer +8 %rsp %rdx %rdx %rdx 213 555 %rsp 0x108 %rsp 0x108 0x100 %rsp 0x108 0x100 Stack “Top” – 5 – – 6 – Control Flow terminology Control Flow When foo calls who : The hardware provides machine instructions for this: foo is the caller , who is the callee  Function call  Control is transferred to the ‘callee’ When function returns  call label  Control is transferred back to the ‘caller’  Push return address on stack (address of next instruction after the call) Last-called, first-return (LIFO) order naturally implemented via stack  Jump to label foo(…) Function return { • • •  ret call who(); who(…)  Pop return address from stack • • • {  Jump to address } • • • call amI(); amI(…) • • • { ret } • • • • • • ret } – 7 – – 8 – 2

  3. 12/22/2016 Control Flow Example #1 Control Flow Example #2 0x130 0x130 0000000000400540 <multstore>: 0000000000400540 <multstore>: 0x128 0x128 • • • • 0x120 0x120 400544: callq 400550 <mult2> 400544: callq 400550 <mult2> 0x118 0x400549 400549: mov %rax,(%rbx) 400549: mov %rax,(%rbx) • • • %rsp 0x120 • %rsp 0x118 %rip 0x400544 %rip 0x400550 0000000000400550 <mult2>: 0000000000400550 <mult2>: 400550: mov %rdi,%rax 400550: mov %rdi,%rax • • • • 400557: retq 400557: retq – 9 – – 10 – Control Flow Example #3 Control Flow Example #4 0x130 0x130 0000000000400540 <multstore>: 0000000000400540 <multstore>: • 0x128 • 0x128 • • 0x120 0x120 400544: callq 400550 <mult2> 400544: callq 400550 <mult2> 0x118 0x400549 400549: mov %rax,(%rbx) 400549: mov %rax,(%rbx) • • • %rsp 0x118 • %rsp 0x120 %rip 0x400557 %rip 0x400549 0000000000400550 <mult2>: 0000000000400550 <mult2>: 400550: mov %rdi,%rax 400550: mov %rdi,%rax • • • • 400557: retq 400557: retq – 11 – – 12 – 3

  4. 12/22/2016 Practice problem Function calls and stack frames What does this code do? For languages supporting recursion (C, Java), code must be re- entrant call next  Multiple simultaneous instantiations of a single function next:  Must store multiple versions of arguments, local variables, return popq %rax address  Return address  Local variables Stack bottom What is the value of %rax?  Function arguments (if necessary) increasing addresses foo’s  Saved register state (if necessary) stack What would this be useful for? stack growth frame Implemented with stack frames who’s  Upon function invocation stack  Stack frame created frame  Stack frame pushed onto stack amI’s  Upon function completion stack  Stack frame popped off stack frame  Caller’s frame recovered – 13 – – 14 – Call chain: foo => who => amI Call Chain Example Example Stack Example Call Chain foo(…) foo foo(…) %rbp { { foo foo foo • • who %rsp • who(…) • who(); who { who(); amI amI • • • • • • amI(); • amI amI amI(…) } • • • } amI { amI(); • amI • • • • } amI amI(); • amI • } Procedure amI() is recursive – 15 – – 16 – 4

  5. 12/22/2016 Example Example Stack Stack foo(…) foo foo(…) foo who(…) who(…) { { foo foo foo foo { { • • who amI(…) who • • • • • • • • %rbp { amI(); amI(); who(); who(); • who who • • • • • • • amI amI • amI amI %rsp • amI(); amI(); • • amI(); %rbp • • • • • • } } } } amI • amI amI } } } } • %rsp } amI amI – 17 – – 18 – Example Example Stack Stack foo(…) foo foo(…) foo who(…) who(…) { { foo foo foo foo { { • • amI(…) who amI(…) who • • • • • • • • { { amI(); amI(); who(); who(); amI(…) amI(…) • • who who • • • • • • amI amI amI amI • • { { • • amI(); amI(); amI(…) • • • • amI(); amI(); • • • • • • { } } } } • • • amI • amI amI amI } } } } • amI(); amI(); • • • • • } } %rbp amI(); • • amI amI amI amI • } } %rsp • %rbp } amI %rsp – 19 – – 20 – 5

  6. 12/22/2016 Example Example Stack Stack foo(…) foo foo(…) foo who(…) who(…) { { foo foo foo foo { { • • amI(…) who amI(…) who • • • • • • • • { { amI(); amI(); who(); who(); amI(…) • • who who • • • • • • • amI amI • amI amI { • • amI(); amI(); • • • amI(); amI(); %rbp • • • • • • } } } } • • amI • amI amI amI } } } } amI(); • • %rsp • } %rbp } • amI amI amI } %rsp – 21 – – 22 – Example Example Stack Stack foo(…) foo foo(…) foo who(…) who(…) { { foo foo foo foo { { • • who amI(…) who • • • • • • • • %rbp { amI(); amI(); who(); who(); • who who • • • • • • amI amI amI amI • • %rsp • amI(); amI(); • • amI(); %rbp • • • • • • } } } } amI • amI amI } } } } • %rsp } amI amI – 23 – – 24 – 6

  7. 12/22/2016 Example Example Stack Stack foo(…) foo foo %rbp who(…) foo(…) { foo foo foo foo { { • who who %rsp • • • • • %rbp amI(); • who(); who • • • who(); • amI amI amI amI %rsp amI(); • • • • • • } } amI amI } } } } amI amI – 25 – – 26 – x86-64/Linux Stack Frame Higher Function arguments Addresses Caller Stack Frame (Pink)  Function arguments for callee Passed in registers typically Overflow onto stack when needed  Only used with 7+ integer  First 6 “integer” arguments Caller arguments Frame  Arguments 1-6 passed in %rdi Arguments registers 7+ %rsi Arg n  Return address Return Addr Frame Pointer %rdx  Pushed by call instruction %rbp Old %ebp • • • %rcx Callee Stack Frame (Yellow) (From (optional) Saved Top to Bottom) %r8 Arg 8 Registers  Old frame pointer (optional) %r9 Arg 7 +  Local variables (optional) Callee Local  If can’t keep in registers Frame Variables  Saved register context (optional) Return value  If certain registers needed Arguments Stack Pointer 7+  Function arguments for next call %rax %rsp – 27 – – 28 – 7

Recommend


More recommend