15 213 The course that gives CMU its

  • Slides: 58
Download presentation
15 -213 “The course that gives CMU its Zip!” Machine-Level Programming III: Procedures Sept.

15 -213 “The course that gives CMU its Zip!” Machine-Level Programming III: Procedures Sept. 15, 2006 x 86 -64 IA 32 n n n stack discipline Register saving conventions n Argument passing in registers Creating pointers to local variables n Minimizing stack usage n Using stack pointer as only reference class 06. ppt 15 -213, F’ 06

IA 32 Stack n Region of memory managed with stack discipline n Grows toward

IA 32 Stack n Region of memory managed with stack discipline n Grows toward lower addresses Register %esp indicates lowest stack address n Stack “Bottom” Increasing Addresses l address of top element Stack Pointer %esp Stack Grows Down Stack “Top” – 2– 15 -213, F’ 06

IA 32 Stack Pushing n pushl Src n Fetch operand at Src Decrement %esp

IA 32 Stack Pushing n pushl Src n Fetch operand at Src Decrement %esp by 4 n n Stack “Bottom” Increasing Addresses Write operand at address given by %esp Stack Pointer %esp Stack Grows Down -4 Stack “Top” – 3– 15 -213, F’ 06

IA 32 Stack Popping n popl Dest n n Read operand at address given

IA 32 Stack Popping n popl Dest n n Read operand at address given by %esp Increment %esp by 4 n Write to Dest Stack “Bottom” Increasing Addresses Stack Pointer %esp Stack Grows Down +4 Stack “Top” – 4– 15 -213, F’ 06

Procedure Control Flow n Use stack to support procedure call and return Procedure call:

Procedure Control Flow n Use stack to support procedure call and return Procedure call: Push return address on stack; Jump call label to label Return address value n Address of instruction beyond call Example from disassembly 804854 e: e 8 3 d 06 00 00 8048553: 50 n call 8048 b 90 <main> pushl %eax l Return address = 0 x 8048553 Procedure return: n – 5– ret Pop address from stack; Jump to address 15 -213, F’ 06

Procedure Call Example 804854 e: 8048553: e 8 3 d 06 00 00 50

Procedure Call Example 804854 e: 8048553: e 8 3 d 06 00 00 50 call 8048 b 90 <main> pushl %eax call 8048 b 90 0 x 110 0 x 10 c 0 x 108 123 0 x 104 0 x 8048553 %esp 0 x 108 %esp %eip 0 x 804854 e 0 x 108 0 x 104 %eip 0 x 8048 b 90 0 x 804854 e %eip is program counter – 6– 15 -213, F’ 06

Procedure Return Example 8048591: c 3 ret 0 x 110 0 x 10 c

Procedure Return Example 8048591: c 3 ret 0 x 110 0 x 10 c 0 x 108 123 0 x 108 0 x 104 0 x 8048553 %esp 0 x 104 %eip 0 x 8048591 123 0 x 8048553 %esp 0 x 104 0 x 108 %eip 0 x 8048553 0 x 8048591 %eip is program counter – 7– 15 -213, F’ 06

Stack-Based Languages that Support Recursion n e. g. , C, Pascal, Java n Code

Stack-Based Languages that Support Recursion n e. g. , C, Pascal, Java n Code must be “Reentrant” l Multiple simultaneous instantiations of single procedure n Need some place to store state of each instantiation l Arguments l Local variables l Return pointer Stack Discipline n State for given procedure needed for limited time l From when called to when return n Callee returns before caller does Stack Allocated in Frames n – 8– state for single procedure instantiation 15 -213, F’ 06

Call Chain Example Code Structure yoo(…) { • • who(); • • } n

Call Chain Example Code Structure yoo(…) { • • who(); • • } n – 9– Call Chain yoo who(…) { • • • am. I(); • • • } Procedure am. I recursive who am. I(…) { • • am. I(); • • } am. I 15 -213, F’ 06

Stack Frames Contents n Local variables n Return information Temporary space n yoo who

Stack Frames Contents n Local variables n Return information Temporary space n yoo who am. I Management n Space allocated when enter procedure l “Set-up” code n Deallocated when return l “Finish” code Pointers n n – 10 – Stack pointer %esp indicates stack top Frame pointer %ebp indicates start of current frame Frame Pointer %ebp Stack Pointer %esp proc Stack “Top” 15 -213, F’ 06

Stack Operation yoo(…) { • • who(); • • } – 11 – Call

Stack Operation yoo(…) { • • who(); • • } – 11 – Call Chain yoo Frame Pointer %ebp Stack Pointer %esp • • • yoo 15 -213, F’ 06

Stack Operation who(…) { • • • am. I(); • • • } –

Stack Operation who(…) { • • • am. I(); • • • } – 12 – • • • Call Chain yoo who Frame Pointer %ebp Stack Pointer %esp yoo who 15 -213, F’ 06

Stack Operation am. I(…) { • • am. I(); • • } – 13

Stack Operation am. I(…) { • • am. I(); • • } – 13 – • • • Call Chain yoo who am. I Frame Pointer %ebp Stack Pointer %esp who am. I 15 -213, F’ 06

Stack Operation am. I(…) { • • am. I(); • • } • •

Stack Operation am. I(…) { • • am. I(); • • } • • • Call Chain yoo who am. I Frame Pointer %ebp am. I Stack Pointer %esp – 14 – 15 -213, F’ 06

Stack Operation am. I(…) { • • am. I(); • • } • •

Stack Operation am. I(…) { • • am. I(); • • } • • • Call Chain yoo who am. I Frame Pointer %ebp am. I Stack Pointer %esp – 15 -213, F’ 06

Stack Operation am. I(…) { • • am. I(); • • } Call Chain

Stack Operation am. I(…) { • • am. I(); • • } Call Chain yoo who am. I – 16 – • • • Frame Pointer %ebp am. I Stack Pointer %esp 15 -213, F’ 06

Stack Operation am. I(…) { • • am. I(); • • } • •

Stack Operation am. I(…) { • • am. I(); • • } • • • Call Chain yoo who am. I Frame Pointer %ebp Stack Pointer %esp who am. I – 17 – 15 -213, F’ 06

Stack Operation who(…) { • • • am. I(); • • • } •

Stack Operation who(…) { • • • am. I(); • • • } • • • Call Chain yoo who am. I Frame Pointer %ebp Stack Pointer %esp yoo who am. I – 18 – 15 -213, F’ 06

Stack Operation am. I(…) { • • } • • • Call Chain yoo

Stack Operation am. I(…) { • • } • • • Call Chain yoo Frame Pointer %ebp who am. I Stack Pointer %esp who am. I – 19 – 15 -213, F’ 06

Stack Operation who(…) { • • • am. I(); • • • } •

Stack Operation who(…) { • • • am. I(); • • • } • • • Call Chain Frame Pointer %ebp yoo who am. I Stack Pointer %esp yoo who am. I – 20 – 15 -213, F’ 06

Stack Operation yoo(…) { • • who(); • • } Call Chain Frame Pointer

Stack Operation yoo(…) { • • who(); • • } Call Chain Frame Pointer %ebp Stack Pointer %esp yoo • • • yoo who am. I – 21 – 15 -213, F’ 06

IA 32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) n Parameters for function

IA 32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) n Parameters for function about to call Caller Frame l “Argument build” n Local variables l If can’t keep in registers n n Arguments Frame Pointer (%ebp) Saved register context Old frame pointer Saved Registers + Local Variables Caller Stack Frame n Return address l Pushed by call instruction n – 22 – Arguments for this call Return Addr Old %ebp Stack Pointer (%esp) Argument Build 15 -213, F’ 06

Revisiting swap Calling swap from call_swap int zip 1 = 15213; int zip 2

Revisiting swap Calling swap from call_swap int zip 1 = 15213; int zip 2 = 91125; void call_swap() { swap(&zip 1, &zip 2); } void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } – 23 – call_swap: • • • pushl $zip 2 pushl $zip 1 call swap • • • # Global Var Resulting Stack &zip 2 &zip 1 Rtn adr %esp 15 -213, F’ 06

Revisiting swap void swap(int *xp, int *yp) { int t 0 = *xp; int

Revisiting swap void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } swap: pushl %ebp movl %esp, %ebp pushl %ebx movl 12(%ebp), %ecx movl 8(%ebp), %edx movl (%ecx), %eax movl (%edx), %ebx movl %eax, (%edx) movl %ebx, (%ecx) movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 24 – Set Up Body Finish 15 -213, F’ 06

swap Setup #1 Resulting Stack Entering Stack %ebp • • • &zip 2 yp

swap Setup #1 Resulting Stack Entering Stack %ebp • • • &zip 2 yp &zip 1 xp Rtn adr %esp Rtn adr Old %ebp %esp swap: pushl %ebp movl %esp, %ebp pushl %ebx – 25 – 15 -213, F’ 06

swap Setup #2 Resulting Stack Entering Stack %ebp • • • &zip 2 yp

swap Setup #2 Resulting Stack Entering Stack %ebp • • • &zip 2 yp &zip 1 xp Rtn adr %esp Rtn adr Old %ebp %esp swap: pushl %ebp movl %esp, %ebp pushl %ebx – 26 – 15 -213, F’ 06

swap Setup #3 Resulting Stack Entering Stack %ebp • • • &zip 2 yp

swap Setup #3 Resulting Stack Entering Stack %ebp • • • &zip 2 yp &zip 1 xp Rtn adr %esp Rtn adr Old %ebp Old %ebx %esp swap: pushl %ebp movl %esp, %ebp pushl %ebx – 27 – 15 -213, F’ 06

Effect of swap Setup Entering Stack Resulting Stack %ebp • • • Offset (relative

Effect of swap Setup Entering Stack Resulting Stack %ebp • • • Offset (relative to %ebp) &zip 2 12 yp &zip 1 8 xp Rtn adr %esp movl 12(%ebp), %ecx # get yp movl 8(%ebp), %edx # get xp. . . – 28 – • • • 4 Rtn adr 0 Old %ebp Old %ebx %esp Body 15 -213, F’ 06

swap Finish #1 swap’s Stack Offset • • • Offset 12 yp 8 xp

swap Finish #1 swap’s Stack Offset • • • Offset 12 yp 8 xp 4 Rtn adr 0 Old %ebp -4 Old %ebx %esp Observation n – 29 – • • • Saved & restored register %ebx movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret 15 -213, F’ 06

swap Finish #2 swap’s Stack Offset swap’s Stack • • • Offset 12 yp

swap Finish #2 swap’s Stack Offset swap’s Stack • • • Offset 12 yp 8 xp 4 Rtn adr 0 Old %ebp -4 Old %ebx %esp 0 Old %ebp %esp movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 30 – 15 -213, F’ 06

swap Finish #3 swap’s Stack Offset %ebp swap’s Stack • • • Offset 12

swap Finish #3 swap’s Stack Offset %ebp swap’s Stack • • • Offset 12 yp 8 xp 4 Rtn adr 0 Old %ebp %esp movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 31 – 15 -213, F’ 06

swap Finish #4 %ebp swap’s Stack %ebp • • • 12 yp &zip 2

swap Finish #4 %ebp swap’s Stack %ebp • • • 12 yp &zip 2 8 xp &zip 1 Offset Exiting Stack %esp 4 Rtn adr %esp Observation n n – 32 – Saved & restored register %ebx Didn’t do so for %eax, %ecx, or %edx movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret 15 -213, F’ 06

Register Saving Conventions When procedure yoo calls who: n yoo is the caller, who

Register Saving Conventions When procedure yoo calls who: n yoo is the caller, who is the callee Can Register be Used for Temporary Storage? yoo: • • • movl $15213, %edx call who addl %edx, %eax • • • ret n – 33 – who: • • • movl 8(%ebp), %edx addl $91125, %edx • • • ret Contents of register %edx overwritten by who 15 -213, F’ 06

Register Saving Conventions When procedure yoo calls who: n yoo is the caller, who

Register Saving Conventions When procedure yoo calls who: n yoo is the caller, who is the callee Can Register be Used for Temporary Storage? Conventions n “Caller Save” l Caller saves temporary in its frame before calling n “Callee Save” l Callee saves temporary in its frame before using – 34 – 15 -213, F’ 06

IA 32/Linux Register Usage Integer Registers n Two have special uses %ebp, %esp n

IA 32/Linux Register Usage Integer Registers n Two have special uses %ebp, %esp n Three managed as callee-save %eax Caller-Save Temporaries %ecx %ebx, %esi, %edi l Old values saved on stack prior to using n Callee-Save Temporaries l Do what you please, %esi %edi Three managed as caller-save %eax, %edx, %ecx %edx Special %esp %ebp but expect any callee to do so, as well n – 35 – Register %eax also stores returned value 15 -213, F’ 06

Recursive Factorial int rfact(int x) { int rval; if (x <= 1) return 1;

Recursive Factorial int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } Registers n n – 36 – %eax used without first saving %ebx used, but save at beginning & restore at end . globl rfact. type rfact, @function rfact: pushl %ebp movl %esp, %ebp pushl %ebx movl 8(%ebp), %ebx cmpl $1, %ebx jle. L 78 leal -1(%ebx), %eax pushl %eax call rfact imull %ebx, %eax jmp. L 79. align 4. L 78: movl $1, %eax. L 79: movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret 15 -213, F’ 06

Rfact Stack Setup Caller pre %ebp pre %ebx Entering Stack x Rtn adr %esp

Rfact Stack Setup Caller pre %ebp pre %ebx Entering Stack x Rtn adr %esp rfact: pushl %ebp movl %esp, %ebp pushl %ebx pre %ebp Caller pre %ebx 8 x 4 Rtn adr Callee – 37 – 0 Old %ebp -4 Old %ebx %esp 15 -213, F’ 06

Rfact Body Recursion movl 8(%ebp), %ebx # ebx = x cmpl $1, %ebx #

Rfact Body Recursion movl 8(%ebp), %ebx # ebx = x cmpl $1, %ebx # Compare x : 1 jle. L 78 # If <= goto Term leal -1(%ebx), %eax # eax = x-1 pushl %eax # Push x-1 call rfact # rfact(x-1) imull %ebx, %eax # rval * x jmp. L 79 # Goto done. L 78: # Term: movl $1, %eax # return val = 1. L 79: # Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } – 38 – Registers %ebx %eax Stored value of x l Temporary value of x-1 l Returned value from rfact(x-1) l Returned value from this call 15 -213, F’ 06

Rfact Recursion leal -1(%ebx), %eax x pushl %eax Rtn adr Old %ebp x Old

Rfact Recursion leal -1(%ebx), %eax x pushl %eax Rtn adr Old %ebp x Old %ebx %esp Rtn adr Old %ebp call rfact %ebp Old %ebx x-1 %eax x-1 %ebx x – 39 – x Rtn adr %esp Old %ebp Old %ebx %eax x-1 %ebx x x-1 Rtn adr %eax x-1 %ebx x %esp 15 -213, F’ 06

Rfact Result imull %ebx, %eax Return from Call x x Rtn adr Old %ebp

Rfact Result imull %ebx, %eax Return from Call x x Rtn adr Old %ebp Old %ebx x-1 %esp %eax (x-1)! x! %ebx x %esp x Assume that rfact(x-1) returns (x-1)! in register %eax – 40 – 15 -213, F’ 06

Rfact Completion movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret pre %ebp pre

Rfact Completion movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret pre %ebp pre %ebx 8 x pre %ebp 4 Rtn adr 0 Old %ebp 8 -4 Old %ebx -8 x-1 %esp pre %ebx pre %ebp x pre %ebx 4 Rtn adr 0 Old %ebp %eax x! %ebx Old %ebx x %eax %ebp %esp Rtn adr %esp x! %ebx Old %ebx %eax x! %ebx Old %ebx – 41 – 15 -213, F’ 06

Pointer Code Recursive Procedure void s_helper (int x, int *accum) { if (x <=

Pointer Code Recursive Procedure void s_helper (int x, int *accum) { if (x <= 1) return; else { int z = *accum * x; *accum = z; s_helper (x-1, accum); } } n – 42 – Top-Level Call int sfact(int x) { int val = 1; s_helper(x, &val); return val; } Pass pointer to update location 15 -213, F’ 06

Creating & Initializing Pointer Initial part of sfact _sfact: pushl %ebp movl %esp, %ebp

Creating & Initializing Pointer Initial part of sfact _sfact: pushl %ebp movl %esp, %ebp subl $16, %esp movl 8(%ebp), %edx movl $1, -4(%ebp) # Save %ebp # Set %ebp # Add 16 bytes # edx = x # val = 1 Using Stack for Local Variable n Variable val must be stored on stack l Need to create pointer to it – 43 – n Compute pointer as 4(%ebp) n Push on stack as second argument 8 x 4 Rtn adr %ebp 0 Old %ebp -4 val = 1 -8 Temp. Space -12 Unused -16 %esp int sfact(int x) { int val = 1; s_helper(x, &val); return val; } 15 -213, F’ 06

Passing Pointer Calling s_helper from sfact leal -4(%ebp), %eax pushl %edx call s_helper movl

Passing Pointer Calling s_helper from sfact leal -4(%ebp), %eax pushl %edx call s_helper movl -4(%ebp), %eax • • • # Compute &val # Push on stack # Push x # call # Return val # Finish Stack at time of call 8 x 4 Rtn adr 0 Old %ebp -4 val =x! val = 1 -8 -12 Unused int sfact(int x) { int val = 1; s_helper(x, &val); return val; } – 44 – -16 &val x %esp 15 -213, F’ 06

Using Pointer void s_helper (int x, int *accum) { • • • int z

Using Pointer void s_helper (int x, int *accum) { • • • int z = *accum * x; *accum = z; • • • } V*x V %eax V*x x %ecx x %edx (accum) • • • movl %ecx, %eax # z = x imull (%edx), %eax # z *= *accum movl %eax, (%edx) # *accum = z • • • n n Register %ecx holds x Register %edx holds accum l Assume memory initally has value V – 45 – l Use access (%edx) to reference memory 15 -213, F’ 06

IA 32 Procedure Summary The Stack Makes Recursion Work n Private storage for each

IA 32 Procedure Summary The Stack Makes Recursion Work n Private storage for each instance of procedure call l Instantiations don’t clobber each other l Addressing of locals + arguments can be relative to stack positions n Can be managed by stack discipline l Procedures return in inverse order of calls IA 32 Procedures Combination of Instructions + Conventions n n Call / Ret instructions Register usage conventions l Caller / Callee save l %ebp and %esp n – 46 – Stack frame organization conventions 15 -213, F’ 06

x 86 -64 General Purpose Registers n – 47 – n %rax %eax %r

x 86 -64 General Purpose Registers n – 47 – n %rax %eax %r 8 d %rbx %ebx %r 9 d %rcx %ecx %r 10 d %rdx %edx %r 11 d %rsi %esi %r 12 d %rdi %edi %r 13 d %rsp %esp %r 14 d %rbp %ebp %r 15 d Twice the number of registers Accessible as 8, 16, 32, or 64 bits 15 -213, F’ 06

x 86 -64 Register Conventions – 48 – %rax Return Value %r 8 Argument

x 86 -64 Register Conventions – 48 – %rax Return Value %r 8 Argument #5 %rbx Callee Saved %r 9 Argument #6 %rcx Argument #4 %r 10 Callee Saved %rdx Argument #3 %r 11 Used for linking %rsi Argument #2 %r 12 C: Callee Saved %rdi Argument #1 %r 13 Callee Saved %rsp Stack Pointer %r 14 %rbp Callee Saved %r 15 Callee Saved 15 -213, F’ 06

x 86 -64 Registers Arguments passed to functions via registers n If more than

x 86 -64 Registers Arguments passed to functions via registers n If more than 6 integral parameters, then pass rest on stack n These registers can be used as caller-saved as well All References to Stack Frame via Stack Pointer n Eliminates need to update %ebp Other Registers – 49 – n 6+1 callee saved n 2 or 3 have special uses 15 -213, F’ 06

x 86 -64 Long Swap void swap(long *xp, long *yp) { long t 0

x 86 -64 Long Swap void swap(long *xp, long *yp) { long t 0 = *xp; long t 1 = *yp; *xp = t 1; *yp = t 0; } n swap: movq ret (%rdi), %rdx (%rsi), %rax, (%rdi) %rdx, (%rsi) Operands passed in registers l First (xp) in %rdi, second (yp) in %rsi l 64 -bit pointers n No stack operations required Avoiding Stack n – 50 – Can hold all local information in registers 15 -213, F’ 06

x 86 -64 Locals in the Red Zone /* Swap, using local array */

x 86 -64 Locals in the Red Zone /* Swap, using local array */ void swap_a(long *xp, long *yp) { volatile long loc[2]; loc[0] = *xp; loc[1] = *yp; *xp = loc[1]; *yp = loc[0]; } Avoiding Stack Pointer Change n Can hold all information within small window beyond stack pointer swap_a: movq (%rdi), %rax movq %rax, -24(%rsp) movq (%rsi), %rax movq %rax, -16(%rsp) movq -16(%rsp), %rax movq %rax, (%rdi) movq -24(%rsp), %rax movq %rax, (%rsi) ret rtn Ptr %rsp − 8 unused − 16 loc[1] − 24 loc[0] – 51 – 15 -213, F’ 06

x 86 -64 Non. Leaf without Stack Frame long scount = 0; /* Swap

x 86 -64 Non. Leaf without Stack Frame long scount = 0; /* Swap a[i] & a[i+1] */ void swap_ele_se (long a[], int i) { swap(&a[i], &a[i+1]); scount++; } n No values held while swap being invoked n No callee save registers needed swap_ele_se: movslq %esi, %rsi # Sign extend i leaq (%rdi, %rsi, 8), %rdi # &a[i] leaq 8(%rdi), %rsi # &a[i+1] call swap # swap() incq scount(%rip) # scount++; ret – 52 – 15 -213, F’ 06

x 86 -64 Call using Jump long scount = 0; /* Swap a[i] &

x 86 -64 Call using Jump long scount = 0; /* Swap a[i] & a[i+1] */ void swap_ele (long a[], int i) { swap(&a[i], &a[i+1]); } n n When swap executes ret, it will return from swap_ele Possible since swap is a “tail call” swap_ele: movslq %esi, %rsi # Sign extend i leaq (%rdi, %rsi, 8), %rdi # &a[i] leaq 8(%rdi), %rsi # &a[i+1] jmp swap # swap() – 53 – 15 -213, F’ 06

x 86 -64 Stack Frame Example long sum = 0; /* Swap a[i] &

x 86 -64 Stack Frame Example long sum = 0; /* Swap a[i] & a[i+1] */ void swap_ele_su (long a[], int i) { swap(&a[i], &a[i+1]); sum += a[i]; } n n – 54 – swap_ele_su: movq %rbx, -16(%rsp) movslq %esi, %rbx movq %r 12, -8(%rsp) movq %rdi, %r 12 leaq (%rdi, %rbx, 8), %rdi subq $16, %rsp leaq 8(%rdi), %rsi Keeps values of a and call swap i in callee save movq (%r 12, %rbx, 8), %rax addq %rax, sum(%rip) registers movq (%rsp), %rbx Must set up stack movq 8(%rsp), %r 12 frame to save these addq $16, %rsp registers ret 15 -213, F’ 06

Understanding x 86 -64 Stack Frame swap_ele_su: movq %rbx, -16(%rsp) # Save %rbx movslq

Understanding x 86 -64 Stack Frame swap_ele_su: movq %rbx, -16(%rsp) # Save %rbx movslq %esi, %rbx # Extend & save i movq %r 12, -8(%rsp) # Save %r 12 movq %rdi, %r 12 # Save a leaq (%rdi, %rbx, 8), %rdi # &a[i] subq $16, %rsp # Allocate stack frame leaq 8(%rdi), %rsi # &a[i+1] call swap # swap() movq (%r 12, %rbx, 8), %rax # a[i] addq %rax, sum(%rip) # sum += a[i] movq (%rsp), %rbx # Restore %rbx movq 8(%rsp), %r 12 # Restore %r 12 addq $16, %rsp # Deallocate stack frame ret – 55 – 15 -213, F’ 06

Stack Operations movq %rbx, -16(%rsp) # Save %rbx %rsp movq %r 12, -8(%rsp) #

Stack Operations movq %rbx, -16(%rsp) # Save %rbx %rsp movq %r 12, -8(%rsp) # Save %r 12 rtn Ptr − 8 %r 12 − 16 %rbx subq $16, %rsp # Allocate stack frame rtn Ptr movq (%rsp), %rbx # Restore %rbx movq 8(%rsp), %r 12 # Restore %r 12 +8 %rsp %r 12 %rbx addq $16, %rsp # Deallocate stack frame – 56 – 15 -213, F’ 06

Interesting Features of Stack Frame Allocate Entire Frame at Once n All stack accesses

Interesting Features of Stack Frame Allocate Entire Frame at Once n All stack accesses can be relative to %rsp n Do by decrementing stack pointer Can delay allocation, since safe to temporarily use red zone n Simple Deallocation n – 57 – Increment stack pointer 15 -213, F’ 06

x 86 -64 Procedure Summary Heavy Use of Registers n Parameter passing n More

x 86 -64 Procedure Summary Heavy Use of Registers n Parameter passing n More temporaries Minimal Use of Stack n n Sometimes none Allocate/deallocate entire block Many Tricky Optimizations n n n – 58 – What kind of stack frame to use Calling with jump Various allocation techniques 15 -213, F’ 06