The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer Organization and Design, 4 th Edition, by Patterson and Hennessey, and were used with permission from Morgan Kaufmann Publishers.
22
Embed
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
The Stack Pointer and the Frame Pointer
(Lecture #19)
ECE 445 – Computer Organization
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
ECE 445 - Computer Organization 3
Some Basic Definitions
Stack 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.
Frame Pointer
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 4
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 5
Stack Allocation and the Frame Pointer
Local data – stack space allocated by the called procedure
stack allocation when a procedure is called
before procedure call
after procedure call
$fp points to the highest addressof the procedure frame
$sp points to the “top” of the stack
ECE 445 - Computer Organization 6
Example:
Calling CalculateF
ECE 445 - Computer Organization 7
CalculateF: f = g*(a+b+c+d+e*2)
main{ .. f = CalculateF( a, b, c, d, e, g ); .. print( f );}
CalculateF( pa, pb, pc, pd, pe, pg ){ x = Sum2( a, b, c, d, e ); pf = Prod1( x, g ); return( pf );}
1000
2000
1024
2004
Sum2( pa, pb, pc, pd, pe ){ px = pa + pb + pc + pd + pe*2; return( px );}
Prod1( px, pg ){ y = pg * px; return( y );}
3000
4000
3004
4004
ECE 445 - Computer Organization 8
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
CalculateF
f = g*(a+b+c+d+e*2)
Sum2
f = (a+b+c+d+e*2)
Prod1
f = g*(sum)
calls
callscalls
Before calling Sum2:
1. Save return address (of main) on the stack.2. Save argument e on the stack.
Before returning to Main:
1. Restore return address (of main) from the stack.2. Restore stack (i.e. stack pointer).
ECE 445 - Computer Organization 9
Main calls CalculateF
Data local to Main function is pushed on the stack.
Parameters to be passed to the called function are also pushed onto the stack.
Stack Pointer points to the last element pushed onto the stack.
low address
high addressStack
.
.
.
data
$sp
e
g
main
Saved arguments
Local Data
$fp
jal CalculateF
procedure call in Main
ECE 445 - Computer Organization 10
Main calls CalculateF
Main program pushes arguments (e and g) onto the stack.
Main function executes
jal CalculateF
$ra = return address of Main
PC = CalculateF
low address
high addressStack
.
.
.
data
$sp
e
g
main
Saved arguments from calling program
$fp
ECE 445 - Computer Organization 11
Stack Usage by CalculateF
low address
high addressStack
.
.
.
data
e
g
$fp (from Main)
addi $sp, $sp -4sw $fp, 0($sp)add $fp, $sp, $zero
Main
$fp, $sp
1. Frame pointer from Main pushed onto stack.
2. Frame pointer for CalculateF set equal to stack pointer
($fp = $sp).
Stack pointer and Frame pointer now point to the highest address in the procedure frame for CalculateF.
All arguments are now relative to the frame pointer.