Previous Page > General Forth index > Clarifying Forth.
Clarifying Forth from Computing Today November 1983 page 270

All Forth words expect every number on which they operate to be on the stack. Numbers can be added to the stack simply by typing them in and they will be used from the top down. Consider the example in the illustration below:
This could, of course, have been keyed in as a single line;
2 4 6 * +
but this does not fully demonstrate the activities involved. The important thing is that each number performs its own unique activity - that of placing itself on the top of the stack - independently of the operators.
The word * can be thought of as taking two values from the stack, multiplying them together, and leaving the result of the top of the stack. The important thing to remember is not that all operators are preceded by their operands (ie reverse Polish), but that the latter should be on the stack regardless of how they got there
Since many words demand that numbers are on the stack in a particular order (eg -) it is often necessary to change this order, a process referred to as 'stack manipulation'. The selection of words available for this is shown in Figure 2.

orth computer language. Most people know that it uses reverse Polish notation and involves manipulation of something called a 'stack', but even those with considerable experience of more conventional languages like Basic and Cobol often find the simplest Forth program completely unreadable. Despite this, versions of Forth - admittedly of varying quality - are now available for micros.
Originally developed in the 60's by Charles Moore, Forth was first implemented on an IBM 1130 ie 'third generation' computer. Moore saw his brainchild as a 'fourth' generation language, but since the 1130 only allowed five character identifiers he settled for Forth, to the subsequent delight of journalists looking for snappy word plays.
Forth's first big success was running the National Radio Astronomy Observatory in Kits Peak, Arizona. This system was designed by Moore himself who then formed Forth Inc. It continues to produce high quality (though very expensive) Forth compilers.
The basic Forth system comprise a small pre compiled 'nucleus' of primitive commands. These can be executed interactively from the keyboard or combine in programs which themselves then become new commands. In fact Forth makes no distinction between its indigenous primitives and user defined commands, all of which are referred to simply as 'words'. Any of these words can be redefined at will, though on many systems this will prompt a warning message.
New words are created by entering a colon followed by a space and the name of the new word, then the stream of words to be executed terminated with a semicolon.
The example above will create a word called NEW-WORD which, when executed, prints the message 'Hello'. Note that Forth treats spaces as separators so they cannot be embedded in names and one must be used either side of every command. You will probably have guessed that the word ." is equivalent to Basic's PRINT, printing out the string contained within the quotes.
The stock of words known to the system at any one time is referred to as the 'dictionary', and usually this can be listed out using the command VLIST. The essence of Forth Programming is simplicity. To begin with, simple general purpose words are created. These can be combined in various ways to perform increasingly powerful functions. This modular approach not only makes debugging much easier, but also imposes a discipline of well structured and efficient programming.
The first job of a programmer is to develop a 'tool kit' of useful words which may eventually evolve into a unique new language specially tailored to suit the specific application. It's one of the most appealing aspects of using Forth. Values are passed between Forth words using the stack, which can be seen as a heap of numbers with the most recent addition on the top.
Stack manipulation can be quite tricky and a useful aid is a breakpoint routine which allows you to view the contents of the stack at any point during the execution of a word. Unfortunately, few Forth systems include this feature, but you should be able to write one yourself without too much effort.
Just as the stack is used as temporary storage for numbers, an area of memory called PAD is used to hold strings of text. The exact position of PAD at any time depends on how large your dictionary is. This is because it is defined as an 80 byte block beginning at a fixed offset above the top of the dictionary, and is illustrated with the memory map shown.
The word PAD leaves an address on the stack pointing to the beginning of the allotted memory. This direct use of memory demonstrates the control which Forth gives over your computer, an aspect of the language which is not to be taken lightly. The careless transposition of value and target address will almost invariably cause an irrevocable system crash. Even more sinister is the 'dormant bug' effect where a corrupted area of memory does not become apparent until accessed later by some perfectly innocent program.
Clarifying Forth from Computing Today November 1983 page 271
stores a value in a given address (Forth programmers actually pronounce this as 'store'), thus
10 30241 !
will store the value 10 as a 16-bit number in memory bytes 30241 and 30242. The opposite word, a (pronounced 'fetch') will retrieve a 16-bit number from the address on the stack and, assuming we had executed the previous example
30241 a
would leave a 10 on the stack. Other words are provided for fetching and storing single bytes (c a and C!), 32-bit numbers (2 a and 2!, operating on four bytes at a time), and for moving chunks of memory around (MOVE or CMOVE which expect two addresses and a byte count on the stack).
Basic programmers frequently have trouble in coming to terms with Forth's use of numbers. This is largely because Basic is fairly sophisticated about its arithmetic while Forth uses fixed length numbers whose range of values must be known beforehand. 'Single' numbers (16-bit) in
FORTH Memory Map
Forth can be up to +32768 signed or 65536 unsigned (note that this allows the system to access 64K of RAM). On the other hand 32-bit or 'double' numbers can be up to +1,073,741,824 signed or 2,147,483,648 unsigned.
Numbers with embedded decimal places can only be output at the terminal or printer by using special number formatting primitives, and this is symptomatic of Forth's inherent lack of input/output routines. In fact there are four main words provided for screen/keyboard I/O. KEY waits for a single keystroke and leaves the AscII value of the character entered on the stack, while EXPECT takes in a given number of characters and leaves them
Forth Stack words
at a predefined address(it expects the address and count on the stack). The output equivalent of these are EMIT and TYPE. Thus if we were to execute PAD 10 EXPECT
the Forth would wait for 10 characters from the keyboard, or a carriage return, and store them as a string at PAD whereupon they could be printed on the screen using PAD 10 TYPE.
Clearly, Forth's input and output routines are very clumsy, and this aspect of the language has convinced many that it is inappropriate for applications which require extensive screen I/O, such as database management. As is often the case with Forth the answer is to write it yourself, and though this can involve considerable effort there are those who would argue that it's well worthwhile.
The table above shows the selection of Forth "words" available for manipulating the Forth "stack". These words act on the stack in much the same way as the operations described in the diagram at left, with the syntax operating such that data (in this case numbers) are taken from the top of the stack and the bottom of the stack is 'pushed up'.
The example in the main text above shows how these words work. In the line 10 30241!, the value 10 is stored as a 16-bit number in memory bytes 30241 and 30242 by the Forth 'word'!.
Clarifying Forth from Computing Today November 1983 page 272

One of the big problems with Forth implementations in home computers is that they almost always use cassette-based data storage. Using cassettes can be inconvenient at the beat of times, but Forth always seems particularly ill suited to it.
As you will see from the memory map (Figure 3) Forth allots an area in high memory for disk buffers. Most disk based systems have a 4K buffer area and data is written to and read from the disk in 1K 'blocks'. There are thus four separate buffers and n disk blocks depending on how many K the disk will hold.
Manipulation of the disk buffers depends largely on the single word BLOCK. Essentially this word, given a disk block number, will read the required block into the least recently used buffer and leave the starting address of that buffer on the stack. It's actual operation is more complex and is illustrated by the flowchart in Figure 4.
As you'll have gathered, an unexpanded Forth system presents the user with a low level language with infinite potential but few sophistications. The idea is to allow the greatest control over your machine using the simplest possible set of basic functions. Some suppliers provide various extra utilities as source code, though frequently you will find yourself rewriting them to your own specifications. At the very least, however, you should expect a standard line editor with which to enter source onto disk blocks (the source of programs compiled from the keyboard is not saved) and a full set of assembly language primitives which can be used to create normal forth words.
Although the nucleus itself should contain a fairly standard set of words, there are three distinct types of Forth compiler. FIG Forth is one created according to guidelines laid out by the Forth Interest Group, while Forth-79 is based on the work of the Forth standards team. The third type, Polyforth, is Forth Inc's proprietary product. Both FIG Forth and Forth-79 are in the 'public domain', that means that anyone can produce such a compiler without fear of copyright infringement. This explains the large number of such systems on the market.
It has been claimed that Forth offers an operating system, low-level language and high-level language in one homogenous package. If you look at the pie chart (Figure 5), however, you'll see the professional use of Forth is almost entirely confined to low-level programming tasks such as industrial control system. It seems that the control which Forth gives the programmer over a machine makes it an ideal tool for applications which might otherwise be written in assembly language, while its lack of sophisticated I/O or inherent file handling facilities tends to put off programmers used to conventional high level languages.
The future of Forth in applications such as control systems and robotics is now assured. Whether it can compete in the world of business software, jostling for position with languages such as Basic, Cobol, Pascal and C, remains to be seen. Whatever the outcome, one fact remains: a Forth system is limited only by the imagination of the programmer.
Pie chart
personal computing news cover page  personal computing news cover page 270

Page 271   Page 272