MachineLevel Programming 4 Procedures Topics n stack discipline















































- Slides: 47
Machine-Level Programming 4 Procedures Topics n stack discipline Register saving conventions n Creating pointers to local variables n
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– CMSC 313, F’ 09
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– CMSC 313, F’ 09
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– CMSC 313, F’ 09
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 CMSC 313, F’ 09
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– CMSC 313, F’ 09
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– CMSC 313, F’ 09
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 CMSC 313, F’ 09
Call Chain Example Code Structure yoo(…) { • • who(); • • } n – 9– Call Chain yoo who(…) { • • • am. I(); • • • } Procedure am. I is recursive who am. I(…) { • • am. I(); • • } am. I CMSC 313, F’ 09
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” CMSC 313, F’ 09
Stack Operation yoo(…) { • • who(); • • } Call Chain yoo Frame Pointer %ebp Stack Pointer %esp • • • S yoo T A C K – 11 – CMSC 313, F’ 09
Stack Operation who(…) { • • • am. I(); • • • } Call Chain yoo who Frame Pointer %ebp Stack Pointer %esp • • • S yoo T who A C K – 12 – CMSC 313, F’ 09
Stack Operation am. I(…) { • • am. I(); • • } Call Chain • • • S yoo T yoo who am. I Frame Pointer %ebp Stack Pointer %esp who A am. I C K – 13 – CMSC 313, F’ 09
Stack Operation am. I(…) { • • am. I(); • • } Call Chain S yoo T yoo who am. I Frame Pointer %ebp Stack Pointer %esp – 14 – • • • A am. I C am. I K CMSC 313, F’ 09
Stack Operation am. I(…) { • • am. I(); • • } Call Chain • • • S yoo T yoo who A am. I C am. I Frame Pointer %ebp Stack Pointer %esp – 15 – am. I K CMSC 313, F’ 09
Stack Operation am. I(…) { • • am. I(); • • } Call Chain S yoo T yoo who am. I – 16 – • • • Frame Pointer %ebp Stack Pointer %esp A am. I C am. I K CMSC 313, F’ 09
Stack Operation am. I(…) { • • am. I(); • • } Call Chain • • • S yoo T yoo who am. I Frame Pointer %ebp Stack Pointer %esp who A am. I C am. I K – 17 – CMSC 313, F’ 09
Stack Operation who(…) { • • • am. I(); • • • } Call Chain yoo who am. I Frame Pointer %ebp Stack Pointer %esp • • • S yoo T who A C am. I K – 18 – CMSC 313, F’ 09
Stack Operation am. I(…) { • • } Call Chain • • • S yoo T yoo Frame Pointer %ebp who am. I Stack Pointer %esp who A am. I C am. I K – 19 – CMSC 313, F’ 09
Stack Operation who(…) { • • • am. I(); • • • } Call Chain Frame Pointer %ebp yoo who am. I Stack Pointer %esp • • • S yoo T who A C am. I K – 20 – CMSC 313, F’ 09
Stack Operation yoo(…) { • • who(); • • } Call Chain Stack Pointer %esp yoo S yoo T A who am. I Frame Pointer %ebp • • • am. I C am. I K – 21 – CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 • • • %ebp • • • &zip 2 &zip 1 %esp # Global Var Resulting Stack before jumping to swap Rtn adr CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 CMSC 313, F’ 09
Disassembled swap 080483 a 4 <swap>: 80483 a 4: 55 push %ebp 80483 a 5: 89 e 5 mov %esp, %ebp 80483 a 7: 53 push %ebx 80483 a 8: 8 b 55 08 mov 0 x 8(%ebp), %edx 80483 ab: 8 b 4 d 0 c mov 0 xc(%ebp), %ecx 80483 ae: 8 b 1 a mov (%edx), %ebx 80483 b 0: 8 b 01 mov (%ecx), %eax 80483 b 2: 89 02 mov %eax, (%edx) 80483 b 4: 89 19 mov %ebx, (%ecx) 80483 b 6: 5 b pop %ebx 80483 b 7: c 9 leave 80483 b 8: c 3 ret Calling Code 8048409: e 8 96 ff ff ff call 80483 a 4 <swap> 804840 e: 8 b 45 f 8 mov 0 xfffffff 8(%ebp), %eax – 33 – CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 – CMSC 313, F’ 09
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 – CMSC 313, F’ 09
Pointer Code Top-Level Call int sfact(int x) { int val = 1; s_helper(x, &val); return val; } n – 43 – 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); } } Pass pointer to update location CMSC 313, F’ 09
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 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; } CMSC 313, F’ 09
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 CMSC 313, F’ 09
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 – 46 – l Use access (%edx) to reference memory CMSC 313, F’ 09
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 – 47 – Stack frame organization conventions CMSC 313, F’ 09