The Activation Stack - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

The Activation Stack

Description:

Instead of giving each function its own unique activation record ... The Activation Stack. The white box represents this program's share of computer memory. ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 35
Provided by: BillL161
Category:
Tags: activation | stack

less

Transcript and Presenter's Notes

Title: The Activation Stack


1
The Activation Stack
2
Outline
  • 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

3
Stacks
  • 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
4
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

5
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

6
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

7
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

8
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

9
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

10
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

11
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

12
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

13
Once 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

14
Once 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.

15
Once upon a time
  • Each of these functions has some executable code
    and usually some local data like the parameters
    which are passed in.

16
Once 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)
17
Can you see a problem with this approach?
18
Solution
  • 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

19
The 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
20
Adding 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
21
Removing 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
22
How 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 )
  • )

23
How Parameters Work
  • Formal parameters receive a copy of the matching
    actual parameter.

4
4
7
9
this_var
that_var
other_var
24
Stack 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))))
25
Abstraction Example
quadratic
(root1, root2)
26
Further Abstraction
num
pos-numerator
27
Stack 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))))
28
Positive Root Part I
29
Positive Root Part II
30
Negative Root Part I
31
Negative Root Part II
32
Summary
  • You should now know
  • Stack as an Abstract Data Type
  • Nature of the Activation Stack
  • Tracing program execution using the Activation
    Stack

33
Questions?
34
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com