15 213 The course that gives CMU its

  • Slides: 47
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. 17, 2002 Topics n IA 32 stack discipline n Register saving conventions Creating pointers to local variables n class 07. ppt

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’ 02

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’ 02

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’ 02

Stack Operation Examples pushl %eax popl %edx 0 x 110 0 x 10 c

Stack Operation Examples pushl %eax popl %edx 0 x 110 0 x 10 c 0 x 108 – 5– 123 0 x 108 123 0 x 104 213 %eax 213 %edx 555 213 %esp 0 x 108 0 x 104 %esp 0 x 104 0 x 108 15 -213, F’ 02

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 – 6– ret Pop address from stack; Jump to address 15 -213, F’ 02

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 – 7– 15 -213, F’ 02

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 – 8– 15 -213, F’ 02

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 – 9– state for single procedure instantiation 15 -213, F’ 02

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

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

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 – 11 – 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’ 02

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

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

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

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

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

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

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’ 02

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 – 16 – 15 -213, F’ 02

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

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

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 – 18 – 15 -213, F’ 02

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 – 19 – 15 -213, F’ 02

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 – 20 – 15 -213, F’ 02

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 – 21 – 15 -213, F’ 02

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 – 22 – 15 -213, F’ 02

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 – 23 – Arguments for this call Return Addr Old %ebp Stack Pointer (%esp) Argument Build 15 -213, F’ 02

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; } – 24 – call_swap: • • • pushl $zip 2 pushl $zip 1 call swap • • • # Global Var Resulting Stack &zip 2 &zip 1 Rtn adr %esp 15 -213, F’ 02

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 – 25 – Set Up Body Finish 15 -213, F’ 02

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 – 26 – 15 -213, F’ 02

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 – 27 – 15 -213, F’ 02

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 – 28 – 15 -213, F’ 02

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. . . – 29 – • • • 4 Rtn adr 0 Old %ebp Old %ebx %esp Body 15 -213, F’ 02

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 – 30 – • • • Saved & restored register %ebx movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret 15 -213, F’ 02

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 – 31 – 15 -213, F’ 02

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 – 32 – 15 -213, F’ 02

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 – 33 – 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’ 02

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 – 34 – who: • • • movl 8(%ebp), %edx addl $91125, %edx • • • ret Contents of register %edx overwritten by who 15 -213, F’ 02

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 – 35 – 15 -213, F’ 02

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 – 36 – Register %eax also stores returned value 15 -213, F’ 02

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 – 37 – %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’ 02

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 – 38 – 0 Old %ebp -4 Old %ebx %esp 15 -213, F’ 02

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; } – 39 – 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’ 02

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 – 40 – 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’ 02

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 – 41 – 15 -213, F’ 02

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 – 42 – 15 -213, F’ 02

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 – 43 – Top-Level Call int sfact(int x) { int val = 1; s_helper(x, &val); return val; } Pass pointer to update location 15 -213, F’ 02

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 – 44 – 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’ 02

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; } – 45 – -16 &val x %esp 15 -213, F’ 02

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; • • • } accum*x accum %eax accum*x x %ecx x %edx • • • movl %ecx, %eax # z = x imull (%edx), %eax # z *= *accum movl %eax, (%edx) # *accum = z • • • n n Register %ecx holds x Register %edx holds pointer to accum l Use access (%edx) to reference memory – 46 – 15 -213, F’ 02

Summary The Stack Makes Recursion Work n Private storage for each instance of 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 – 47 – Stack frame organization conventions 15 -213, F’ 02