Stack

From en64 wiki
Jump to: navigation, search

  After execution, a certain adjacent(neighboring) section of memory is set aside for the program; this is the stack! The stack follows a Last In First Out (LIFO) storage device/system/access policy, and is used for temporary storage. Nintendo 64 games are, for the most part, programmed in C. However, unlike usual program flow associated with the C programming language, N64 games have multiple threads running parallel to each other and have a stack for every thread. Note, the amount of threads varies per game as the Nintendo 64 Operating System does not impose a default amount, nor size of said threads. The stack is composed of the stack pointer, limit, and bottom. The smaller (valid) addresses are at the limit (the top), the larger (valid) addresses are located at the bottom (as the name suggests, the bottom of the stack). The stack pointer contains the smallest address and is normally a register the top of the stack, or, "points" to where the top of the stack is, if you will.

  Anything above the stack pointer (SP) is considered garbage, anything below is considered valid! Back to LIFO policy: Push and Pop are the only two operations of the stack.

  • push: You can push one or more registers at a given time by setting the stack pointer to a smaller value. That is usually done by subtracting four times the number of registers to be pushed onto the stack.
  • pop: You can pop one or more registers by copying the data from the stack to the registers, then add a value to the stack pointer (The opposite of push, usually by adding four times the number of registers to be popped on the stack)

Visualization of the above:

Empty Stack:

|     |
|     |
|     |
-------

After Pushing A, you get:

|     |
|     |
|  A  |
-------

After Pushing B, you get:

|     |
|  B  |
|  A  |
-------

After Popping, you get:

|     |
|     |
|  A  |
-------

After Pushing C, you get:

|     |
|  C  |
|  A  |
-------

After Popping, you get:

|     |
|     |
|  A  |
-------

After Popping, you get:

|     |
|     |
|     |
-------

Calling Convention etc