Difference between revisions of "W1251 Parameter Passing"

From Coder Merlin
(Created page with "== Research == * Read Section 5.2 (Computer Science I Textbook by Bourke)")
 
Line 1: Line 1:
== Background ==
=== The Stack ===
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 ===
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
{|
| style="vertical-align: top;" | Let's consider an example where we '''push''' three elements on to the stack:  57, 42, and 84.  We begin with an empty stack.
|[[File:Stack-Empty.png|thumb|right]]
|-
| style="vertical-align: top;" | 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.
|[[File:Stack-57.png|thumb|right]]
|-
|We then PUSH(42).
|-
|And finally, we PUSH(84).
|}
== Research ==
== Research ==
* Read [[Media:ComputerScienceOne.pdf| Section 5.2 (Computer Science I Textbook by Bourke)]]
* Read [[Media:ComputerScienceOne.pdf| Section 5.2 (Computer Science I Textbook by Bourke)]]

Revision as of 21:41, 10 April 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

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:

  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).
And finally, we PUSH(84).


Research[edit]