Home > Previous Page >  Gil Filbey - Forth Series part 1
Archive Search  

Languages

FORTH

Gil Filbey


  Forth is a high-level programming language designed for small com puters. It has been in existence for some years and has a following in America, but is only just beginning to appear in Britain. The main advantages of FORTH are:


  • It requires only a small amount of
         memory
  • It is fast
  • It lends itself naturally to structured
         programming
  • It is virtually machine independent

  I propose to illustrate the use of FORTH by showing a program and what it does; then discussing how it does it. The implementation I am using is APPLEFORTH V1.5 on cassette. I also have APPLEFORTH V1.2 by Programma International, though I have not made much use of this version yet. Both these versions are also available on disc.


A FORTH program (explanation later)  
: STAR 42 ECHO ; OK
: STARS 0 DO STAR LOOP ; OK
: PLACES 0 DO DOWN LOOP ; OK
: SLOGAN."FORTH RULES" ; OK
: GRAFFITO HOME 10 PLACES
    11 SPACES 15 STARS CR
    11 SPACES STAR 13 SPACES STAR CR
    11 SPACES STAR SPACE SLOGAN SPACE CR
    11 SPACES STAR 13 SPACES STAR CR
    11 SPACES 15 STARS DOWN ; OK


What it does

  On typing GRAFFITO and hitting the RETURN key, you get:


    * * * * * * * * *
    *               *
    *  FORTH RULES  *
    *               *
    * * * * * * * * *
                    OK

(The OK comes free. FORTH always does it if it likes what you've done.)

How is this result achieved?

  FORTH is usually described as consisting of a dictionary of defined words which are stored in memory when the tape (or disc) is loaded. I prefer to think of it as a book of Spells; since, when you invoke one by typing it and hitting the RETURN key, something always happens.

  It might be something disastrous. For example, if you type FORGET and hit RETURN the entire dictionary in main memory is wiped out and you need to load again. You can look at the dictionary words, together with their addresses in memory, by typing CAT. Any key pressed will stop the listing.

  Here's what happens when I try a few out of curiousity. (The word I type appears on the left and I underline the response).

WHO BY WFR, 7/30/78 OK 

(This refers to the fact that Bill Ragsdale wrote the original for this version. It was subsequently extended, however, by John Draper.)

Here's another

HELP 5CLO 5 CSA 8 CAS 6 REC  

(Actually, this is just the first line of a complete listout of the dictionary.)

  The listout above proved very useful to me, since I had very little documentation with this version. It revealed the fact that although FORTH recognises only the first three letters of a word it, also, takes account of the word length. That is, the first word above is CLO**, the third word is CAS***** A guess that CLO stood for CASSETTE LOAD and CSA for CASSETTE SAVE, proved right. The numbers in front of the words give their lengths.

  Now, it is obvious that a language which merely consists of a couple of hundred words (most of which are used by the system and not by the user) is not much use as it stands. The power of FORTH, however, lies in a small internal program which enables the user to define his own words by linking others.

  We'll first say precisely what is meant by a FORTH word. A word in FORTH is any set of characters limited at each end by at least one space, and containing no internal spaces.
HELLO is a word, and so are the three words:
AB-RA-CA-DAB-RA 1 *!)(*?

Such words mean nothing unless they either appear in the Dictionary already (for example, does); or you have put them in the Dictionary yourself by writing definitions for them. In the FORTH program above, words which I have myself defined are in boldface, to help.

  This brings us back to the program I started with.

  Let's look at the first line:

: STAR 42 ECHO; 
Here, I am defining the word STAR.

You must start such a definition with: a colon, followed by a space, and then the word you wish to define. All the words which then follow the defined word must already be in the Dictionary - that is to say, they must already be in the package (permanently defined) or have been previously defined by yourself. However, pure numbers need no definition at all.

Back to the first line:

42 is a number. (Needs no definition.)

  ECHO is a FORTH word. It says that the number preceding it is to be interpreted as an ASCII character, in this case, rather than as a number. (As a FORTH word, ECHO comes permanently defined, with the package.)

  ; This FORTH word terminates a definition.

  Thus I have defined STAR myself, in terms of pre-defined FORTH words.
  If I now type STAR and hit RETURN, a Star will be printed on the screen.

  The next definition I effect:

: STARS 0 DO STAR LOOP;

enables me to type the expression 10 STARS, and a row of ten stars will be printed. In this definition, 0 is a number not an ASCII character. DO and LOOP are FORTH Words (they come ready-defined in the package) and STAR has been defined already, but by myself. I will leave the discussion of the Loop construction until later

  Continuing the explanation of the program:
: PLACES 0 DO DOWN LOOP ;
DOWN is a FORTH word. It moves the Cursor down one line. The rest of the words we have met already, and PLACES is thus now defined in terms of them.

: SLOGAN ."FORTH RULES" ;

  ." is a FORTH word and anything enclosed between it and the next" will be printed on the screen in just the format you choose. If I now type SLOGAN, it causes FORTH RULES OK to appear on the screen.

  We can now play about. Type in:

3 STARS SLOGAN 3 STARS

and we get:

* *  * FORTH RULES * * * OK 

  If we precede this command by:
10 PLACES
then the message will appear 10 lines down.

  It is best to type HOME at the beginning, because this clears the screen and puts the Cursor at the top.

Now the last line:

  GRAFFITO is another word I have defined; it's a longer definition, with the termination word ; just before OK (see the program).
GRAFFITO is almost self-explained, except for the words SPACE, SPACES and CR. Now, these are ready-defined FORTH words. CR stands for Carriage Return, and SPACE and SPACES have obvious meanings.

  I have not attempted to produce the slickest program for producing the above result but have chosen a form which comes close to being directly readable.

  It is possible to relieve the tedium of writing 11 SPACES five times over by the definition:

: ON SPACES ;
Now ON means what SPACES meant before.
Why? Because we have defined it to be.

  ON and SPACES can now be used interchangeably. You‘can, if you want, from now on retain ON in the Dictionary permanently and so save time in formatting.

  There is one point to bear in mind. If you redefine SPACES to mean something else, for example:

: SPACES ." HOLES" ;

its original definition will be lost to you, since the Dictionary is searched from the last entry backwards. But, any FORTH Word which contains the word SPACES will not lose the original FORTH definition of the word SPACES. This is

FORTH

because these FORTH words are deeper in the Dictionary than your definition.

  Now the command FORGET SPACES will remove your definition (i.e. SPACES will no longer be replaced by HOLES), and also remove all above it. But if you then type FORGET SPACES again, you will remove about half of the actual FORTH dictionary that you loaded in! So remember to FORGET carefully.

Here is another short program which involves time delays. It uses do-nothing Loops for this purpose.


: TING 7 ECHO ;
: GAP 200 0 DO LOOP;
: GAPO 600 0 DO LOOP;
: S GAPO TING GAP TING GAP TING;
: O GAPO TING GAPO TING GAPO TING;
: SOS S O S;

Typing SOS now gives the Morse Code signal for SOS

Then try this:


: HELP! 100 0 DO SOS LOOP;

Type HELP! and it does it 100 times.

  Throughout this article, numbers are decimal. In some versions of FORTH the system starts up in HEX and in some in DECIMAL. Appleforth V 1.5 starts in HEX, so you type DECIMAL at the very beginning of each program so as to stay in Decimal until you decide to change.


  Now to explain the Loop construction. There are several different types, but the one we have been using is the DO LOOP form. This is as follows (using the Morse Code example).


: GAP 200 O DO LOOP; 

The Loop is invoked by typing GAP and, as usual, hitting the Return key. This automatically sets up a counter which starts at 0 and increments by 1, up to 199, and on each step looks for something to do. In the example above it does nothing two hundred times, but if it was required to do anything the word or words telling the Loop what to do would be placed between DO and LOOP. Here, as there is nothing to do the result is simply a time delay. This, by the way, gives a good measure of the speed of the language. In this test, 1000 steps of do nothing in BASIC took 1.3 seconds on an IBM 370/115, and about 1.4 seconds on my Apple 11. The equivalent in FORTH takes . 14 seconds on Apple 11.


  Straight comparisons on all tests are not easy, and the results will obviously depend on the algorithm chosen for evaluation functions. (Number crunching is not the application in which FORTH is used to best advantage). I shall give the results of other tests in a later article.

To look at a simple task for the Loop to perform, we go back to the Morse Code example:


: HELP! 100 0 DO SOS LOOP 2;   

Now we see that the command SOS, between DO and LOOP, and defined earlier, will be executed 100 times. That is, from 0 to 99. When the counter gets to 100 it stops without executing SOS. But suppose we don't want to be definite about the number of executions? We might then define:


: HELP! 0 DO SOS LOOP ; 

No upper limit has been specified. We cannot now invoke HELP! as it stands; but, remember, whenever we type HELP! after it has been defined, it is replaced by all the words in its definition. Therefore, typing:

10 HELP!

is precisely the same thing as:
10 0 DO SOS LOOP an

  The FORTH word for causing a value to be printed on the VDU is:

. (ie, a full stop) Being a word, it must be spaced away from the words on each side of it by at least one space. We can now use this FORTH word to print out the first N natural numbers:


: NATNUM 0 DO I . LOOP ;

Here, I means, Get The Count Value

. means, Print It

If I now type:10 NATNUM


I get: 0 1 2 3 4 5 6 7 8 9


If I prefer this printout to run from 1 to 10, I can define:


: NATNUM 0 DO I 1 + . LOOP ;

Notice that FORTH uses Reverse Polish Notation (RPN) so that to add 1 to I we write

1 1 + .

which will add 1 and print the result.

  Now we'll sum the first 10 natural numbers, starting from 1:


 : SUM 0 10 0 DO I 1 + + LOOP ;

The reason for the 0 immediately after SUM is that when we have got the counter first value I = 0, and added 1, we then add the sum to what we have already (that's what the second + is for); but there isn't any initial value to add to, so we must initialise it. That's what the first 0 does.

For a sum in which the upper limit is not initially defined, we define:


: SUM 0 DO I 1 + + LOOP. ;

Now the first 0 defines the lower limit of the count. To sum the natural numbers from 1 to 50 you type 0 50 SUM Here the initialises the Sum to zero, 50 sets the upper limit, and the word SUM is automatically replaced by:

 0 DO 1 1 + + LOOP     

Those readers who are already familiar with BASIC programming will realise that the slightly bizarre way we express ourselves in FORTH is because we avoid defining variables, This helps to make FORTH run faster and economises on memory. In BASIC, we would define variables SUM and NUM, and proceed:


10 SUM = 0
20 NUM = 0
30 FOR I = 1 TO 10
40 NUM = NUM + 1
50 SUM = SUM + NUM
60 PRINT SUM

Now try the following simple exercises.

Define EVEN and ODD to list the first Neven and odd natural numbers, the number N to be chosen at run time.

Define SUMEVEN and SUMODD to sum them.

When writing these programs, arrange for the results to be written on a line below your invoked word; and for SUM to be printed before the answer.

I will answer all readers' queries sent to me care of Computer Age

.

  In concluding this first article, I want to suggest that, if you are interested in FORTH, you nag the software distributors to handle it.


Bibliography

  Microforth Primer (Forth Inc). Obtainable from D E Butler, 239 Kimbolton Road, Bedford.

  6502 FORTH Reference Manual (Programma International Inc). .

FORTH High-level Programming Technique on Micros Elisabeth D Rather & Charles H Moore)

  FORTH for Microcomputers (John S James in Dr Dobbs Journal, No. 25).

  FORTH: A New Way to Program a Minicomputer (Charles H Moore, National Radio Astronomy Laboratory, USA)