Forth Lesson 1
In the previous lesson we learned that:
- The top-level parser only looks for whitespace, so Forth words must be separated by one or more spaces
- Execution proceeds from left to right. Get a word, execute it.
Today we will start trying some commands.
Getting a Forth Interpreter
If you have an OLPC XO machine that is "unlocked" (security is disabled or you have a developer key), press the power button, then hit the Esc key (upper left corner) when you hear the startup sound. That will display an ok prompt. Otherwise, get http://firmworks.com/linux/forth.zip , extract it on an x86 Linux system, and type "./forth". It is better to use an OLPC system if possible, because you will be able to access hardware directly, which you cannot do easily under an operating system. That won't matter for the first few lessons, though.
Forth has an explicitly visible stack that is used to pass numbers between words (commands). Using Forth effectively requires you to think in terms of the stack. That can be hard at first, but as with anything, it becomes much easier with practice.
Forth also has a secondary stack - the "return stack" - that is used for return addresses and a few other things. The Forth system automatically manages the return stack so you don't have to think about it for the most part (but you can get at it if you need to; Forth doesn't block you from doing anything).
Displaying the Stack
will cause Forth to show you what is on the stack before every "ok" prompt. "noshowstack" turns it off. With showstack mode on, if the stack contained the numbers 5678 and 112233, you would see:
5678 112233 ok █
as the prompt. The number to the right is the top of the stack.
I recommend running with showstack mode on while you are learning. (If showstack is off, and you want to display the stack contents once, type ".s".)
Putting Numbers on the Stack
If you type a number, it gets pushed onto the stack:
ok 12345 998877 12345 998877 ok █
What we see here is that the two numbers did get pushed onto the stack, and since showstack mode was on, the stack contents were displayed to the left of the ok prompt.
12345 998877 ok + 9aabbc ok █
The "+" word pops two numbers from the stack, adds them, and pushes the sum back on the stack. Note that:
- + doesn't display the result, it just pushes the result back on the stack. In this case, however, we are assuming that "showstack" mode is on, so the interpreter displayed the stack for us automatically before the next prompt.
- The numbers are in hexadecimal. Forth can operate in any base up to 36. The default base in Open Firmware is hex, whereas when Forth is running under an OS, the default is decimal. You can change the base at any time by typing "hex" or "decimal". All of the examples in this tutorial series will use hex as the default base.
- The current number base applies to both number input and number output.
9aabbc ok noshowstack ok █
Now the stack display is gone, but the number is still there.
ok .s 9aabbc ok .s 9aabbc ok █
".s" displays the entire stack, non-destructively.
ok . 9aabbc ok .s Empty ok showstack ok █
"." pops the top of the stack and displays that number. Afterwards, the number is no longer there. There are variations of "." (with different spellings) that display in specific bases, with specific formatting, signed/unsigned, etc. Those will be covered later.
How the Interpreter Works
The Forth interpreter is very simple. It just does this over and over:
- Read a line of input
- While there is more data in the line:
- Parse a whitespace-delimited word
- Lookup the word in a list of defined words. If found, execute the code for that word.
- Otherwise, try to interpret the word as a number in the current number base. If so, push it on the stack.
- Otherwise display an error message
Thus endeth the lesson.