by Donald Forbes
Few things in life are harder than hitting a round ball with a curved bat. Baseball coaches who do their homework can make it seem easy. Some young folks get the knack. Some few grow up to be experts. Then they make more money in a year than the President of the United States. Dave Winfield of the New York Yankees earned more than $2 million last year.
If you want to demonstrate FORTH, remember the coach: you have to make it look easy. One way is to present FORTH as a language, even though it's more than that. It is also an operating system and can be a complete software interface between the hacker and his hardware. But FORTH can be thought of as a language for program development.
If FORTH is a language, it must has words. And every language has to have words which do things (called "verbs") and words which name things (called "nouns"). What are the verbs in FORTH? What are the nouns? And which should we tackle first?
If we are to demo FORTH to our friends, we should present the nouns first, because that makes it easy for them to start writing simple programs. Before we do that, though, let's show them what the verbs look like. How? All it takes is one word: VLIST.
On your screen you'll get a listing of all the words in the FORTH dictionary. You can pause the listing with CTRL 1. These are all the words you will need to know to master FORTH. Most of them are written in FORTH, except for a few at the bottom of the list, which are written in assembly language. The majority are verbs, because in FORTH a "word" is used to refer to a command, instruction or executable procedure.
When you talk about the dictionary in FORTH, you're usually referring to the FORTH vocabulary. But there are other vocabularies as well -particularly the editor and assembler vocabularies.
To see them, you first have to load the editor or assembler code. Then type EDITOR DEFINITIONS followed by VLIST Typing FORTH DEFINITIONS will restore the original VLIST.
If you scan the output from VLIST, you will immediately recognize a few of the verbs of FORTH. There is the plus sign (+) for addition, the minus sign (-) for subtraction, the asterisk (*) for multiplication and the slash (/) for division. (What the Mock Turtle in Alice in Wonderland called "the different branches of Arithmetic - Amb it ion, Distraction, Uglification, and Derision" And, of course, VLIST)
Where are the nouns? The answer is: you create them yourself What are they? They can be numbers, letters or strings of characters. How do you create them? Where do you put them? That brings us to the topics of your demo for today.
People have been standing in line for aeons, so mathematicians developed what they call the queueing theory, which they describe as the area of stochastic (random) processes, emphasizing those processes modeled on the situation of individuals lining up for service.
The theory studies two kinds of queues: a queue like a line at the box office, called a "first-in, first out" or "FIFO" queue; and a "last-in, first-out" or "LIFO" queue, usually called a "stack . " Some common stacks we all recognize are: a card deck with the top card turned over, the row of shopping carts in the grocery store, Tom Hudson's descriptive stack of pancakes (Boot Camp, issue 20), or the balls in a can of tennis balls. The last ball in the can is at the top of-stack (or "TOS"), and there is no way in the world (without breaking the can) to get at the bottom ball without removing each of the others in turn.
A stack is easier to manage than a queue. You only need a pointer to keep track of the top of the stack and a counter to record the depth.
What does a stack have to do with FORTH? Actually, everything. FORTH is built around the dictionary (and editor) for verbs and the stack for nouns. (The other key elements of FORTH are the return stack, the text and inner interpreters, the assembler and disk memory-all good topics for future demos.)
Whenever you create a noun in FORTH, it goes on the top of the stack and stays there until you retrieve it. Mastery of the stack is the key to mastery of FORTH. Some words in FORTH do not affect the stack, but most of them either take or remove data from the stack. A few of them do both.
The stack (also known as the "data stack" or "parameter stack") is the main reason for FORTH's power and flexibility. As you create new procedures, you can pass data backward and forward by means of the stack. There are no parentheses in FORTH (except to enclose your comments), and the stack is what makes it possible to get rid of the parentheses.
There is a list processing language called LISP that offers some competition to FORTH in the field of Al (artificial intelligence). It has no stack and, consequently, uses so many parentheses that some jokers will tell you that LISP stands for Lots of Irritating Silly Parentheses.
You have to get used to the FORTH stack. Not because it's difficult to use, but because of the way you were brainwashed in elementary school. You were told that 2 plus 3 makes 5. What the teacher should have said is: "Take 2, then take 3, and add them together."
If you type:
2 3 + .
FORTH will put the 2 on the stack, then the 3. The "word" + takes the two top items from your stack, adds them together and puts the answer on the top of the stack. The final . (dot) takes the number off the top of the stack and prints it.
If you want to calculate (3+4)*(5 +6), you will say to yourself: "Take 3 and 4 and add them, then take 5 and 6 and add them, then multiply the answers." FORTH does it exactly the same way:
3 4 + 5 6 +
and comes back with 77 OK.
Here is what the stack looks like if you calculate (4+7)*(9-3) and enter it in FORTH notation:
Fortunately, the stack can be displayed with ease at any time by the S word, which is found in QS Forth, Team Atari Forth and vaIFORTH. In Team Atari Forth, the command STACKON will display the stack at the top of the screen in color. In valFORTH, the command ON STACK displays the stack after each OK.
With the stack and some simple arithmetic commands, you can use your FORTH computer as a handheld calculator.
You can even demonstrate exponentiation with this code:
so that you get:
if you want to multiply two by itself fourteen times. You can also use your FORTH computer as a programmable calculator. For example, to evaluate the polynomial 5X2 - 7 x - 10, you could use this code:
which would give you:
Or, if you wanted the value of this expression for various values of y (2y4+3y3+4y2+5y+6), then this code will work:
so that, for a value of two, we get:
Four factorial is four times three times two times one. Here is a factorial program:
which will give us:
and here is another program to find the square root of an unsigned number:
which we can test with:
To work with the stack, as you have seen, we must bear in mind the tennis balls in a can. We need verbs in FORTH that will let us add, change or delete data from the stack.
Deleting is easy. DROP removes the top item from the stack this way:
A quick way to wipe the stack clean is to type an imaginary word (like XX), which gives an error message and, at the same time, clears the stack.
To add items to the stack, we can use DUP, -DUP, OVER and PICK-like this:
To move items on the stack, we use SWAP, ROT and ROLL.
ROLL, like PICK, is not in fig-FORTH but can be defined as follows:
Here are some examples that only move the items on the stack:
You must be aware during your demo that the stack will only handle 16 bits at a time. With this number of bits, we can only represent integer numbers from -32768 to +32767. One of the bits is used for the plus or minus sign. The above examples will give you wrong answers if you work with numbers outside this range. If we do not need the sign bit, then we can handle unsigned values from 0 to 65,535.
If you need larger numbers than this, you can work with the two top items on the stack, which will give you 32 bits. These can represent signed numbers between - 2,147,483,648 and + 2,147,483,647 (or from 0 to 4,294,967,295, if unsigned). To do this, however, you will need a different set of FORTH words. There are floating point packages which allow you to handle numbers with the same ease as in BASIC. They are not part of the public domain fig-FORTH, but exist in vendor offerings such as valFORTH.
One point that is often overlooked in FORTH is brought out by C. Kevin McCabe in his book FORTH Fundamentals, Vol. 1 ($16 from Mountain View Press, PO. Box 4656, Mountain View, CA). He gives the most complete explanation to date of the basics of figFORTH. On page 39, he says:
How does FORTH distinguish between signed and unsigned characters on the stack or in memory? Quite simply, it doesn't The characteristics of each value exist only in the programmer's mind, in selecting operations where the value is an appropriate operand. FORTH does not keep track of the kind of values represented, nor does it prevent or check for range errors. The burden is on you, the programmer, to insure that the contents of the stack and memory are those intended and that they represent values of the correct type.
In other words, the 16 binary digits at the top of the stack mean whatever you say you want them to.
This raises another point. You can display numbers on the stack in binary, octal, decimal or hexadecimal. In figFORTH, in fact, you can choose any base between 2 and 36. For example:
However, as McCabe points out: "It must be emphasized that the numeric base is only used during the conversion of values sent to and from the terminal. Internally, all values will still be in binary..."
The number on the stack can also represent an address. If you type:
then FORTH will interpret the 0 as an address and start to type out the character equivalent of the first 1000 bytes in memory.
The number on the stack can also be used to represent a logical flag, as with this code
so that can make a test for false if zero and true otherwise:
We can also use the stack to store the ATASCII code of keyboard characters, with this code:
which will wait for a key press and then print the code, as well as the corresponding character:
The stack can also be used to handle bytes-equivalent to 8 of the 16 bits on the stack. The words C! and Ca store and fetch 1 byte at a time to and from memory; they merely pad the other 8 bits with zeros. The assembler command C moves bytes from the stack to the dictionary.
At this point, your audience should begin to appreciate the two principal features of FORTH. First, you write programs by expanding the dictionary (hence, the language) with your own verbs. Second, FORTH can use a stack (instead of named variables, as other languages do) to pass your nouns-or data-from one word (or subprogram) to another. If FORTH code appears less readable than the code in other languages, this is a small price to pay for ease in developing fast and efficient programs.
The power and flexibility of the stack brings with it the responsibility- that falls on you-to insure that everything is done correctly. Much of the effort of learning FORTH consists of memorizing the stack input and output parameters of the respective FORTH words.
In the code below, you will notice that PLOT and DRAWTO and POS. all expect two numbers on the stack, while SOUND looks for four.
What we have here is a small "wake up" demo to start your presentation. Remember that any character (except BACKSPACE, BLANK and the RETURN key) can be used as a FORTH word. Some tricks were used to save space, such as the use of the caret (A), vertical stroke (1) and underscore (_), as well as lower case letters and, finally, the redefinition of PLOT as PL and DRAWTO as DR.
Note, also, the rising sound on each letter-it will help to identify the double consonants. The demo will work on both Team Atari Forth and valFORTH versions of fig-FORTH, so you can modify it to suit your tastes. QS Forth requires a minor change to screen 1. The number in the COLOR statement must be added to each PLOT or DRAWTO so remove 3 COL OR in Line 4 and change Line 6 to:: PL 3 PLOT;: DR 3 DRAWTO;
There are two other "last-in, first-out" stacks in FORTH. They are the return stack and the dictionary itself But, if you want to make FORTH took easy, you'll have to save those demonstrations for another day.