Difference between revisions of "W1251 Parameter Passing"
Line 17: | Line 17: | ||
|[[File:Stack-57.png|thumb|right]] | |[[File:Stack-57.png|thumb|right]] | ||
|- | |- | ||
|We then PUSH(42). | | style="vertical-align: top;" | We then PUSH(42). | ||
|[[File:Stack-57-42.png|thumb|right]] | |||
|- | |- | ||
|And finally, we PUSH(84). | | style="vertical-align: top;" | And finally, we PUSH(84). | ||
|[[File:Stack-57-42-84.png|thumb|right]] | |||
|} | |} | ||
Revision as of 21:49, 10 April 2019
Background[edit]
The Stack[edit]
An operating system provides an executing program with a structure called a stack. (Multithreaded programs may be provided with one stack per thread.) The stack is used by the application to ensure that when the invocation of a function completes, controls returns to the proper location, i.e., the next statement after the function invocation. A stack is a data structure that maintains its elements in a certain order, namely Last-In-First-Out. This means that the most recent element placed on the stack is the first one to be removed. Placing an element on the stack is performed by a PUSH operation, while removing an element from the stack (always the top-most element), is performed by a POP operation. The top of the stack is tracked in a special processor register called the stack pointer.
PUSH Operation[edit]
The PUSH operation performs the following steps:
- Write the data to the location immediately above the current stack pointer
- Decrement the stack pointer so that it points at the next available location
Let's consider an example where we push three elements on to the stack: 57, 42, and 84. We begin with an empty stack. | |
We then PUSH(57). Remember that we write the data to the location immediately above the current stack pointer and then decrement the stack pointer. | |
We then PUSH(42). | |
And finally, we PUSH(84). |