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--];
}
}
|
Therefore:
|
Information stored in the system stack include:
|
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
| System | | a function/method, reserve space for:
| stack | | (1) return address
| | | (2) parameter variables
| | v (1) local variables
+----------+
1. Parts A and B exist as soon as the program starts execution
and will exist throughout the program execution.
2. Part C will grow when program creates new objects
(with the new operator in Java or malloc() in C)
Part C is called the "system heap"
3. Part D will grow when program invokes a function/method
Part D is called the "system stack"
|
|
So the system stack that is located at the end of the memory area:
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
| System | | a function/method, reserve space for:
| stack | | (1) return address
| | | (2) parameter variables
| | v (1) local variables
+----------+
|
do not need to be defined (as an array as in Java)
(The memory is there and it is reserved for us to make a stack)
|
|
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 !)
| |
| |
A7 -->+==========+
| | ^
| | ^ <--- Direction of growth is "upwards"
| | ^
| D | |
| | | <--- This part grows when the program invokes
| System | | a function/method, reserve space for:
| stack | | (1) return address
| | | (2) parameter variables
| | v (1) local variables
+----------+
|
+----------+
| |
+----------+
| | (available, i.e., unused)
+----------+
A7 -->| Z |
+----------+ ^
| Y | ^ <--- Direction of growth is "upwards"
+----------+ ^
| | |
+----------+ |
| | |
| System | |
| stack | |
| | |
| | v
+----------+
|
+----------+
| |
+----------+
A7 -->| X | (Memory space no longer available)
+----------+
| Z |
+----------+ ^
| Y | ^ <--- Direction of growth is "upwards"
+----------+ ^
| | |
+----------+ |
| | |
| System | |
| stack | |
| | |
| | v
+----------+
|
(The memory space is no longer available because all memory spaces "at and below" the stack pointer A7 are used !!!!)
suba.l #4, A7 // Move system stacktop up 4 bytes
move.l X, (A7) // Store X in the top of the stack
|
adda.l #4, A7 // Move system stacktop down 4 bytes
|
|
|
|