![]() |
Previous | Table Of Contents | Next | ![]() |
Passing Arguments To A Program
Here's a paradox: for a program to be useful, it generally needs to be incomplete. That is, it needs operands which are not specified as part of the program. The flexibility offered by these unspecified operands motivate us to execute the program more than once.
Our first example program, IncrementForever.txt, had no unspecified operands, and hence produces the exact same results each time it is run. Once we have seen this program run, there is little reason to ever run it again. We generally want to avoid this outcome when we go to the trouble to write a program.
We can improve that program by leaving some aspect of it unspecified. Let's create an IncrementForever2.txt example program that leaves the starting value unspecified. I have already written the IncrementForever2.txt program and so you only need to load it into the RPN calculator. You accomplish this via the File/Open choice on the menu bar. You can load a program while the calculator is in either Run mode or Program mode (you have to be in Program mode to create or modify a program). The full contents of the IncrementForever2.txt file are shown below:
The first thing you notice about this program is that it includes both English sentences and calculator statements. The sentences are examples of comments. Comments are ignored when the program is executed. All programming languages support the use of comments in order to allow the programmer to explain the operation of his program. For the RPN calculator, anything on a line after a semicolon (;) is a comment and is ignored.
There is no way to create comment lines (or blank lines) via the calculator's keyboard. If you would like to add comments to the programs you construct by clicking (while in Program mode) the keys on the calculator then you will have to save the program into a .TXT file that you later modify with Notepad. Once you have memorized the mnemonics used to represent each of the keystrokes, you can construct your program from scratch in Notepad.
Once you have loaded my IncrementForever2.txt program (which you can do in either Run mode or Program mode) go into Program mode so you can inspect it. You will see that the comment lines do not become numbered statements, as shown below. All of my comments are at the top of the program but you are free to place them anywhere you want.
Now that the shock of the comment lines has worn off, you can observe that IncrementForever2.txt is an even simpler program than IncrementForever.txt . It's simpler because it does less: it skips the initialization that used to exist prior to the infinite loop. It is now the responsibility of the operator to provide this initialization before he runs the program. To run this program you must enter Run mode, initialize the X stack location with a desired starting value (maybe -5000), and then click on GSB 0. The program will start and continue counting up from -5000 until you halt it. "Fascinating!" I can just hear you saying.
Okay, so I need to show you better reasons to use what are called arguments or passed parameters. I will do so in this section via the SecondOrderEq.txt example program but I want to show you a few more tricks before we leave our IncrementForever series of programs.
Load the example program held in the IncrementForever3.txt file. This program accomplishes the exact same thing as IncrementForever2.txt, but looks nothing like it. It too will keep adding 1 to whatever value is placed into the X stack location before the program is run. Since this program lacks a LBL statement you will have to run it via the keystroke sequence GTO.00, R/S. But where is the infinite loop? The IncrementForever3.txt program purposefully allows the program counter (PC) to overflow or wrap-around from its maximum value of 98 back to 0. All microprocessors and computers display this rollover phenomenon but it is usually not used in this manner.
To see the rollover occurring you just need to execute the program while the Program window is open. I have already mentioned that the SST keystroke will open the Program window. Keep clicking on SST until you observe the PC (and hence the highlight) wrap-around back to the beginning.
There is another way to open the Program window. The full list of user specifiable program options can be displayed by selecting View/Options from the menu bar. One of these options allows you to request that the Program window always remain visible. You might like to activate this option. The only downside is that the calculator simulation runs about 33% slower due to the extra work necessary to continually redraw the highlight while the program runs.
With that diversion out of the way, let's get back to studying why and how arguments are passed to programs. Suppose a teacher has asked you to plot the following quadratic (that is, second order) equation:
y = x2 + 2 * x + 3
You figure you could do a good enough job plotting this function if you knew the 21 y values that correspond to the 21 integer x values ranging from -10 to +10. So you have to substitute 21 x values into the equation and solve for 21 y values. Normally, that would take a while. But with a programmable calculator it's a snap.
What we want to leave unspecified in this program is the particular x axis value. Each time we initiate the program we will supply a different x value by placing it into the X stack location. The x axis value for which we want the corresponding y axis value is the argument or parameter that we pass to the program. We then only need to initiate the program 21 times, each time using a different argument, and we will have the numbers needed for our plot.
Before initiating the program we place the x axis argument into the calculator's operand stack. We use the operand stack because it is both accessible to us before we execute the program and is accessible to the program once the program starts running. We will later learn that there is another hardware feature: the data memory, that shares this dual accessibility feature and hence can also be used for passing parameters to programs.
Note that I use lower-case letters for algebra symbols and upper-case letters to identify the stack locations. It is perfectly fine for our program to cause the x axis value to move around among the X, Y, Z, and T stack locations.
So it's time to write the program. Maybe we decide to compute the right-hand side of the equation:
y = x2 + 2 * x + 3
working from left to right. In this case we can begin our program (after the LBL 0 statement that we always place at the top) with an x2 keystroke to compute the second order term. This works but causes a problem. The first order term, 2 * x, also involves the passed parameter x which we no longer have available! The human running the program (usually called the operator) placed the desired x axis value into the X stack location before he initiated the program. The program then immediately squared the value in the X stack location in order to compute the x2 term. This means the X stack location no longer holds the x axis value. Worse, the x axis value is not available anywhere else and hence we are prevented from computing the 2 * x term.
Perhaps the cleanest solution to this problem is to have the program copy the original x axis value into data memory so that it can be recalled whenever it is again needed. But since I have not yet introduced data memory I am going to solve the problem another way. I am going to make a duplicate copy of the passed parameter in the operand stack. The human running the program places a single copy of the x axis value into the X stack location. The first thing the program does is copy the X stack location to the Y stack location so that the operand stack holds 2 copies of the x axis value. When the x2 keystroke consumes one of these copies, the other is still available for the 2 * x term.
The complete program is shown below. You can load it from the SecondOrderEq.txt file.
; file SecondOrderEq.txt ; This RPN calculator program computes the second order eq.: ; y = f(x) = 1x^2 + 2x + 3 ; To run the program enter an x value into the X stack location ; and then type GSB 0 (alternatively, type GTO .00 followed ; by R/S). When the program stops the corresponding y value ; is visible in the X stack location. LBL 0 ; program entry point, at this time X = x ENTER ; make a duplicate copy of x axis value, ; at this time X = x, Y = x x^2 ; X = x^2, Y = x x<>y ; X = x, Y = x^2 +2. ; X = 2, Y = x, Z = x^2 * ; X = 2x, Y = x^2 + ; X = x^2 + 2x +3. ; X = 3, Y = x^2 + 2x + ; X = x^2+2x+3 R/S ; we're done
Note how I use a comment on every program statement to show the contents of the calculator's four-deep operand stack.
Go ahead and run this program for various x axis values. You should get the following results:
Single step the program and watch how the X, Y, Z, and T stack locations take on the values identified in the comments in the program. Remember that I use lower-case letters for algebra symbols and upper-case letters to identify the stack locations.
![]() |
Previous | Table Of Contents | Next | ![]() |