Forth Programming Beginner Guide
Forth Programming Beginner Guide (Using SwiftForth)
How Forth programming works
Forth is a stack based language. You can think of the “stack” as something similar to a “stack” of books.
If book number 5 is the “top” of the stack. Then in the Forth programming language we would use the word “drop” to take this book off the stack.
This would leave us with books 1-4.
Words used in the video:
Word | Description |
---|---|
DUP | Duplicates the top of stack |
SWAP | Swaps first item with 2nd item on the data stack |
ROT | Rotates 3 items on the stack |
DROP | Drops the first item off of the stack |
PAD | Scratch pad of memory |
DUMP | Dump a piece of memory |
-TRAILING | Elimnates trailing spaces in memory |
ZCOUNT | Counts until you get to a zero byte |
NOTES about PAD
PAD is a volatile piece of memory. If you create a new word, lets say
: test ." hello world" ;
Your memory in PAD will be overwritten. It’s recommended to move whatever you put in there to somewhere else.
PAD exists about 512 bytes above the dictionary. So thats why it’s recommended to treat it as a temporary sctrach pad of memory.
Useful SwiftForth words to figure out what a word does
Say you odn’t know what DUP does. Just type into the SwiftForth Terminal:
LOCATE DUP
LOCATE will show you the file directoy its located in and the source code. Also it will show you the usage:
( x -- xx ) means if you do this for example:
1 DUP <press enter key on keyboard>
Then the output on the data stack will be:
1 1 <-TOP
So we know that DUP will duplicate whatever number is currently at the top of the data stack.
Another handy word is WHERE. This word will show all *.f files that have used this word:
WHERE DUP
Doing this will generate a lot of output to the screen and show you how its used in different programs and applications. win
These two words LOCATE and WHERE are what I use the most to figure out what a word does.
EXAMPLE of LOCATE AND WHERE | Description |
---|---|
LOCATE DUP | Displays the word DUP, its location in file directory and source code |
WHERE DUP | Displays ALL *.f files that have used the word DUP |