Machine-Level Programming III: Procedures Jan 30, 2003

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
X86 Programming CS 740 Sept. 12, 2007 Topics Basics Accessing and Moving Data Arithmetic operations Control Flow Procedures Data Structures.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
IA32 Stack Discipline From Last Time
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Introduction to x86 Assembly or “How does someone hack my laptop?”
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Carnegie Mellon Machine-Level Programming III: Switch Statements, 1-D Array and IA32 Procedures Lecture, Mar. 7, 2013 These slides are from
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
MACHINE-LEVEL PROGRAMMING III: SWITCH STATEMENTS AND IA32 PROCEDURES.
1 Assembly Language: Function Calls Jennifer Rexford.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Section 5: Procedures & Stacks
A job ad at a game programming company
Reading Condition Codes (Cont.)
C function call conventions and the stack
CSCE 212 Computer Architecture
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming 1 Introduction
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
IA32 Stack Discipline From Last Time
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
IA32 Stack Discipline From Last Time
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming III: Procedures Sept 18, 2001
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Presentation transcript:

Machine-Level Programming III: Procedures Jan 30, 2003 15-213 “The course that gives CMU its Zip!” Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables

IA32 Stack Region of memory managed with stack discipline Stack “Bottom” Region of memory managed with stack discipline Grows toward lower addresses Register %esp indicates lowest stack address address of top element Increasing Addresses Stack Grows Down Stack Pointer %esp Stack “Top”

IA32 Stack Pushing Pushing pushl Src Fetch operand at Src Decrement %esp by 4 Write operand at address given by %esp Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer %esp -4 Contents of Src Stack “Top”

IA32 Stack Popping Popping popl Dest Read operand at address given by %esp Increment %esp by 4 Write to Dest Stack “Bottom” Increasing Addresses Stack Pointer %esp Stack Grows Down +4 Goes to Dest Stack “Top”

Stack Operation Examples pushl %eax popl %edx 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 0x104 213 213 %eax 213 %eax 213 213 %eax 213 %edx 555 %edx 555 %edx 555 %esp 0x108 %esp 0x104 0x108 %esp 0x104 0x108

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 value Address of instruction beyond call Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop address from stack; Jump to address

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x110 0x10c 0x108 123 %esp 0x108 %eip 0x804854e %eip is the program counter

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x110 0x10c (bump up %eip to next instruction) 0x108 123 %esp 0x108 %eip 0x804854e %eip is the program counter

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x110 0x10c (bump up %eip to next instruction) 0x108 123 (save %eip on stack) %esp 0x108 %eip 0x8048553 %eip is the program counter

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x110 0x10c (bump up %eip to next instruction) 0x108 123 0x104 (save %eip on stack) %esp 0x104 %eip 0x8048553 0x8048553 %eip is the program counter

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 8048b90 0x110 0x10c (bump up %eip to next instruction) 0x108 123 0x104 0x8048553 (save %eip on stack) %esp 0x104 %eip 0x8048553 0x (set %eip to dest adr) %eip is the program counter

Procedure Return Example 8048591: c3 ret 0x110 0x10c 0x108 123 0x104 0x8048553 %esp 0x104 %eip 0x8048591 0x %eip is the program counter

Procedure Return Example 8048591: c3 ret (incr %eip) 0x110 0x10c 0x108 123 (pop top of stack to %eip) 0x104 0x8048553 0x8048553 %esp 0x104 %eip 0x8048591 0x8048592 %eip is the program counter

Procedure Return Example 8048591: c3 ret (incr %eip) 0x110 0x10c 0x108 123 (pop top of stack to %eip) 0x104 0x8048553 0x8048553 %esp 0x108 %eip 0x8048591 0x8048553 %eip is the program counter

Stack-Based Languages Languages that Support Recursion e.g., C, Pascal, Java Code must be “Reentrant” Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation Arguments Local variables Return pointer Stack Discipline Callee returns before caller does State for given procedure needed for limited time When? Stack Allocated in Frames state for single procedure instantiation

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

Call Tree Example Code Structure Call Tree Procedure amI recursive yoo(…) { • who(); } yoo who(…) { • • • amI(); } who amI amI amI(…) { • amI(); } amI amI Procedure amI recursive

Stack Frames Contents Management Pointers Local variables Return information Temporary space Management Space allocated when enter procedure “Set-up” code (prolog) Deallocated when return “Finish” code (epilog) Pointers Stack pointer %esp indicates stack top Frame pointer %ebp indicates base of current frame yoo who amI Frame Pointer %ebp proc Stack Pointer %esp Stack “Top”

Stack Operation Call Tree • Frame Pointer %ebp yoo yoo(…) { • Stack who(); } Stack Pointer %esp yoo

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) • • • amI(); Frame Pointer %ebp yoo who who Stack Pointer %esp

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) • • • amI(); amI(…) yoo who Frame Pointer %ebp who amI amI Stack Pointer %esp

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) { • • • amI(); } amI(…) { • amI(); } amI(…) { • amI(); } yoo who who amI amI Frame Pointer %ebp amI amI Stack Pointer %esp

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) { • • • amI(); } amI(…) { • amI(); } amI(…) { • amI(); } amI(…) { • amI(); } yoo who who amI amI amI amI Frame Pointer %ebp amI amI Stack Pointer %esp

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) { • • • amI(); } amI(…) { • amI(); } amI(…) { • amI(); } yoo who who amI amI Frame Pointer %ebp amI amI Stack Pointer %esp amI

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) { • • • amI(); } amI(…) { • amI(); } yoo who Frame Pointer %ebp who amI amI Stack Pointer %esp amI amI

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) { • • • amI(); } Frame Pointer %ebp yoo who who Stack Pointer %esp amI amI amI

Stack Operation Call Tree • yoo yoo(…) { • who(); } who(…) { • • • amI(); } amI(…) { • amI(); } yoo who Frame Pointer %ebp who amI amI amI Stack Pointer %esp amI amI

Stack Operation Call Tree • yoo yoo(…) { • who(); } Frame Pointer %ebp • • • amI(); } yoo who who Stack Pointer %esp amI amI amI amI

Stack Operation Call Tree • Frame Pointer %ebp yoo yoo(…) { • who(); } %esp yoo who amI amI amI amI

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) Parameters for function about to call “Argument build” Local variables If can’t keep in registers Saved register context Old frame pointer Caller Stack Frame Return address Pushed by call instruction Arguments for this call Caller Frame Arguments Return Addr Frame Pointer (%ebp) Old %ebp Saved Registers + Local Variables Argument Build Stack Pointer (%esp)

Calling swap from call_swap Revisiting swap Calling swap from call_swap int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } call_swap: • • • pushl $zip2 # Global Var pushl $zip1 # Global Var call swap • Resulting Stack void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Rtn adr %esp

Revisiting swap 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 Prolog void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Epilog

swap Prolog #1 Resulting Entering Stack Stack swap: pushl %ebp • %ebp yp xp Rtn adr Old %ebp %ebp • %esp &zip2 &zip1 Rtn adr %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

swap Prolog #2 Resulting Entering Stack Stack swap: pushl %ebp • %ebp • &zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp swap: pushl %ebp movl %esp,%ebp pushl %ebx %esp

swap Prolog #3 Resulting Entering Stack Stack Why save %ebx? swap: • %ebp • &zip2 Why save %ebx? yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp swap: pushl %ebp movl %esp,%ebp pushl %ebx Old %ebx %esp

Effect of swap Prolog Body Entering Stack Resulting Stack Offset • %ebp • Offset (relative to %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 . . . Body

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

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

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

swap Finish #4 swap’s Stack Exiting Stack movl -4(%ebp),%ebx • %ebp • %ebp Exiting Stack Offset 12 yp &zip2 8 xp &zip1 %esp 4 Rtn adr %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Observation Saved & restored register %ebx Didn’t for %eax, %ecx, or %edx

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

Register Saving Conventions When procedure yoo calls who:  yoo is the caller, who is the callee Can Register be Used for Temporary Storage? Conventions “Caller Save” Caller saves temporary in its frame before calling “Callee Save” Callee saves temporary in its frame before using

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

Swap: 1 mo’ time callswap: pushl %ebp movl %esp,%ebp subl $24,%esp movl $1,-8(%ebp) addl $-8,%esp movl $2,-4(%ebp) leal -4(%ebp),%eax pushl %eax leal -8(%ebp),%eax call _swap movl -4(%ebp),%eax addl $-4,%esp movl -8(%ebp),%eax pushl $LC0 call _printf movl %ebp,%esp popl %ebp ret Void callswap(void) { int x = 1; int y = 2; swap(&x, &y); printf(“%d %d”, x, y); } void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }

Callswap’s frame callswap: pushl %ebp movl %esp,%ebp subl $24,%esp movl $1,-8(%ebp) addl $-8,%esp movl $2,-4(%ebp) leal -4(%ebp),%eax pushl %eax leal -8(%ebp),%eax call _swap movl -4(%ebp),%eax addl $-4,%esp movl -8(%ebp),%eax pushl $LC0 call _printf movl %ebp,%esp popl %ebp ret Void callswap(void) { int x = 1; int y = 2; swap(&x, &y); printf(“%d %d”, x, y); } Caller’s %ebp y x

After swap’s prolog Caller’s %ebp y x Void callswap(void) { int x = 1; int y = 2; swap(&x, &y); printf(“%d %d”, x, y); } void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &y &x Swap’s Frame RetAdr caller’s $ebx

Recursive Factorial Prolog Where is X? Where is rval? Epilog .globl rfact .type rfact,@function rfact: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx cmpl $1,%ebx 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 Recursive Factorial int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } Epilog Where is X? Where is rval?

Recursive Factorial Prolog Where is X? 8(%ebp), %ebx .globl rfact .type rfact,@function rfact: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx cmpl $1,%ebx 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 Recursive Factorial int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } Prolog Where is X? 8(%ebp), %ebx Where is rval? %eax Epilog

Can now see that argument, x, is at 8(%ebp) Rfact Stack Prolog pre %ebp %ebp x Rtn adr Caller %esp pre %ebx Entering Stack rfact: pushl %ebp movl %esp,%ebp pushl %ebx rfact: pushl %ebp movl %esp,%ebp pushl %ebx rfact: pushl %ebp movl %esp,%ebp pushl %ebx pre %ebp Old %ebp Caller pre %ebx 4 8 %ebp -4 x Can now see that argument, x, is at 8(%ebp) Rtn adr Callee %esp Old %ebx

Rfact Body Registers %ebx Stored value of x %eax movl 8(%ebp),%ebx # ebx = x cmpl $1,%ebx # Compare x : 1 jle .L78 # 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 .L79 # Goto done .L78: # Term: movl $1,%eax # return val = 1 .L79: # Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call

Rfact Body Registers %ebx Stored value of x %eax movl 8(%ebp),%ebx # ebx = x cmpl $1,%ebx # Compare x : 1 jle .L78 # 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 .L79 # Goto done .L78: # Term: movl $1,%eax # return val = 1 .L79: # Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call

Rfact Body Registers %ebx Stored value of x %eax movl 8(%ebp),%ebx # ebx = x cmpl $1,%ebx # Compare x : 1 jle .L78 # 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 .L79 # Goto done .L78: # Term: movl $1,%eax # return val = 1 .L79: # Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call

Rfact Body Recursion Registers %ebx Stored value of x %eax movl 8(%ebp),%ebx # ebx = x cmpl $1,%ebx # Compare x : 1 jle .L78 # 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 .L79 # Goto done .L78: # Term: movl $1,%eax # return val = 1 .L79: # Done: Recursion int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call

Rfact Body Recursion Registers %ebx Stored value of x %eax movl 8(%ebp),%ebx # ebx = x cmpl $1,%ebx # Compare x : 1 jle .L78 # 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 .L79 # Goto done .L78: # Term: movl $1,%eax # return val = 1 .L79: # Done: Recursion int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call

Recursive Factorial Registers %eax used without first saving .globl rfact .type rfact,@function rfact: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx cmpl $1,%ebx 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 Recursive Factorial int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } Registers %eax used without first saving %ebx used, but save at beginning & restore at end

Rfact Recursion x-1 leal -1(%ebx),%eax %ebp pushl %eax %esp x-1 %ebp Rtn adr call rfact x x Rtn adr Old %ebp Old %ebx x-1 %eax %ebx x Rtn adr Old %ebp Old %ebx x-1 %eax %ebx Rtn adr Old %ebp %ebp Old %ebx %esp %eax %ebx x … leal -1(%ebx),%eax # eax = x-1 pushl %eax # Push x-1 call rfact # rfact(x-1)

After Rfact Recursion? %ebx has value of x %eax has value of (x-1)! leal -1(%ebx),%eax %ebp pushl %eax %esp x-1 %ebp %esp Rtn adr call rfact x x Rtn adr Old %ebp Old %ebx x-1 %eax %ebx x Rtn adr Old %ebp Old %ebx x-1 %eax %ebx Rtn adr Old %ebp %ebp Old %ebx %esp %eax %ebx x %ebx has value of x %eax has value of (x-1)! … leal -1(%ebx),%eax # eax = x-1 pushl %eax # Push x-1 call rfact # rfact(x-1) imull %ebx,%eax # rval * x

Rfact Result Return from Call imull %ebx,%eax x! x x Rtn adr Rtn adr Old %ebp %ebp Old %ebp %ebp Old %ebx Old %ebx x-1 %esp x-1 %esp %eax (x-1)! (x-1)! %eax (x-1)! %ebx x %ebx x Assume that rfact(x-1) returns (x-1)! in register %eax

Rfact Epilog movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret pre %ebp x Rtn adr Old %ebp %ebp 4 8 %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx x Rtn adr %ebp %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx pre %ebx 8 x 4 Rtn adr Old %ebp %ebp -4 Old %ebx Old %ebx -8 x-1 %esp %eax x! %ebx x

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

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

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

Using Pointer Register %ecx holds x void s_helper (int x, int *accum) { • • • int z = *accum * x; *accum = z; } accum*x %edx accum x %eax accum*x %ecx x • • • movl %ecx,%eax # z = x imull (%edx),%eax # z *= *accum movl %eax,(%edx) # *accum = z Register %ecx holds x Register %edx holds pointer to accum Use access (%edx) to reference memory

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