University of Washington
Procedures and Call Stacks
¢ How do I pass arguments to a procedure? ¢ How do I get a return value from a procedure? ¢ Where do I put local variables? ¢ When a func on returns, how does it know where to return to?
¢ To answer these ques ons, we need a call stack …
Autumn 2012 Procedures and Stacks 1
University of Washington
Memory Layout 2N-1 Stack local variables
Dynamic Data new'ed variables (Heap)
static variables Static Data (including global variables (C))
Literals literals (e.g., “example”)
Instructions 0
Autumn 2012 Procedures and Stacks 2 University of Washington
Memory Layout
Stack “Automatic” lifetime; writable; not executable mutable
Dynamic Data Programmer controlled lifetime; writable; not executable mutable (Heap)
writable; not executable Static Data Execution lifetime; mutable
Read-only; not executable Literals Execution lifetime; immutable
Read-only; executable Instructions Execution lifetime; immutable
Autumn 2012 Procedures and Stacks 3
University of Washington
IA32 Stack
¢ Region of memory managed Stack “Bo om” with a stack discipline ¢ Grows toward lower addresses Increasing ¢ Customarily shown “upside-down” Addresses
¢ Register %esp contains lowest stack address = address of “top” element Stack Grows Down Stack Pointer: %esp
Stack “Top”
Autumn 2012 Procedures and Stacks 4 University of Washington
IA32 Stack: Push Stack “Bo om” ¢ pushl Src
Increasing Addresses
Stack Grows Down Stack Pointer: %esp
Stack “Top”
Autumn 2012 Procedures and Stacks 5
University of Washington
IA32 Stack: Push Stack “Bo om” ¢ pushl Src § Fetch operand at Src § Decrement %esp by 4 Increasing Addresses § Write operand at address given by %esp
Stack Grows Down
-4 Stack Pointer: %esp
Stack “Top”
Autumn 2012 Procedures and Stacks 6 University of Washington
IA32 Stack: Pop Stack “Bo om” ¢ popl Dest
Increasing Addresses
Stack Grows Down Stack Pointer: %esp
Stack “Top”
Autumn 2012 Procedures and Stacks 7
University of Washington
IA32 Stack: Pop Stack “Bo om” ¢ popl Dest § Read operand at address %esp § Increment %esp by 4 Increasing Addresses § Write operand to Dest
Stack Grows Down +4 Stack Pointer: %esp
Stack “Top”
Autumn 2012 Procedures and Stacks 8 University of Washington
Procedure Call Overview Caller …
¢ Callee must know where to find args ¢ Callee must know where to find “return address” ¢ Caller must know where to find return val ¢ Caller and Callee run on same cpu → use the same registers § Might need to save registers used by Callee
Autumn 2012 Procedures and Stacks 9
University of Washington
Procedure Call Overview Caller …
¢ The conven on of where to leave/find things is called the procedure call linkage § Details vary between systems § We will see the conven on for IA32/Linux in detail
Autumn 2012 Procedures and Stacks 10 University of Washington
Procedure Control Flow
¢ Use stack to support procedure call and return ¢ Procedure call: call label § Push return address on stack § Jump to label
Autumn 2012 Procedures and Stacks 11
University of Washington
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 instruc on beyond call § Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90
Autumn 2012 Procedures and Stacks 12 University of Washington Procedure Call Example
804854e: e8 3d 06 00 00 call 8048b90
call 8048b90
0x110 0x10c 0x108 123
%esp 0x108
%eip 0x804854e
%eip: program counter Autumn 2012 Procedures and Stacks 13
University of Washington Procedure Call Example
804854e: e8 3d 06 00 00 call 8048b90
call 8048b90
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104
%esp 0x108 %esp 0x108
%eip 0x804854e %eip 0x804854e
%eip: program counter Autumn 2012 Procedures and Stacks 14 University of Washington Procedure Call Example
804854e: e8 3d 06 00 00 call 8048b90
call 8048b90
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104
%esp 0x108 %esp 0x108
%eip 0x804854e %eip 0x804854e0x8048553
%eip: program counter Autumn 2012 Procedures and Stacks 15
University of Washington Procedure Call Example
804854e: e8 3d 06 00 00 call 8048b90
call 8048b90
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553
%esp 0x108 %esp 0x1080x104
%eip 0x804854e %eip 0x804854e0x8048553
%eip: program counter Autumn 2012 Procedures and Stacks 16 University of Washington Procedure Call Example
804854e: e8 3d 06 00 00 call 8048b90
call 8048b90
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553
%esp 0x108 %esp 0x1080x104
%eip 0x804854e %eip 0x8048553 + 0x000063d 0x8048b90 %eip: program counter Autumn 2012 Procedures and Stacks 17
University of Washington
Procedure Return Example
8048591: c3 ret
ret
0x110 0x10c 0x108 123 0x104 0x8048553
%esp 0x104
%eip 0x8048591
%eip: program counter Autumn 2012 Procedures and Stacks 18 University of Washington
Procedure Return Example
8048591: c3 ret
ret
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553
%esp 0x104 %esp 0x104
%eip 0x8048591 %eip 0x8048591
%eip: program counter Autumn 2012 Procedures and Stacks 19
University of Washington
Procedure Return Example
8048591: c3 ret
ret
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553
%esp 0x104 %esp 0x104
%eip 0x8048591 %eip 0x80485910x8048553
%eip: program counter Autumn 2012 Procedures and Stacks 20 University of Washington
Procedure Return Example
8048591: c3 ret
ret
0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553
%esp 0x104 %esp 0x1040x108
%eip 0x8048591 %eip 0x80485910x8048553
%eip: program counter Autumn 2012 Procedures and Stacks 21
University of Washington Stack-Based Languages
¢ Languages that support recursion § e.g., C, Pascal, Java § Code must be re-entrant § Mul ple simultaneous instan a ons of single procedure – What would happen if code could not be reentrant? § Need some place to store state of each instan a on § Arguments § Local variables § Return pointer ¢ Stack discipline § State for a given procedure needed for a limited me § Star ng from when it is called to when it returns § Callee always returns before caller does ¢ Stack allocated in frames § State for a single procedure instan a on
Autumn 2012 Procedures and Stacks 22 University of Washington
Call Chain Example Example Call Chain yoo(…) { • yoo • who(…) who(); { who • • • • • amI(); } amI(…) amI amI • • • { amI(); • amI • • • • } amI(); • amI • }
Procedure amI is recursive (calls itself)
Autumn 2012 Procedures and Stacks 23
University of Washington
Stack Frames Previous ¢ Contents Frame § Local variables § Return informa on Frame Pointer: %ebp § Temporary space Frame for proc
Stack Pointer: %esp
¢ Management? Stack “Top”
Autumn 2012 Procedures and Stacks 24 University of Washington
Stack Frames Previous ¢ Contents Frame § Local variables § Return informa on Frame Pointer: %ebp § Temporary space Frame for proc
Stack Pointer: %esp
¢ Management § Space allocated when procedure is entered Stack “Top” § “Set-up” code § Space deallocated upon return § “Finish” code
Autumn 2012 Procedures and Stacks 25
University of Washington
Stack Example
yoo(…) yoo %ebp { • yoo who • %esp who(); • amI amI • } amI
amI
Autumn 2012 Procedures and Stacks 26 University of Washington
Stack Example
who(…) yoo { • • • yoo who amI(); • • • %ebp amI(); amI amI who • • • %esp } amI
amI
Autumn 2012 Procedures and Stacks 27
University of Washington
Stack Example
amI(…) yoo { • yoo who • amI(); • amI amI who • } amI %ebp amI %esp amI
Autumn 2012 Procedures and Stacks 28 University of Washington
Stack Example
amI(…) yoo { • yoo who • amI(); • amI amI who • } amI amI
amI %ebp amI %esp
Autumn 2012 Procedures and Stacks 29
University of Washington
Stack Example
amI(…) yoo { • yoo who • amI(); • amI amI who • } amI amI
amI
amI
%ebp amI %esp
Autumn 2012 Procedures and Stacks 30 University of Washington
Stack Example
amI(…) yoo { • yoo who • amI(); • amI amI who • } amI amI
amI %ebp amI %esp
Autumn 2012 Procedures and Stacks 31
University of Washington
Stack Example
amI(…) yoo { • yoo who • amI(); • amI amI who • } amI %ebp amI %esp amI
Autumn 2012 Procedures and Stacks 32 University of Washington
Stack Example
who(…) yoo { • • • yoo who amI(); • • • %ebp amI(); amI amI who • • • %esp } amI
amI
Autumn 2012 Procedures and Stacks 33
University of Washington
Stack Example
amI(…) yoo { • yoo who • • • amI amI who • } amI %ebp amI %esp amI
Autumn 2012 Procedures and Stacks 34 University of Washington
Stack Example
who(…) yoo { • • • yoo who amI(); • • • %ebp amI(); amI amI who • • • %esp } amI
amI
Autumn 2012 Procedures and Stacks 35
University of Washington
Stack Example
yoo(…) yoo %ebp { • yoo who • %esp who(); • amI amI • } amI
amI
Autumn 2012 Procedures and Stacks 36 University of Washington
IA32/Linux Stack Frame
¢ Current Stack Frame (“Top” to Bo om) § Old frame pointer § Local variables Caller If can’t be just kept in registers Frame § Saved register context Arguments When reusing registers § “Argument build area” Frame pointer Return Addr Parameters for func on %ebp Old %ebp about to be called Saved Registers + ¢ Caller Stack Frame Local § Return address Variables Pushed by call instruc on § Arguments for this call Argument Stack pointer Build %esp
Autumn 2012 Procedures and Stacks 37
University of Washington Revisi ng swap
int zip1 = 15213; int zip2 = 98195;
void call_swap() { swap(&zip1, &zip2); }
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
Autumn 2012 Procedures and Stacks 38 University of Washington Revisi ng swap Calling swap from call_swap int zip1 = 15213; call_swap: int zip2 = 98195; • • • pushl $zip2 # Global Var void call_swap() pushl $zip1 # Global Var { call swap swap(&zip1, &zip2); • • • }
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
Autumn 2012 Procedures and Stacks 39
University of Washington Revisi ng swap Calling swap from call_swap int zip1 = 15213; call_swap: int zip2 = 98195; • • • pushl $zip2 # Global Var void call_swap() pushl $zip1 # Global Var { call swap swap(&zip1, &zip2); • • • }
Resul ng • Stack void swap(int *xp, int *yp) • { • int t0 = *xp; int t1 = *yp; &zip2 *xp = t1; &zip1 *yp = t0; } Rtn adr %esp
Autumn 2012 Procedures and Stacks 40 University of Washington Revisi ng swap
swap: pushl %ebp void swap(int *xp, int *yp) movl %esp,%ebp Set { pushl %ebx Up int t0 = *xp; int t1 = *yp; movl 12(%ebp),%ecx *xp = t1; movl 8(%ebp),%edx *yp = t0; movl (%ecx),%eax Body } movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx)
movl -4(%ebp),%ebx movl %ebp,%esp Finish popl %ebp ret
Autumn 2012 Procedures and Stacks 41
University of Washington swap Setup #1
Entering Stack Resul ng Stack?
%ebp • • •
&zip2 &zip1 Rtn adr %esp
swap: pushl %ebp movl %esp,%ebp pushl %ebx
Autumn 2012 Procedures and Stacks 42 University of Washington swap Setup #1
Entering Stack Resul ng Stack
%ebp %ebp • • • • • •
&zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %esp
swap: pushl %ebp movl %esp,%ebp pushl %ebx
Autumn 2012 Procedures and Stacks 43
University of Washington swap Setup #1
Entering Stack Resul ng Stack
%ebp • • • • • •
&zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp %esp
swap: pushl %ebp movl %esp,%ebp pushl %ebx
Autumn 2012 Procedures and Stacks 44 University of Washington swap Setup #1
Entering Stack Resul ng Stack
%ebp • • • • • •
&zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp Old %ebx %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx
Autumn 2012 Procedures and Stacks 45
University of Washington swap Setup #1
Entering Stack Resul ng Stack
%ebp • • • • • Offset rela ve • to new %ebp &zip2 12 yp &zip1 8 xp Rtn adr %esp 4 Rtn adr Old %ebp %ebp Old %ebx %esp
movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp . . .
Autumn 2012 Procedures and Stacks 46 University of Washington swap Finish #1
swap’s Stack Resul ng Stack?
• • •
yp xp Rtn adr Old %ebp %ebp Old %ebx %esp
movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Autumn 2012 Procedures and Stacks 47
University of Washington swap Finish #1
swap’s Stack Resul ng Stack
• • • • • •
yp yp xp xp Rtn adr Rtn adr Old %ebp %ebp Old %ebp %ebp Old %ebx %esp Old %ebx %esp
movl -4(%ebp),%ebx movl %ebp,%esp Observa on: Saved and restored popl %ebp ret register %ebx Autumn 2012 Procedures and Stacks 48 University of Washington swap Finish #2
swap’s Stack Resul ng Stack
• • • • • •
yp yp xp xp Rtn adr Rtn adr Old %ebp %ebp Old %ebp %ebp %esp Old %ebx %esp
same as popl %ebx movl -4(%ebp),%ebx movl %ebp,%esp but only because %esp = %ebp - 4 popl %ebp ret Autumn 2012 Procedures and Stacks 49
University of Washington swap Finish #3
swap’s Stack Resul ng Stack
%ebp • • • • • •
yp yp xp xp Rtn adr Rtn adr %esp Old %ebp %ebp Old %ebx %esp
movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Autumn 2012 Procedures and Stacks 50 University of Washington swap Finish #4
swap’s Stack Resul ng Stack
%ebp • • • • • •
yp yp xp xp %esp Rtn adr Old %ebp %ebp Old %ebx %esp
movl -4(%ebp),%ebx ¢ Observa on movl %ebp,%esp popl %ebp § Saved & restored register %ebx ret § Didn’t do so for %eax, %ecx, or %edx Autumn 2012 Procedures and Stacks 51
University of Washington
Disassembled swap
080483a4
Calling Code 8048409: e8 96 ff ff ff call 80483a4
swap’s Stack Resul ng Stack
%ebp • • • • • •
yp yp xp xp %esp Rtn adr Old %ebp %ebp Old %ebx %esp
movl -4(%ebp),%ebx ¢ Observa on movl %ebp,%esp popl %ebp § Saved & restored register %ebx ret § Didn’t do so for %eax, %ecx, or %edx Autumn 2012 Procedures and Stacks 53
University of Washington Register Saving Conven ons
¢ When procedure yoo calls who: § yoo is the caller § who is the callee
¢ Can a register be used for temporary storage? yoo: who: • • • • • • movl $12345, %edx movl 8(%ebp), %edx call who addl $98195, %edx addl %edx, %eax • • • • • • ret ret
§ Contents of register %edx overwri en by who
Autumn 2012 Procedures and Stacks 54 University of Washington
Saving registers
¢ When should you save them?
¢ When should you not save them? § Why not save all of them?
Autumn 2012 Procedures and Stacks 55
University of Washington Register Saving Conven ons
¢ When procedure yoo calls who: § yoo is the caller § who is the callee
¢ Can register be used for temporary storage? ¢ Conven ons § “Caller Save” § Caller saves temporary in its frame before calling § “Callee Save” § Callee saves temporary in its frame before using ¢ Why do we have these conven ons?
Autumn 2012 Procedures and Stacks 56 University of Washington IA32/Linux Register Usage
¢ %eax, %edx, %ecx § Caller saves prior to call if %eax values are used later Caller-Save Temporaries %edx %ecx ¢ %eax § also used to return %ebx Callee-Save integer value Temporaries %esi %edi ¢ %ebx, %esi, %edi %esp § Callee saves if wants to Special use them %ebp
¢ %esp, %ebp § special
Autumn 2012 Procedures and Stacks 57
University of Washington Recursive Factorial
int rfact(int x) rfact: { pushl %ebp int rval; movl %esp,%ebp if (x <= 1) pushl %ebx return 1; movl 8(%ebp),%ebx rval = rfact(x-1); cmpl $1,%ebx return rval * x; jle .L78 } leal -1(%ebx),%eax pushl %eax call rfact imull %ebx,%eax jmp .L79 .align 4 .L78: movl $1,%eax .L79: movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Autumn 2012 Procedures and Stacks 58 University of Washington Recursive Factorial
int rfact(int x) rfact: { pushl %ebp int rval; movl %esp,%ebp if (x <= 1) pushl %ebx return 1; movl 8(%ebp),%ebx rval = rfact(x-1); cmpl $1,%ebx return rval * x; jle .L78 } leal -1(%ebx),%eax pushl %eax call rfact ¢ Registers imull %ebx,%eax § %ebx used, but saved at jmp .L79 beginning & restored at end .align 4 § %eax used without first saving .L78: movl $1,%eax § expect caller to save .L79: § pushed onto stack as movl -4(%ebp),%ebx parameter for next call movl %ebp,%esp popl %ebp § used for return value ret Autumn 2012 Procedures and Stacks 59
University of Washington
Pointer Code
Recursive Procedure Top-Level Call void s_helper int sfact(int x) (int x, int *accum) { { int val = 1; if (x <= 1) s_helper(x, &val); return; return val; else { } int z = *accum * x; *accum = z; s_helper (x-1,accum); } }
¢ Pass pointer to update loca on
Autumn 2012 Procedures and Stacks 60 University of Washington Crea ng & Ini alizing Pointer
int sfact(int x) ¢ Variable val must be stored on stack { § Because: Need to create pointer to it int val = 1; ¢ Compute pointer as -4(%ebp) s_helper(x, &val); ¢ Push on stack as second argument return val; }
8 x 4 Rtn adr Ini al part of sfact 0 Old %ebp %ebp __sfact:sfact: pushl %%ebpebp # Save %%ebpebp -4 val = 1 movl %%esp,%ebpesp,%ebp # Set %%ebpebp -8 Temp. subl $16,%esp # Add 16 bytes Space movl 8(%8(%ebp),%edxebp),%edx # edx = x -12 Unused movl $1,-4(%$1,-4(%ebp)ebp) # val = 1 -16 %esp
Autumn 2012 Procedures and Stacks 61
University of Washington
Passing Pointer Stack at me of call int sfact(int x) { 8 x int val = 1; 4 Rtn adr s_helper(x, &val); %ebp return val; 0 Old %ebp } -4 valval =x!= 1 -8 -12 Unused -16 Calling s_helper from sfact &val leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack x %esp pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val • • • # Finish
Autumn 2012 Procedures and Stacks 62 University of Washington
IA 32 Procedure Summary
¢ Stack makes recursion work § Private storage for each instance of procedure call § Instan a ons don’t clobber each other Caller § Addressing of locals + arguments can be Frame rela ve to stack posi ons Arguments § Managed by stack discipline Return Addr § Procedures return in inverse order of calls %ebp Old %ebp ¢ IA32 procedures Saved Combina on of Instruc ons + Conven ons Registers § call / ret instruc ons + § Local Register usage conven ons Variables § caller / callee save § %ebp and %esp Argument Build § Stack frame organiza on conven ons %esp
Autumn 2012 Procedures and Stacks 63
University of Washington
x86-64 Procedure Calling Conven on
¢ Doubling of registers makes us less dependent on stack § Store argument in registers § Store temporary variables in registers
¢ What do we do if we have too many arguments or too many temporary variables?
Autumn 2012 Procedures and Stacks 64 University of Washington
x86-64 64-bit Registers: Usage Conven ons
%rax Return value %r8 Argument #5
%rbx Callee saved %r9 Argument #6
%rcx Argument #4 %r10 Caller saved
%rdx Argument #3 %r11 Caller Saved
%rsi Argument #2 %r12 Callee saved
%rdi Argument #1 %r13 Callee saved
%rsp Stack pointer %r14 Callee saved
%rbp Callee saved %r15 Callee saved
Autumn 2012 Procedures and Stacks 65
University of Washington
Revisi ng swap, again
swap: pushl %ebp void swap(int *xp, int *yp) movl %esp,%ebp Set { pushl %ebx Up int t0 = *xp; int t1 = *yp; movl 12(%ebp),%ecx *xp = t1; movl 8(%ebp),%edx *yp = t0; movl (%ecx),%eax Body } movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx)
movl -4(%ebp),%ebx movl %ebp,%esp Finish popl %ebp ret
Autumn 2012 Procedures and Stacks 66 University of Washington
Revisi ng swap, IA32 vs. x86-64 versions
swap: swap (64-bit long ints): pushl %ebp movq (%rdi), %rdx movl %esp,%ebp Set movq (%rsi), %rax pushl %ebx Up movq %rax, (%rdi) movq %rdx, (%rsi) movl 12(%ebp),%ecx ret movl 8(%ebp),%edx movl (%ecx),%eax Body ¢ Operands passed in registers movl (%edx),%ebx § First ( ) in , movl %eax,(%edx) xp %rdi movl %ebx,(%ecx) second (yp) in %rsi § 64-bit pointers movl -4(%ebp),%ebx ¢ No stack opera ons movl %ebp,%esp Finish popl %ebp required (except ret) ret ¢ Avoiding stack § Can hold all local informa on in registers Autumn 2012 Procedures and Stacks 67
University of Washington
X86-64 procedure call highlights
¢ Arguments (up to first 6) in registers § Faster to get these values from registers than from stack in memory ¢ Callq instruc ons stores 64-bit address on stack § Address pushed onto stack decremen ng rsp by 8 ¢ Local variables also in registers (if there is room) § Eliminates stack accesses and need to allocate stack space ¢ Func ons can access storage on stack up to 128 beyond rsp § Can store some temps on stack without altering rsp ¢ No frame pointer § All references to stack made rela ve to rsp, no need to restore base ptr ¢ Some registers designated “callee-saved” § Must to restored by callee before ret
Autumn 2012 Procedures and Stacks 68 University of Washington
Passing differently sized arguments
¢ Use registers %edi, %esi, %edx, … for 32-bit arguments ¢ Use registers %di, %si, %dx, … for 16-bit arguments ¢ Use registers %dil, %sil, %dl, … for 8-bit arguments
¢ Useful instruc ons: § movzbl: move byte to low-end of long zero-filled § movslq: move long to low-end of quad sign-extended
Autumn 2012 Procedures and Stacks 69
University of Washington
Example long int call_proc() call_proc: { subq $32,%rsp long x1 = 1; movq $1,16(%rsp) int x2 = 2; movl $2,24(%rsp) short x3 = 3; movw $3,28(%rsp) char x4 = 4; movb $4,31(%rsp) proc(x1, &x1, x2, &x2, • • • x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); }
Return address to caller of call_proc %rsp
NB: Details may vary depending on compiler.
Autumn 2012 Procedures and Stacks 70 University of Washington
Example long int call_proc() call_proc: { subq $32,%rsp long x1 = 1; movq $1,16(%rsp) int x2 = 2; movl $2,24(%rsp) short x3 = 3; movw $3,28(%rsp) char x4 = 4; movb $4,31(%rsp) proc(x1, &x1, x2, &x2, • • • x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); }
Return address to caller of call_proc x4 x3 x2 x1
%rsp-32
Autumn 2012 Procedures and Stacks 71
University of Washington
Example long int call_proc() call_proc: { • • • long x1 = 1; leaq 24(%rsp),%rcx int x2 = 2; leaq 16(%rsp),%rsi short x3 = 3; leaq 31(%rsp),%rax char x4 = 4; movq %rax,8(%rsp) proc(x1, &x1, x2, &x2, movl $4,(%rsp) x3, &x3, x4, &x4); leaq 28(%rsp),%r9 return (x1+x2)*(x3-x4); movl $3,%r8d } movl $2,%edx movl $1,%edi Return address to caller of call_proc call proc x4 x3 x2 • • • x1 Arg 8 Arg 7 %rsp (old rsp – 32)
Autumn 2012 Procedures and Stacks 72 University of Washington
Example long int call_proc() call_proc: { • • • long x1 = 1; leaq 24(%rsp),%rcx int x2 = 2; leaq 16(%rsp),%rsi short x3 = 3; leaq 31(%rsp),%rax char x4 = 4; movq %rax,8(%rsp) proc(x1, &x1, x2, &x2, movl $4,(%rsp) x3, &x3, x4, &x4); leaq 28(%rsp),%r9 return (x1+x2)*(x3-x4); movl $3,%r8d } movl $2,%edx movl $1,%edi Return address to caller of call_proc call proc x4 x3 x2 • • • x1 Arg 8 Arg 7 Return address to line a er call to proc %rsp
Autumn 2012 Procedures and Stacks 73
University of Washington
Example long int call_proc() call_proc: { • • • long x1 = 1; movswl 28(%rsp),%eax int x2 = 2; movsbl 31(%rsp),%edx short x3 = 3; subl %edx,%eax char x4 = 4; cltq proc(x1, &x1, x2, &x2, movslq 24(%rsp),%rdx x3, &x3, x4, &x4); addq 16(%rsp),%rdx return (x1+x2)*(x3-x4); imulq %rdx,%rax } addq $32,%rsp ret Return address to caller of call_proc x4 x3 x2 x1 Arg 8 Arg 7 %rsp
Autumn 2012 Procedures and Stacks 74 University of Washington
Example long int call_proc() call_proc: { • • • long x1 = 1; movswl 28(%rsp),%eax int x2 = 2; movsbl 31(%rsp),%edx short x3 = 3; subl %edx,%eax char x4 = 4; cltq proc(x1, &x1, x2, &x2, movslq 24(%rsp),%rdx x3, &x3, x4, &x4); addq 16(%rsp),%rdx return (x1+x2)*(x3-x4); imulq %rdx,%rax } addq $32,%rsp ret Return address to caller of call_proc %rsp
Autumn 2012 Procedures and Stacks 75
University of Washington
x86-64 Procedure Summary
¢ Heavy use of registers (faster than using stack in memory) § Parameter passing § More temporaries since more registers
¢ Minimal use of stack § Some mes none (best case, less memory references) § Address rela ve to stack pointer when necessary § No more base pointer § Allocate/deallocate en re block
¢ Many op miza ons § What kind of stack frame to use § Various alloca on techniques
Autumn 2012 Procedures and Stacks 76