Lecture Slides 05 053-Stacklanguages
Lecture Slides 05 053-Stacklanguages
Stack-Based Languages
University of Washington
Stack-Based Languages
Languages that support recursion
e.g., C, Pascal, Java
Code must be re-entrant
Multiple simultaneous instantiations of single procedure
Need some place to store state of each instantiation
Arguments
Local variables
Return pointer
Stack discipline
State for a given procedure needed for a limited time
Starting from when it is called to when it returns
Callee always returns before caller does
Stack allocated in frames
State for a single procedure instantiation
Stack-Based Languages
University of Washington
Stack-Based Languages
University of Washington
Stack Frames
Previous
Contents Frame
Local variables
Function arguments Frame Pointer: %ebp
Return information Frame
Temporary space for
current
proc
Stack Pointer: %esp
Management
Space allocated when procedure is entered
Stack “Top”
“Set-up” code
Space deallocated upon return
“Finish” code
Stack-Based Languages
University of Washington
Stack
Example
yoo(…) yoo
%ebp
{
• yoo
who
• %esp
who();
• amI amI
•
}
amI
amI
Stack-Based Languages
University of Washington
Stack
Example
who(…) yoo
{
• • • yoo
who
amI();
• • • %ebp
amI(); amI amI who
• • •
%esp
}
amI
amI
Stack-Based Languages
University of Washington
Stack
Example
amI(…) yoo
{
• yoo
who
•
amI();
• amI amI who
•
} %ebp
amI
amI
%esp
amI
Stack-Based Languages
University of Washington
Stack
Example
amI(…) yoo
{
• yoo
who
•
amI();
• amI amI who
•
}
amI
amI
amI %ebp
amI
%esp
Stack-Based Languages
University of Washington
Stack
Example
amI(…) yoo
{
• yoo
who
•
amI();
• amI amI who
•
}
amI
amI
amI
amI
%ebp
amI
%esp
Stack-Based Languages
University of Washington
Stack
Example
amI(…) yoo
{
• yoo
who
•
amI();
• amI amI who
•
}
amI
amI
amI %ebp
amI
%esp
Stack-Based Languages
University of Washington
Stack
Example
amI(…) yoo
{
• yoo
who
•
amI();
• amI amI who
•
} %ebp
amI
amI
%esp
amI
Stack-Based Languages
University of Washington
Stack
Example
who(…) yoo
{
• • • yoo
who
amI();
• • • %ebp
amI(); amI amI who
• • •
%esp
}
amI
amI
Stack-Based Languages
University of Washington
Stack
Example
amI(…) yoo
{
• yoo
who
•
•
• amI amI who
•
} %ebp
amI
amI
%esp
amI
Stack-Based Languages
University of Washington
Stack
Example
who(…) yoo
{
• • • yoo
who
amI();
• • • %ebp
amI(); amI amI who
• • •
%esp
}
amI
amI
Stack-Based Languages
University of Washington
Stack
Example
yoo(…) yoo
%ebp
{
• yoo
who
• %esp
who();
• amI amI
•
}
amI
amI
Stack-Based Languages