www.jupiter-ace.co.uk
Previous Page > General Forth Index > Not fifth but FORTH, a language that builds itself.
Computing Today October 1981 page 34
Not fifth but FORTH, a language that builds itself
F
ORTH is an unusual computer language, originally developed over 10 years ago by an American, Charles Moore, as an alternative to existing high-level languages. Its early uses included operating systems for the control of Radio Telescopes. Today, FORTH has developed into a powerful and easily implemented high-level language with a rapidly growing group of hobbyist enthusiasts both in this country and America. Although FORTH was developed for control applications, its vocabulary can easily be expanded to include additional program structures, for example, Pascal-like CASE statements, and data types. These extensions allow a wide range of different problems to be solved using FORTH. Moreover, FORTH allows programmers to develop their own special vocabularies and also allows easy access to all the software and hardware components of the computer being programmed.
Structure And Syntax
FORTH is both an interpreter and a compiler; merged within its structure are the best features of an interactive interpreter and a run-time execution system which produces program execution times similar to those achieved by a native code compiler. Its syntax is different from that of other high-level computer languages. Consider the following example - a program to add the numbers 2 and 3, and then display the result on a VDU.
2 3 + .
5 OK
FORTH employs the reverse Polish notation, where numbers are entered in an expression prior to operators. This mode of operation is also adopted by a number of popular calculators, notably Hewlett Packard's.
In the above example the numbers 2 and 3 are pushed onto a parameter stack as the FORTH program text is scanned from left to right. The operator '+' adds the top two numbers on the parameter stack and places the result back on the top of the parameter stack. Finally the operator '.' removes a number from the top of the parameter stack and outputs it to the display VDU. Most implementations of FORTH echo the word OK to the VDU to signify that the previous operations were completed successfully.
The operators which compose a FORTH program are called words. The
FORTH language consists of a dictionary of these words, linked together to form a list, and a small segment of machine code which is the kernel of the FORTH interpreter. The dictionary accounts for over 95% of the language.
The fundamental FORTH dictionary is roughly 40 primitive words long. Each primitive is usually written in machine code. New words are defined in terms of the primitives. In FORTH, as soon as a new word is defined it may be executed immediately or used to create additional words. FORTH does not use argument lists. Routines communicate through the parameter stack. However, FORTH does allow programmers to define variables but these variables are themselves operations which, for example, place the address in memory of the variable on the top of the parameter stack. This implies that FORTH programmers can not only extend the language by adding new words to the dictionary, but can also create their own variables or data types.
Rolling Your Own
When writing FORTH programs the process used to develop a new program is straightforward but different to, say, BASIC. Let us consider a second example. Imagine that we wish to tabulate a list of the cubes of a series of positive numbers. The first step is to create and add to the dictionary a FORTH word which calculates the cube of a number. The following fragment taken from a FORTH programming session at a terminal shows the FORTH code for a word called CUBE. Once a new word is defined it may be tested before use in other sections of the program under development.
: CUBE
  DUP DUP
  * *
The definition of a new word starts with the FORTH defining word colon':' which is followed by the name of the new word, CUBE in our example. The body of the new word then follows, being terminated by the FORTH word semicolon ';'. Notice that each word is separated by a space or a carriage return at the end of a line. Also that the body of the word may extend over many program lines. The word colon ':' is one of the FORTH operations called 'defining words'. A defining word takes the next word in the input
sequence, from a terminal or mass storage device, and prepares a dictionary entry for the new word. The word colon also switches the FORTH language into the compile mode so that the words which follow the name of the new word are compiled into the dictionary and not executed immediately. The FORTH language stays in the compile mode until the word semicolon ';' is read from the input device.
Testing the word CUBE is simply done by entering, at the terminal, test data, and the name CUBE, for example
5 CUBE .
followed by a carriage return. FORTH responds with
125 OK
The word CUBE expects to find the number to be cubed on top of the parameter stack. If we look at the body of the word CUBE the first word in the body is DUP. This word duplicates the number on the top of the parameter stack. The second DUP repeats the same operation. Hence, if the number 5 was on top of the parameter stack, following execution of the words DUP DUP we have 5, 5, 5 on the parameter stack. The word sequence ** is next. The FORTH word '*' takes the top two numbers on the parameter stack, multiplies them together and places the result back on the top of the parameter stack. Hence, after execution of the first '*' the parameter stack becomes 25, 5, and after execution of the second '*' the top of the parameter stack holds the number 125.
To compute a table of CUBES we must define a second word, which we will call TABLE.
: TABLE
  10 0
  DO
    CR I . I CUBE .
  LOOP
;
Again, testing the word TABLE is simply done by entering at the terminal the name TABLE followed by a carriage return. FORTH responds with
0  0
1  1
2  8
3  27
4  64
5  125
6  216
7  343
8  512
9  729 OK


Computing Today October 1981 page 35
PROGRAMMING LANGUAGES
The word TABLE uses the previously defined FORTH word CUBE to calculate the cube of a number. TABLE also demonstrates a number of additional FORTH features. The DO . . LOOP is a structured loop with a counting index incremented in unity steps. DO takes two arguments from the parameter stack, the 10 and 0 in the example TABLE. The initial value, 0 is the top item on the parameter stack. These loop parameters are written in the reverse order to most other high-level languages. The word 'CR' simply prints a carriage return on the VDU. The FORTH word 'I' causes the current value of the loop counter to be pushed onto the top of the parameter stack. The loop counter is stored on a second stack called the return stack.
Taking The Time
Although FORTH programs are interpreted the execution of FORTH words is in general very much faster than the same program written in BASIC. The next FORTH example demonstrates a timing benchmark. The FORTH word TIME computes 30000 empty loops. On a typical 8-bit microprocessor, TIME executes in roughly 4 S. The same loop executed by an Integer BASIC interpreter takes approximately 40 seconds.
: TIME 30000 0 DO LOOP ;
It is difficult to describe FORTH in detail because its extensible features allow each programmer to extend the language to solve the application which is being programmed. However, every FORTH implementation is characterised by a number of common elements; these are the dictionary, the parameter and return stacks, an inner and an outer interpreter, an assembler and a virtual memory management system.
The dictionary is an extensible threaded list of words, each entry in the list defines a word in the FORTH vocabulary. Two push-down stacks, the parameter and return stacks, are maintained by FORTH. These stacks are used to communicate arguments between words in a FORTH program. FORTH employs two interpreters; these are called the inner and outer interpreters. The outer interpreter is a conventional program for passing text strings from a terminal, and looking up each decoded word in the FORTH dictionary. If a word is found in the dictionary it is executed by calling the FORTH inner interpreter.
Unlike a BASIC interpreter the FORTH inner interpreter is very small, often 25 or less machine code instructions when run on an 8-bit microprocessor, which results in fast
execution. The FORTH inner interpreter. execution speed, although slower than that obtained with an optimised assembly code program, is often as fast as the run-times achieved by the code generated by an 8-bit native code compiler. In the future, when the next. generation of 16-bit microprocessors are adopted for personal computers, microprocessors like the ZILOG Z8000 and the Motorola 68000 will allow FORTH interpreters to run at even higher speeds. The speed will probably be in the region of 20 to 40 times faster than that obtained by an 8-bit microprocessor BASIC interpreter.
Most FORTH implementations include a resident machine code assembler which is used to create words constructed from machine code segments or to immediately execute machine code instructions. This language facility is important when programming critical timing operations or when controlling peripheral devices. FORTH assemblers use the reverse Polish notation with the assembler mnemonics following their arguments. Fully structured assembly programming is defined in the FORTH assembler syntax through the use of structured sequences which include IF . . . ELSE . . . THEN and other constructions.
In common with other computer languages, some form of mass storage device is necessary to store FORTH source programs and test data. FORTH defines a virtual memory system based on blocks which are fixed-length segments of disc space. These blocks may contain programs or test data. A number of buffers are also held in random access memory, so that the blocks can be lead into the buffers automatically from the mass storage device. If a block is modified in memory
it is automatically replaced on disc by the new version. This virtual memory management scheme removes the need for explicit mass storage read and write operations and hence the storage of FORTH programs and test data is independent of the mass storage operating system. Associated with the FORTH virtual memory scheme is a text editor which provides a means to edit program source texts. Normally both line and character editing facilities are supported.
How To Get It
The FORTH language is in the public domain and has been implemented on the majority of popular microprocessors, including the 808, Z80, 6800, 6502, 6809 and, more recently, the 16-bit 8086 microprocessor. Each implementation of the language varies in size from a 2K to 3K language subset to a 16K to 20K versions which include the machine code and virtual memory management language components.
The FORTH Interest Group, PO Box 1105, San Carlos, CA 94070, USA, publishes FORTH source listings and installation instructions to help programmers get started with FORTH. The following articles and texts are also worth reading.
1. Moore, C H, The Evolution of FORTH, an Unusual Language, Byte, August 1980, pp 76-92.
2. Fritzson, R, Write your own FORTH interpreter, Microcomputing, February 1981, pp 76-92.
3. Fritzson, R, Write your own Pseudo-FORTH compiler, Microcomputing, March 1981, pp 44-57.
4. Loeligen, R G, Threaded Interpretive Languages, Byte Books, 1981.
5. Forth Inc, Using FORTH, 1980.
6. Stevens, A FORTH primer, Kit Peak, 1979.

Computing Today cover page

Computing Today page 34    Computing Today page 35