W1251 Parameter Passing

From Coder Merlin

Prerequisites[edit]

Background[edit]

  • Pass-by-Value
  • Pass-by-Reference

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:

  1. Write the data to the location immediately above the current stack pointer
  2. 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.
Stack-Empty.png
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.
Stack-57.png
We then PUSH(42).
Stack-57-42.png
And finally, we PUSH(84).
Stack-57-42-84.png


Research[edit]