Stack and Arithmetic Operations Lesson 5 Contents

This is a good time to learn several other basic Mops operations. They're rather simple, so you may as well get them out of the way now. We won't be saying too much about them here, but you might want to experiment with them for a bit to get a feeling of how they work.

One group of operations compares the values of the two topmost items in the parameter stack. The result of the comparison is placed on the stack. Here they are:

MIN ( n1 n2 -- n-min ) Leaves the smaller of n1 and n2 on the stack
MAX ( n1 n2 -- n-max ) Leaves the larger of n1 and n2 on the stack

The next group manipulates the signs of integers, positive or negative. One returns the absolute (positive) value of the topmost number in the stack. The other changes the sign of the topmost number in the stack: if the original is positive, the operation changes it to negative, and vice versa. Here are these two operations:

ABS ( n -- |n| ) Leaves the absolute value of n on the stack
NEGATE ( n -- n ) Changes the sign of the topmost number on the stack.

Next is a list of simple arithmetic shortcuts. Their meanings should be self-evident.

1+ ( n -- n+1 ) Adds 1 to the number on the stack
1- ( n -- n-1 ) Subtracts 1 from the number on the stack
2+ ( n -- n+2 ) Adds 2 to the number on the stack
2- ( n -- n-2 ) Subtracts 2 from the number on the stack
2* ( n -- 2n ) Multiplies the number on the stack by 2
2/ ( n -- n/2 ) Divides the number on the stack by 2
3+ ( n -- n+2 ) Adds 3 to the number on the stack
3- ( n -- n-2 ) Subtracts 3 from the number on the stack
3* ( n -- 2n ) Multiplies the number on the stack by 3
3/ ( n -- n/2 ) Divides the number on the stack by 3
4+ ( n -- n+2 ) Adds 4 to the number on the stack
4- ( n -- n-2 ) Subtracts 4 from the number on the stack
4* ( n -- 2n ) Multiplies the number on the stack by 4
4/ ( n -- n/2 ) Divides the number on the stack by 4

The application of these shortcuts will become more apparent the more you program in Mops. The addition and subtraction shortcuts, for example, come in handy when you need to increment or decrement a counter of some kind.

Displaying Text
Many times in a program, you want to display text on the screen. It may be to display a heading on a screen or to "humanize" a purely numeric answer by describing what the number is. In the latter case, you are actually combining the display of a pre-planned text message with a numeric answer, which can change from execution to execution.

The quotation marks fall into a broad category of symbols in computer languages called delimiters, because they delimit or set the limits for something—in this case a text message. The text within the delimiters is called a text string, or just string. Note that for normal Mops words, spaces, tabs or carriage returns are delimiters. However for message strings we usually want to be able to include spaces as part of the string, so we use " as a delimiter instead. However, since ." is a Mops word, it must itself be delimited by a space. This space is not included as part of the string, but the first character after the space is the first character of the string.

Text strings can be made part of Mops word definitions very easily. In the following example, You'll define the word "hi" so that it prints a greeting message from the computer.

: hi ." hello, this is Mops operating on the Macintosh." cr ;

Now, when you type "hi" at a Mops prompt, the message between the quotes appears on the screen. Note again that the space immediately after the ." is not part of the message, but just serves to delimit ." as a word. If the space wasn't there, Mops would try to interpret ."hello" as a word, which certainly isn't what we want.

One of the nice things about Mops is that you can use previously defined words inside the definitions of new words.
Therefore, you could take the "hi" Mops word and incorporate it inside yet another Mops definition.
For example:

: greeting hi ." How are you?" cr ;

produces not only the message of "hi", but an additional text string whenever you type "greeting" at a Mops prompt.
Try it, you will like it.

Now combine your knowledge of arithmetic operations and text strings to humanize your earlier arithmetic word, add. In this case, you're going to redefine add. To do this, simply type in the new definition. Mops may alert you that you have redefined the word when you press «enter», depending on how Mops has been set up (we will describe this later).
Here's the new definition:


: add  ." The sum is: " + . cr ;
ADD redefined  0->_

To use the new word, issue the command at the Mops prompt like this:


10 20 add 
The sum is:  30

Previous Chapter Contents Next Chapter
This page online:  http://PowerMops.com/MopsManual/Lessons/Chapter5.html