Example:
(1) Initially: +----------+ (empty stack) |
Example: (CS171 material)
public class Stack { int[] A; int stacktop; // Points to the top of the stack public Stack(int size) { A = new int[size]; // Create the array to hold values in stack stacktop = -1; } public void push( int x ) { A[++stacktop] = x; // Move stack top and put x on the stack } public int pop( ) { return A[stacktop--]; } } |
due to the limitation of the programming language to store data:
|
Recall from this lecture note ( click here ) that when a computer program is execute, the computer memory contains:
In other words:
|
Information stored in the program stack include:
|
We will first study how to save the return address using the stack
Later, we will pass parameters using the stack and create local variables on the stack
(I.e., I will separate the discussion on these 3 items)
But first, we need to study how to implement the program stack.
|
|
Traditionally (for efficiency reasons), the program stack is located at the end of the memory area:
Explanation:
Memory: 0 +----------+ | | <--- this part contains the "code" | A | This part of the memory is "write protected" | | (can not - and should not - be modified) | | +----------+ | | <--- this part contains "static variables" | B | These variables exist at the start of the | | program (a.k.a.: compiled-time variables) +----------+ | | ^ | | | <--- this part grows when the program creates | | | new objects: reserve space for instance | C | | variables in the new object | | v | | v <--- Direction of growth is "downwards" | | v +==========+ | | <---- this part is "free" memory | | (Free means: unreserved !) | | | | +==========+ | | ^ | | ^ <--- Direction of growth is "upwards" | | ^ | D | | | | | <--- This part grows when the program invokes | Program | | a function/method, reserve space for: | stack | | (1) return address | | | (2) parameter variables | | v (1) local variables +----------+ |
|
|
Furthermore, the program stack always grows towards lower memory addresses.
That's because the beginning of the computer memory is used to store program instructions, so the program stack is located at the far end of the memory
Therefore, the grow direction of the program stack is always toward the top of the memory
Example:
|
Memory:
0 +----------+
| | <--- this part contains the "code"
| A | This part of the memory is "write protected"
| | (can not - and should not - be modified)
| |
+----------+
| | <--- this part contains "static variables"
| B | These variables exist at the start of the
| | program (a.k.a.: compiled-time variables)
+----------+
| | ^
| | | <--- this part grows when the program creates
| | | new objects: reserve space for instance
| C | | variables in the new object
| | v
| | v <--- Direction of growth is "downwards"
| | v
+==========+
| | <---- this part is "free" memory
| | (Free means: unreserved !)
| |
| |
SP -->+==========+
| | ^
| | ^ <--- Direction of growth is "upwards"
| | ^
| D | |
| | | <--- This part grows when the program invokes
| Program | | a function/method, reserve space for:
| stack | | (1) return address
| | | (2) parameter variables
| | v (1) local variables
+----------+
|
I will use the name SP (or sp to denote the stack pointer (register) -- it's easier to remember that R13...