0% found this document useful (0 votes)
7 views15 pages

Lecture Slides 05 053-Stacklanguages

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Lecture Slides 05 053-Stacklanguages

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

University of Washington

Section 5: Procedures & Stacks


 Stacks in memory and stack operations
 The stack used to keep track of procedure calls
 Return addresses and return values
 Stack-based languages
 The Linux stack frame
 Passing arguments on the stack
 Allocating local variables on the stack
 Register-saving conventions
 Procedures and stacks on x64 architecture

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

Call Chain Example


Example
yoo(…) Call Chain
{
• yoo
• who(…)
who(); { who
• • • •
• amI();
} amI(…) amI amI
• • • {
amI(); •
• • • amI

} amI();
• amI

}

Procedure amI is recursive


(calls itself)

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

You might also like