Difference between revisions of "W1251 Parameter Passing"

From Coder Merlin
m (Merlin moved page Project-1251 to W1251 Parameter Passing: Improved navigation)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Prerequisites ==
* [[W1215 Function Signatures]]
== Background ==
== Background ==
* Pass-by-Value
* Pass-by-Reference
=== The Stack ===
=== 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'''.   
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'''.   
Line 17: Line 23:
|[[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]]
|}
|}


Line 26: Line 34:
== 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)]]
* View [https://www.youtube.com/watch?v=d-2Peb3pCBg The Stack (YouTube-Abelardo Pardo)]

Latest revision as of 19:43, 19 June 2019

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

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]