ECE 445 – Computer Organization

advertisement
ECE 445 – Computer Organization
The Stack Pointer and the Frame Pointer
(Lecture #19)
The slides included herein were taken from the materials accompanying
Computer Organization and Design, 4th Edition, by Patterson and Hennessey,
and were used with permission from Morgan Kaufmann Publishers.
Material to be covered ...
Chapter 2: Sections 8 – 9
Some Basic Definitions

Stack Pointer


Frame Pointer


A value denoting the most recently allocated address in a stack
that shows where registers should be spilled or where old
register values can be found.
A value denoting the location of the saved registers and local
variables for a given procedure.
Procedure Frame (aka. Activation Record)

The segment of the stack containing a procedure's saved
registers and local variables.

Caller:
A program or function that calls a procedure

Callee: The procedure that is called
ECE 445 - Computer Organization
3
Frame Pointer


The frame pointer points to the highest address in
the procedure frame (or activation record).
The frame pointer remains fixed at this memory
location for the duration of the procedure.


Whereas, the stack pointer moves each time an
element is added to or removed from the stack.
The frame pointer must be preserved across
procedure calls.
ECE 445 - Computer Organization
4
Stack Allocation and the
Frame Pointer
$fp points to the highest address
of the procedure frame
stack allocation when a procedure is called
before
procedure call
after
procedure call
$sp points to the “top” of the stack
Local data – stack space allocated by the called procedure
ECE 445 - Computer Organization
5
Example:
Calling CalculateF
ECE 445 - Computer Organization
6
CalculateF: f = g*(a+b+c+d+e*2)
1000 main
3000 Sum2( pa, pb, pc, pd, pe )
{ ..
f = CalculateF( a, b, c, d, e, g );
1024
..
print( f );
}
2000 CalculateF( pa, pb, pc, pd, pe, pg )
{
x = Sum2( a, b, c, d, e );
pf = Prod1( x, g );
return( pf );
2004
{
px = pa + pb + pc + pd + pe*2;
return( px );
3004
}
Prod1( px, pg )
4000 {
y = pg * px;
return( y );
4004
}
}
ECE 445 - Computer Organization
7
CalculateF: f = g*(a+b+c+d+e*2)

The main program calls CalculateF to carry out the arithmetic operation.
CalculateF calls Sum2 to carry out the addition.
 CalculateF then calls Prod1 to carry out the multiplication.

Main
calls
Before calling Sum2:
Before returning to Main:
1. Save return address (of main)
on the stack.
2. Save argument e on the stack.
CalculateF
f = g*(a+b+c+d+e*2)
calls
1. Restore return address (of main)
from the stack.
2. Restore stack (i.e. stack pointer).
calls
Sum2
Prod1
f = (a+b+c+d+e*2)
f = g*(sum)
ECE 445 - Computer Organization
8
Main calls CalculateF
Local Data



Data local to Main function is
pushed on the stack.
Stack
$fp
Parameters to be passed to the
called function are also pushed
onto the stack.
$sp
Stack Pointer points to the last
element pushed onto the
stack.
high address
data
e
main
g
.
.
.
Saved arguments
jal CalculateF
procedure call in Main
ECE 445 - Computer Organization
low address
9
Main calls CalculateF


Main program pushes
arguments (e and g) onto
the stack.
Stack
$fp
$sp

$ra = return
address of Main

PC = CalculateF
data
e
Main function executes
jal CalculateF
high address
main
g
.
.
.
Saved arguments
from calling program
low address
ECE 445 - Computer Organization
10
Stack Usage by CalculateF
high address
Stack
data
e
Main
1. Frame pointer from Main
pushed onto stack.
2. Frame pointer for CalculateF set
equal to stack pointer
($fp = $sp).
g

$fp, $sp
$fp (from Main)
.
.
.
low address
Stack pointer and Frame pointer
now point to the highest
address in the procedure frame
for CalculateF.
CalculateF

All arguments are now relative
to the frame pointer.
addi $sp, $sp -4
sw $fp, 0($sp)
add $fp, $sp, $zero
ECE 445 - Computer Organization
11
Stack Usage byCalculateF
high address
Stack
data
e
Main
• Return address is pushed
onto the stack.
• Stack pointer adjusted.
g

$fp
$fp (from Main)
$sp
$ra (from Main)
It is now possible to call
another function.
CalculateF
.
.
.
addi $sp, $sp, -4
sw $ra, 0($sp)
low address
ECE 445 - Computer Organization
12
Stack Usage by CalculateF
high address
Stack
data
e
Main
g
$fp
Read argument e from stack.
Multiply e by 2.
Push value onto stack.
Adjust stack pointer.
$fp (from Main)
$ra (from Main)
$sp
e*2
CalculateF
.
.
.
lw $t0, 8($fp)
sll $t0, $t0, 1
addi $sp, $sp, -4
sw $t0, 0($sp)
low address
ECE 445 - Computer Organization
13
CalculateF calls Sum2
ECE 445 - Computer Organization
14
CalculateF: f = g*(a+b+c+d+e*2)

CalculateF calls Sum2

Arguments passed to Sum2
a, b, c, d → $a0 - $a3

e*2 → last element on stack
Return address ($ra) for Main pushed onto stack before call

Value returned to CalculateF in register $v0
jal Sum2
procedure call in CalculateF
Sum2: lw $t0, 0($sp)
return value from Sum2
# f=(e*2)
add $t0, $t0, $a0
# f=(e*2)+a
add $t0, $t0, $a1
# f=(e*2)+a+b
add $t0, $t0, $a2
# f=(e*2)+a+b+c
add $t0, $t0, $a3
# f=(e*2)+a+b+c+d
add $v0, $t0, $zero # return value
jr $ra
# return control
ECE 445 - Computer Organization
15
CalculateF calls Prod1
ECE 445 - Computer Organization
16
CalculateF: f = g*(a+b+c+d+e*2)

CalculateF calls Prod1

Arguments passed to Prod1
sum from Sum2 → $a0

g → $a1
Return address ($ra) for Main pushed onto stack before call

Value returned to CalculateF in register $v0
CalculateF: ...
...
passing the sum to Prod1
add $a0, $v0, $zero # sum from Sum2
lw $a1, 4($fp)
# read g from stack
jal Prod1
# call Prod1
passing g to Prod1
procedure call to Prod1
ECE 445 - Computer Organization
17
CalculateF returns to Main
ECE 445 - Computer Organization
18
Restore $sp, $fp, $ra and Return
high address
Stack
data
e
main
g
$fp
Read $ra from stack.
Restore $sp.
Read $fp from stack.
Return to main.
$fp (from Main)
$ra (from Main)
$sp
e*2
CalculateF
.
.
.
lw $ra, -4($fp)
add $sp, $fp, $zero
lw $fp, 0($sp)
addi $sp, $sp, 4
jr $ra
low address
ECE 445 - Computer Organization
19
Memory Layout
ECE 445 - Computer Organization
20
Memory Layout


Text: program code
Static data: global variables



Dynamic data: heap


e.g., static variables in C,
constant arrays and strings
$gp initialized to address
allowing ±offsets into this
segment
e.g., malloc in C, new in Java
Stack: automatic storage
Heap – memory allocated for dynamic data.
The heap and stack grow towards each other in the same memory space.
ECE 445 - Computer Organization
21
Questions?
Fall 2010
ECE 445 - Computer Organization
22
Download