Penguin
Note: You are viewing an old revision of this page. View the current version.

FORTH is a programming language? used in embedded systems. FORTH systems produce very compact code. A whole FORTH interpreter and development system will fit into 8 kilobytes, easily, and leave plenty of room for code. Back when the computer with 8 kilobytes of RAM that you were to write programs for was also the the computer you had to write programs on FORTH was very popular.

(Links and whatnot appear here after I've had some sleep. --GlynWebster)

FORTH machines

FORTH functions are called words.

A FORTH machine has four stacks. When words are called they pop arguments off the parameter stack and push back return values. The return stack holds the return addresses of word calls. Variables and byte-compiled word definitions are pushed onto the user dictionary stack as they are defined. The last is just an input buffer for the FORTH parser.

FORTH programs are written in ReversePolish notation, FORTH words work directly with the parameter stack. When you use a FORTH word you need to keep track of how many values it has pushed and popped. The stack is also used for temporary storage, there are no local variables in FORTH; there are words for flipping around values on the top of the stack to help you with this. This is all part of ChuckMoore's philosophy of brutal simplicity in software engineering, and the most indigestible aspect of FORTH.

Layers of words on the user dictionary stack are called vocabularies. The user dictionary initially contains a tiny kernel of word definitions written in machine code. Some are primitives like arthimetic operators, some deal with passing the input buffer and manipulating byte-codes and the user dictionary. Next on the stack a vocabulary of standard library words written in FORTH byte-codes, then there are usually vocabularies that define a tiny text editor, assembler and disk operating system. I guess a modern FORTH system would have a TCP/IP stack on there somewhere too.

The most important word is ":".

":" will compile the FORTH words that follow it in the input buffer into a new byte-code word definition in the user dictionary. Some words have flags that tell ":" to execute them immediately, when called they create control structures in the byte-code. Words without the flags are compiled into call statements. Words that look like integers are compiled into push statements. The word ";" compiles a return statement and halts compilation. 1?

Usually machine code statements are used as byte codes so the machine can execute it directly. The machine will mostly be executing call statements but this does at least need for a inner loop in the interpreter. This is called threaded interpretation.3?

You use : to add new words to the FORTH system until you've created a high-level FORTH vocabulary that is easy the express you problem in. Then you just keep going until you've defined a single word that, when called, executes your entire program. FORTH is very much a BottomUp language :-)

Real FORTH geeks write their own FORTH kernels from the metal up2?. (ChuckMoore scratches his own FORTH machines onto silicon.) It's not very hard once you've absorbed the FORTH philosophy. There is shockingly little holding FORTH up.


1? It goes something like that anyway. I think I'm giving you the gist.
2? I wrote my own, on paper, when I was 144?. I was working from a FORTH book and a Z80 op-code table; I didn't have computer. I still have pieces of it. God knows if it would have worked.
3? Hoo boy, don't trust me on this.
4? I don't think I can do that anymore.