Title: The Activation Stack
1The Activation Stack
2Outline
- Prerequisites
- Basic operation of functions
- Physical and logical components of a computer
system - Objectives
- Stack as an Abstract Data Type
- Nature of the Activation Stack
- Program execution using the Activation Stack
3Stacks
- Is a pile or collection or set of items.
- Items can only be added to the top
- Items can only be taken off the top
- Last-in-first-out (LIFO)
Push
Pop
Thing 3
Thing 2
Thing 1
4What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
5What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
6What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
7What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
8What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
9What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
10What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
11What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
12What is a Stack?
- A data structure for storing items which are
to be accessed in last-in first-out order (LIFO).
13Once upon a time
- Many years ago programmers realized that
programming a complex task would be simpler if
they could break the big job down into pieces
14Once upon a time
- This was the birth of modular programming where
big programs get broken down into smaller pieces
called modules, methods, subroutines, procedures
or in the case of Scheme functions.
15Once upon a time
- Each of these functions has some executable code
and usually some local data like the parameters
which are passed in.
16Once upon a time
- Initially each function had its own little area
for data called its activation record. Thus that
was where the data was stored when the function
was activated or running.
Activation Record for f x
f(x)
Activation Record for g y
g(y)
Activation Record for h z
h(z)
17Can you see a problem with this approach?
18Solution
- Instead of giving each function its own unique
activation record - Every time a function is called it gets a fresh
activation record on a stack called the
activation stack. - Now we call each of these records on the stack a
stack frame
19The Activation Stack
- The white box represents this programs share of
computer memory. - The function being executed gets the first frame
at the bottom of the stack. - Variables which are used within a function reside
in that functions stack frame.
foo
20Adding Modules to the Stack
- When that function calls another function, the
new function gets its own frame pushed on the
stack. - The new functions variables live in that new
frame. - ONLY the top frame is active. All frames
underneath it are stopped.
foo
var1
var2
var3
21Removing modules from the stack
- When the active function completes its
instructions, its frame is popped off the stack
and all of its data dies. - The only thing that survives is the return value
result which is passed back to the calling
function underneath.
foo
var1
var2
var3
22How Parameters Work
- Formal parameters get a copy of the matching
actual parameter. - In the calling function
- (bar this_var)
- In the called function
- (define (bar this_one )
- )
23How Parameters Work
- Formal parameters receive a copy of the matching
actual parameter.
4
4
7
9
this_var
that_var
other_var
24Stack Trace Example
(define (quadratic a b c)(list (pos-root a b
c)Â Â Â (neg-root a b c))) (define (pos-root a b
c)(/ (pos-numerator a b c)Â Â Â (denominator
a))) (define (pos-numerator a b c)( (-
b)Â Â Â (sqrt-of-discriminant a b c))) (define
(denominator a)( a 2.0))
(define (neg-root a b c)(/ (neg-numerator a b
c)Â Â Â Â (denominator a)) (define (neg-numerator a
b c)(- (- b)Â Â Â (sqrt-of-discriminant a b
c))) (define (sqrt-of-discriminant a b c)(sqrt
(- ( b b)Â Â Â ( 4.0 a c))))
25Abstraction Example
quadratic
(root1, root2)
26Further Abstraction
num
pos-numerator
27Stack Trace Example
(define (quadratic a b c)(list (pos-root a b
c)Â Â Â (neg-root a b c))) (define (pos-root a b
c)(/ (pos-numerator a b c)Â Â Â (denominator
a))) (define (pos-numerator a b c)( (-
b)Â Â Â (sqrt-of-discriminant a b c))) (define
(denominator a)( a 2.0))
(define (neg-root a b c)(/ (neg-numerator a b
c)Â Â Â Â (denominator a)) (define (neg-numerator a
b c)(- (- b)Â Â Â (sqrt-of-discriminant a b
c))) (define (sqrt-of-discriminant a b c)(sqrt
(- ( b b)Â Â Â ( 4.0 a c))))
28Positive Root Part I
29Positive Root Part II
30Negative Root Part I
31Negative Root Part II
32Summary
- You should now know
- Stack as an Abstract Data Type
- Nature of the Activation Stack
- Tracing program execution using the Activation
Stack
33Questions?
34(No Transcript)