Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27
Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure Call Example Recursive Procedure Call Example Young W. Lim Procedure Calls 2016-11-05 Sat 2 / 27
Based on "Self-service Linux: Mastering the Art of Problem Determination", Mark Wilding "Computer Architecture: A Programmer s Perspective", Bryant & O Hallaron I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Procedure Calls 2016-11-05 Sat 3 / 27
Stack Frame (1) procedure calls pass procedure arguments store the return info save registers stack frame: the portion of the stack allocated for single procedure call frame pointer (%ebp) stack pointer (%esp) Young W. Lim Procedure Calls 2016-11-05 Sat 4 / 27
Stack Frame : A Caller s Viewpoint (2) HIGH ADDRESS - frame pointer (%ebp) saved registers local variables temporaries arguments for a funcion call to the callee return address stack pointer (%esp) LOW ADDRESS Young W. Lim Procedure Calls 2016-11-05 Sat 5 / 27
Stack Frame : A Callee s Viewpoint (3) %ebp+c: argument 2 from the caller %ebp+8: argument 1 from the caller %ebp+4: return address of the caller HIGH ADDRESS - %ebp : caller s frame pointer stored saved registers of the callee local variables of the callee temporaries of the callee... LOW ADDRESS Young W. Lim Procedure Calls 2016-11-05 Sat 6 / 27
Stack Frames & Pointers Frame Pointer (%ebp) : High : bottom of a stack frame Stack Pointer (%esp) : Low : top of a stack frame stack grows toward lower addresses pushl decrease %esp (growing stack) popl increase %esp (shrinking stack) data accessed via %ebp Young W. Lim Procedure Calls 2016-11-05 Sat 7 / 27
Stack Frames & Arguments Caller P Callee Q Caller P s Stack Frame Argument values to Q Return address to P Callee Q Stack Frame P s frame pointer (%ebp) Saved registers (some local vars) Local variables Temporaries Q s arguments to other functions Young W. Lim Procedure Calls 2016-11-05 Sat 8 / 27
Stack Frames & Heap HIGH ADDRESS - STACK (stack frame grows toward lower addresses) Stack Frame #1 Stack Frame #2... Stack Frame #n Currently Unused Heap (heap grows toward higher addresses) Global Variables (BSS Seg) Static Variables (Data Seg) Machine Code (Text(ELF)) LOW ADDRESS - Young W. Lim Procedure Calls 2016-11-05 Sat 9 / 27
Stack Frames & Memory Map HIGH ADDRESS - STACK (toward lower addresses) Heap (toward higher addresses) Global Variables (BSS Seg) Static Variables (Data Seg) Machine Code (Text(ELF)) LOW ADDRESS - Young W. Lim Procedure Calls 2016-11-05 Sat 10 / 27
Procedure Instructions Procedure Call call label (direct call) call *operand (indirect call) Procedure Return leave (stack preparation) ret Young W. Lim Procedure Calls 2016-11-05 Sat 11 / 27
Call Instructions push a return address jump to the start the called function return address the address of the instruction immediately following the call instruction Direct Call call label Indirect call call *operand offset Imm a base reg Eb an index reg Ei a scale factor s Young W. Lim Procedure Calls 2016-11-05 Sat 12 / 27
Ret Instructions ret instruction pops the return address from the stack jump to the return address location stack pointer must points to the return address leave instruction prepare the stack for returning equivalent to the following mov %ebp, %esp Set stack ptr to the beginning of the stack) pop %ebp Restore saved %ebp Set the stack ptr to the end of caller s stack Young W. Lim Procedure Calls 2016-11-05 Sat 13 / 27
Return Value register %eax is used for returning the value of any function that returns an integer or pointer Young W. Lim Procedure Calls 2016-11-05 Sat 14 / 27
IA32 conventions for register usage the callee should not overwrite some registers that the caller is going to use later Caller Save Registers %eax, %edx, %ecx the callee can overwrite these registers Callee Save Registers %ebx, %esi, %edi the callee must save these registers before using must restore them before returning %ebp for the frame pointer %esp for the stack pointer Young W. Lim Procedure Calls 2016-11-05 Sat 15 / 27
GCC Example for a procedure call pushl %edi pushl %esi pushl %ebx movl 24(%ebp), %eax imull 15(%ebp), %eax leal 0(,%eax,4), %ecx addl 8(%ebp), %ecx movl %ebx, %edx Callee Save Registers %edi, %esi, %ebx restore these at the end by popl instructions Caller Save Registers other registers %eax, %ecx, %edx no need to restore Young W. Lim Procedure Calls 2016-11-05 Sat 16 / 27
P() calls Q() int P() { int a1 = 55; int a2 = 77; int sum = Q( &a1, &a2 ); int diff = a1 - a2; } return sum * diff; int Q(int *xp, int *yp) { int x = *xp; int y = *yp; } *xp = y; *yp = x; return x+y; Young W. Lim Procedure Calls 2016-11-05 Sat 17 / 27
Stack Frames for P() & Q() %ebp+0 : saved %ebp %ebp-4 : a2 %ebp-8 : a1 %ebp-12: &a2 %ebp-16: &a1 just beforecall to Q() %ebp+24 : saved %ebp (of P) %ebp+20 : a2 %ebp+16 : a1 %ebp+12 : &a2 %ebp+ 8 : &a1 %ebp+ 4 : return address %ebp+ 0 : saved %ebp (<-FP) %ebp- 4 : saved %ebx (<-SP) in the body of Q() Young W. Lim Procedure Calls 2016-11-05 Sat 18 / 27
Calling Code of P() leal -4(%ebp), %eax pushl %eax leal -8(%ebp), %eax pushl %eax call Q - compute &a2 - push &a2 - compute &a1 - push &a1 - call Q() function Young W. Lim Procedure Calls 2016-11-05 Sat 19 / 27
Setup Code for Q() %ebx is used in Q %ebx is a callee save register %ebx is pushed onto the stack Q: pushl %ebp movl %esp, %ebp pushl %ebx %ebp : frame pointer of P save this old %ebp set %ebp as a new frame pointer save %ebx Young W. Lim Procedure Calls 2016-11-05 Sat 20 / 27
Body Code for Q() return value is at %eax movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %ecx, (%edx) movl %ebx, (%ecx) addl %ebx, %eax %edx holds xp %ecx holds yp %ebx holds x %eax holds y assign y to *xp assign x to *yp %eax holds x+y Young W. Lim Procedure Calls 2016-11-05 Sat 21 / 27
Finish Code for Q() popl %ebx movl %ebp, %esp popl %ebp ret restore %ebx restore %esp restore %ebp return to the caller Young W. Lim Procedure Calls 2016-11-05 Sat 22 / 27
Fibonacci Sequence int fibo(int n) { int prev, val; if (n <= 2) return 1; prev = fibo(n-2); val = fibo(n-1); return prev + val; } multiple outstanding calls each call has its own local variables allocated only when the procedure is called deallocated when it returns Young W. Lim Procedure Calls 2016-11-05 Sat 23 / 27
Stack Frames for the caller and the callee %ebp+8 : n %ebp+4 : return address %ebp+0 : saved %ebp...... %ebp-20: saved %esi %ebp-24: saved %ebp after initial setup %ebp+8 : n %ebp+4 : return address %ebp+0 : saved %ebp...... %ebp-20: saved %esi %ebp-24: saved %ebp...... %ebp-40: n-2 just before the 1st recusive call Young W. Lim Procedure Calls 2016-11-05 Sat 24 / 27
Setup Code for fibo() fibo: pushl %ebp movl %esp, %ebp subl $16, %esp pushl %esi pushl %ebx Set up code %ebp: frame pointer alloc 16 bytes on stack save %esi (-20) save %ebx (-24) Young W. Lim Procedure Calls 2016-11-05 Sat 25 / 27
Body Code for fibo() movl 8(%ebp), %ebx cmpl $2, %ebx jle.l24 addl $-12, %esp leal -2(%ebx), %eax pushl %eax call fibo movl %eax, %esi addl $-12, %esp leal -1(%ebx), %eax pushl %eax call fibo addl %esi, %eax jmp.l25 Young W. Lim Procedure Calls 2016-11-05 Sat 26 / 27
Finish Code for Q() popl %ebx movl %ebp, %esp popl %ebp ret restore %ebx restore %esp restore %ebp return to the caller Young W. Lim Procedure Calls 2016-11-05 Sat 27 / 27