Quick Forth Guide

Good organisation and effective communication require us to:

This is how Forth works. It consists of a dictionary of words grouped into vocabularies. Each word is an operation and words are defined in terms of those that went before. This dictionary defines Forth itself and is added to by your application to grow into a new Forth that solves your problem.

Let us look at an example of how this works for a simple FAX machine.

The : in Forth indicates the start of a new definition. The space delimited text following will be the name of the next entry in the dictionary. These names are called "words" in Forth. The other words already exist in the dictionary and are the functions performed by this new definition. The ; terminates the new dictionary addition. This process is called compilation and the finding of the words in the dictionary interpretation.

The words ring, read, transmit and hang-up are defined in terms of other words, for example the word read:

Here we see read defined in terms of other words, this time with a looping structure that terminates at the page end. Notice that the definitions are short and the word names relevant. Also we have shown the definitions in a top-down style, which is how you might consider the problem, but they would be implemented in a bottom-up fashion. This means that the simplest task is defined and tested first, building up to the final function send.

The words in read may well consist of what are called Forth primitives as are the BEGIN...UNTIL structure in read itself. These primitives are the fundamental functions of Forth and are the starting point from where the Forth grows or extends. Hence the Forth language is termed "extensible".

To recap, Forth is an extensible language that consists of a dictionary of words each defined in terms of other words.

Interactive

One of the many advantages of Forth is it's ability to execute a word from the dictionary by name. Once a colon definition has been entered it may be run, or executed, just be typing the word name and pressing the RETURN key.

Forth is thus called an interactive language because it carries out your commands as soon as you enter them at the keyboard.

When the RETURN key is pressed Forth tries to run all the words on the command line and at the end prints:

This is true providing no problem was found. For instance if a word is not in the dictionary Forth will say:

Or one of the words may not have enough parameters and you may see:

The Stack

Forth is a stack based language. This means that all the parameters used during operations or calculations are held on a stack. The stack is a Last-in-First-Out, LIFO, structure in the re-writable area of computer memory. If we put a value on the stack we say we are 'pushing' it onto the stack. If we retrieve it we say we are 'popping' it off the stack.

This stack operation is very useful but leads to some differences. Suppose we wish to add two numbers:

PRINT 1 + 2 3

This is how we might look at the problem in Basic. In Forth we would write:

1 2 + . 3 ok

Here the values are put first and the operator last. This is called 'post-fix' or Reverse Polish Notation (RPN), we normally use 'pre-fix'. The . word outputs the top stack item converted to an ASCII number string to the PC screen.

The advantage to the computer is that it can only handle postfix order as it does everything sequentially. It must have the values before it can do the addition. In Forth the entry of the two numbers pushes them onto the stack. The 2 was last so it is on the top of the stack with the 1 underneath it. When Forth executes the + code it only knows that it must add the top two stack values together and leave the answer on top of the stack. The + code does not need a location for the two numbers it just uses whatever is on the stack. If we performed:

1 2 + 5 - . -2 ok

The answer of -2 would be left on top of the stack. No intermediate storage of the 1+2 is required. This requires less memory and is dynamic with no need to 'garbage-collect' as there is no 'garbage'.

Stack Nomenclature and Documentation

All Forth functions operate with the stack for parameter passing. Any number of stack values may be input and output from a definition so we show the stack use of a word when we define it. To do this (S -- ) are used as stack comments.

(S n1 n2 -- n3 )

Would mean that two values were required on entry and one value is left on exit from the word. Operands of differing sizes and types are shown below.

n1 n2 16 bit signed numbers

d1 d2 32 bit signed numbers

u1 u2 16 bit unsigned numbers

a1 a2 16 bit addresses

fa1 fa2 8 bit file addresses

b1 b2 8 bit bytes

c1 c2 ASCII characters

f 8 bit flag. 0 = false, 255 = true

 

Contents