| The Stack | Lesson 2 | Contents |
The Parameter Stack
Forthish languages do not pass variables (parameters) like other languages. Forth uses a Stack which values are dumped on and taken off downstream in the process. The upper half on the PowerMops window is where the Stack displays its values. Going back to our example from the previous page:
36 emit CR
To see how this works, type ONLY the number 36 and then «enter». The number 36 appears in the Stack, at the top of the window. Now type emit «enter» . The contents of the Stack are taken off and used as a parameter for the command. You didn't even know this was happening, since the Stack in a well running program is essentially invisible.
Going into detail about the Stack, type the numbers 7, 4 and 1 on one line with a space between them, thus:
7 4 1 «enter»
After you press «enter», the stack display should now appear thus:
Stack: depth 3 1 4 7
The space (or delimiter) you typed between the three digits told Mops that you intended those three digits to be three different numbers. (You can equally well use two or more spaces, or the tab key). If you had typed 741 instead, then the single number 741 would have been put on the stack. Understanding the way these numbers are entered and stored on the stack is of utmost importance at this stage of learning Mops.
The best way to demonstrate how a stack works is to summon the often cited analogy of the springloaded pile of dishes you encounter in a cafeteria line. If you place one plate on the spring, it is obviously the first one that will come off the top. But if you place a second plate on top of the first, the weight of the second plate pushes the first one down one step, and the second plate is the one that will be picked up by the next customer in line.
| In other words, the last one put on the stack is the first one to be taken off the stack. This kind of action in computer lingo is called LIFO, an acronym for "Last In First Out" . |
|
Viewing Stack Variables
How can you remove numbers from the stack? One way is to use the Mops word that does the reverse of what you did when you entered a number—it takes a number off the stack, and "types" it in the lower part of the screen, at the flashing cursor position. That word is a simple period (.), called "dot".
Type this now:
. «enter»
1
What happened here was that the dot (type to screen) command pulled the 1 off the top of the stack and "typed" it. The stack display will show that two numbers are still on the stack.
In other words, whenever you perform a dot operation on a number in the stack, the number is removed from the stack and "typed"—that is, displayed in the current window. If the Mops window is the current window, as it is here, any number "typed" will appear at the cursor position, just as if you had typed it at the keyboard.
To bring the Mops prompt to the left margin, where it will be less confusing, simply press «return» once. Now type two periods, with a space in between, and press «enter»:
. . «enter»
4 7
Mops has now printed the two remaining numbers in the order in which they came off the stack. Remember that the 7 was at the bottom of the stack; it was therefore the last number off the stack, and was displayed on the screen as the final item before the Mops prompt reappeared. Multiple dot commands, as you see, leave a trail of numbers off the top of the stack from left to right across the screen. And notice, too, that nothing remains in the stack when the last dot command has been executed.
To do the same on the floating point stack use f. «enter»
Mops also has a word, .s (dot s), that displays a list of all numbers on the stack without removing them. This can be useful during the running of a program, since you may not want to stop the program to see the stack displayed at the top of the window. Also, there may be more items on the stack than can fit in the display. The stack can hold far more items than we have room to display at the top of the window, but if you use .s, all the stack items will be typed in the lower part of the window.
To see how it works, place the same three numbers on the stack (don't forget the spaces):
7 4 1 «enter»
And when you type .s, your screen will look similar to this:
.s «enter»
Stack: Depth 3
1 $ 1
4 $ 4
7 $ 7
The numbers to the left of the dollar sign are the decimal values, while the numbers to the right are the hexadecimal values. The dollar sign in this list is a hex indicator. In this case, it happens that the stack numbers in both bases are the same. Note also that the Mops prompt at the end shows that the three numbers are still on the stack.
The regular dot command displays and removes them while .s simply takes a snapshot of them.
Experiment with the operation of the stack by putting numbers on the stack, viewing them with the .s operation, and taking them off by printing them to the screen, either one at a time or in a series. As an added shortcut, you can use the CR command, which is short for "carriage return", after a dot command. If you type a "CR" as a command after one or more dot commands (remember to type a space between the last period and the CR), the Mops prompt returns to the left margin of the next line.
For example:
1 10 100 «enter» . . . CR «enter» 100 10 1
If you accidentally issue one more dot command than you have entries on the stack, Mops will send you a message (along with the alert beep) that the stack is empty. Try it, no harm will occur.
The stack is also called the parameter stack, because a good many operations in Mops require that one or more values be present on the stack before the operation can be performed These values, in computer jargon, are called parameters, and they are said to be passed, or handed to, an operation. Actually, the operation looks to the stack for the number(s) it needs, and pulls them off.
You saw a glimpse in the last section of how parameters work, when the parameter stack held values that were to be printed to the screen. The parameter stack, inother words, is a kind of holding box for values that many operations rely on. This concept will become clearer as we now discuss how Mops performs arithmetic.
Stack Arithmetic
If you've ever used a Hewlett-Packard calculator, you are already familiar with keying in two values and then pressing the key that bears the symbol of the desired operation, such as + for addition or * for multiplication. You're actually utilizing a stack-type computer when you do this.
For those who have never touched an HP machine, the steps to add 2 and 7 go like this.
First press the 2 key.
The 2 is placed on the top of the stack.
Then press the Enter key.
This pushes the 2 one cell deeper into the HP calculator's stack, a place in the calculator's memory where values are temporarily held until they are needed for
an operation.
Then press the 7 key, which places the 7 on the top of the stack.
Finally, press the + key, which reads each value from the stack (first the 7, then the 2) and adds them.
The answer, 9, appears both in the display and on the top of the stack, ready for further operations, if desired.
Mops works very much the same way.
The step-by-step approach to add two numbers would be to put each number on the stack one at a time, and then press the + key as follows. (Remember to type «enter» at the end of each line so that Mops will interpret these lines as commands. We'll assume this from now on, without saying so).
7
2
+
. cr
Let's follow what happened here.
You should already understand how the stack counter increments each time you type a number and press «enter». In the third line, you type the operation, the + sign for addition. When you press «enter», the computer calculates the sum for you. Mops stores the sum on the stack, hence the stack counter shows one value on the stack.
Note, too, that the original numbers were taken off the stack by the addition operation. Or rather, in a split instant, there was nothing on the stack as the two numbers were being added inside the computer. To display the contents of the stack, and the result of your addition, you must issue the dot command. Sure enough, the answer (9) was on the stack.
Mops lets you perform all these manipulations in a simpler form as a single line of instructions, with at least one space between each element.
Here's how it looks:
7 2 + . cr
9
The line of instructions contains the same commands as the step-by-step method, but is much easier to type in. The only thing you miss along the way is a step-by-step readout of the stack counter. But after all, it's the answer that should be important, not the momentary contents of the stack.
| Previous Chapter | Contents | Next Chapter |
|---|---|---|
| ⇧ | ||
| This page online: http://PowerMops.com/MopsManual/Lessons/Chapter2.html | ||