|
|
|
|
|
|
|
A stack of dishes is the common analogy. It is fine as far as it goes, but it is wrong in a fundamental way. A more accurate mental picture is of a series of cubbyholes aligned top to bottom. The top of the stack is whatever cubby the stack pointer (which is another register) happens to be pointing to. |
|
|
|
|
|
|
|
|
Each of the cubbies has a sequential address, and one of those addresses is kept in the stack pointer register. Everything below that magic address, known as the top of the stack, is considered to be on the stack. Everything above the top of the stack is considered to be off the stack and invalid. Figure 5.5 illustrates this idea. |
|
|
|
|
|
|
|
|
FIGURE 5.5
The stack pointer. |
|
|
|
|
|
|
|
|
When data is put on the stack, it is placed into a cubby above the stack pointer, and then the stack pointer is moved to the new data. When data is popped off the stack, the address of the stack pointer is changed by moving it down the stack. Figure 5.6 makes this rule clear. |
|
|
|
|
|