EECS 213 MachineLevel Programming III Procedures Apr 21

  • Slides: 43
Download presentation
EECS 213 Machine-Level Programming III: Procedures Apr. 21, 2008 Topics n IA 32 stack

EECS 213 Machine-Level Programming III: Procedures Apr. 21, 2008 Topics n IA 32 stack discipline n Register saving conventions Creating pointers to local variables n

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– EECS 213, S’ 08

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– EECS 213, S’ 08

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– EECS 213, S’ 08

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 EECS 213, S’ 08

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 n Address of instruction beyond call Example from disassembly 804854 e: e 8 3 d 06 00 00 8048553: 50 call pushl 8048 b 90 <main> %eax l Return address = 0 x 8048553 Procedure return: n – 6– ret Pop address from stack; Jump to address EECS 213, S’ 08

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 0 x 110 0 x 10 c 0 x 108 123 0 x 108 call pushl 8048 b 90 <main> %eax 8048 b 90 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– EECS 213, S’ 08

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– EECS 213, S’ 08

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 EECS 213, S’ 08

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 EECS 213, S’ 08

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” EECS 213, S’ 08

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

Stack Operation yoo(…) { • • who(); • • } – 12 – Call Chain yoo Frame Pointer %ebp Stack Pointer %esp • • • yoo EECS 213, S’ 08

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

Stack Operation who(…) { • • • am. I(); • • • } – 13 – • • • Call Chain yoo who Frame Pointer %ebp Stack Pointer %esp yoo who EECS 213, S’ 08

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 EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 EECS 213, S’ 08

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 EECS 213, S’ 08

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 movl Set Up 12(%ebp), %ecx 8(%ebp), %edx (%ecx), %eax (%edx), %ebx %eax, (%edx) %ebx, (%ecx) movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 25 – Body Finish EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 – EECS 213, S’ 08

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 4 Rtn adr %esp movl 12(%ebp), %ecx # get yp movl 8(%ebp), %edx # get xp. . . – 29 – • • • 0 Old %ebp Old %ebx %esp Body EECS 213, S’ 08

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 EECS 213, S’ 08

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

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 – EECS 213, S’ 08

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

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 – EECS 213, S’ 08

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 4 Rtn adr Offset Exiting Stack %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 EECS 213, S’ 08

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 EECS 213, S’ 08

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 – EECS 213, S’ 08

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 EECS 213, S’ 08

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 EECS 213, S’ 08

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 Callee – 38 – pre %ebx 8 x 4 Rtn adr 0 Old %ebp -4 Old %ebx %esp EECS 213, S’ 08

Rfact Body Recursion movl 8(%ebp), %ebx cmpl $1, %ebx jle. L 78 leal -1(%ebx),

Rfact Body Recursion movl 8(%ebp), %ebx cmpl $1, %ebx jle. L 78 leal -1(%ebx), %eax pushl %eax call rfact imull %ebx, %eax jmp. L 79. L 78: # movl $1, %eax. L 79: # int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } – 39 – # ebx = x # Compare x : 1 # If <= goto Term # eax = x-1 # Push x-1 # rfact(x-1) # rval * x # Goto done Term: # return val = 1 Done: 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 EECS 213, S’ 08

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 EECS 213, S’ 08

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 – EECS 213, S’ 08

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 4 Rtn adr 0 Old %ebp pre %ebp -4 Old %ebx -8 x-1 pre %ebx pre %ebp 8 x pre %ebx 4 Rtn adr x %ebp %esp 0 Old %ebp %eax x! %ebx Old x %ebx %eax %ebp %esp Rtn adr %ebp %esp x! %ebx Old %ebx %eax x! %ebx Old %ebx – 42 – EECS 213, S’ 08

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 – 43 – Stack frame organization conventions EECS 213, S’ 08