Lesson 2
The StackType the numbers 7, 4, and 1 on one line with a space between them:
After you press ENTER, the stack display should now look like this: 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 are not limited to one space. You may use two or more spaces, as well as the TAB key.) If you had typed 741 instead, then the single number 741 would have been put on the stack. It is important that you understand how numbers (such as these) are entered and stored on the stack. The best way to demonstrate how a stack works is to use the oftcited analogy of the springloaded pile of dishes you encounter in a cafeteria line. If you place one plate on the spring, it will obviously be the first one that will come off the top. But if you place a second plate on top of the first plate, 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 off the top 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. (LIFO, in computer jargon, meaning "Last In, First Out".) You can see that this principle applies to the Mops stack, since the number 7, which you entered first, is at the bottom of the stack, while the number 1, which is the most recent entry, is at the top. 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" (or prints) it at position of the flashing cursor in the lower part of the screen. That word is a simple
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 (4 and 7) 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 in this example), any number "typed" will appear at the cursor position, just as if you had typed it at the keyboard yourself. To bring the Mops prompt to the left margin, where it will be less confusing, simply press RETURN (not ENTER!) once. (Carriage returns are not automatically printed, unless you, as the programmer, tell Mops to print one. We'll show how to do that very shortly.) Now type two periods, with a space in between, and press ENTER.
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 (the flashing cursor) reappeared. As you can see, multiple dot commands leave a trail of numbers coming off the top of the stack, printing them from left to right across the screen. And notice, too, that nothing remains in the stack when the last dot command was executed. Mops also has a word, To see how it works, place the same three numbers that we used before on the stack (don't forget the spaces):
And when you type
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 Experiment with the operation of the stack by putting numbers on the stack, viewing them with the For example:
If you accidentally issue one more
The stack is also called the parameter stack (sometimes referred to as the data stack), because a good many operations in Mops require that one or more values be present on the stack before an 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, in other words, is a kind of container for values that many operations rely on. This concept will become clearer as we now discuss how Mops performs arithmetic. Stack ArithmeticIf you've ever used a HewlettPackard RPN 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 stacktype computer when you do this. For those who have never used one of HP's RPN calculators, the steps to add numbers like 2 and 7 go like this:
Mops works in very much the same way. The stepbystep 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:
(We don't need to keep reminding you to type ENTER after each line of input, do we?) 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 of the top two numbers on the stack for you, storing the sum on the stack. (Hence the stack display 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 (in this case, the result of your addition), you must issue the Mops lets you perform all these manipulations in a simpler form as a single line of instructions, as long as there is at least one space between each element. Here's how it looks:
The line of instructions contains the same commands as the stepbystep method, but is much easier to type in. The only thing you miss along the way is a stepbystep readout of the stack display. But after all, it's the answer that should be important, not the momentary contents of the stack.
