Why go Forth?
by THOMAS McNAMEE
As an ATARI BASIC programmer, I began noticing Forth articles at about the same time as I noticed BASIC's limitations. One thing that grabbed my attention was the frequent comment, "It's real fast."
Well, fine. Assembly Language is fast but it's a lot of work writing anything useful in it. Macro assemblers helped a little by allowing me to create a library of useful subroutines, but they were up to me to create. What I really wanted was easy access to the graphics and sounds functions of the ATARI, at speeds fast enough to write an arcade game, without having to develop advanced programming skills. Little did I know, but I was describing Forth.
My interest in Forth led me to ask: "What's in it for me?, and "How hard is it to use?" To answer these questions I'll discuss what Forth can offer the BASIC programmer, and then show the development of an example Forth "word".
Forth is analogous to the human body. The body has systems comprised of organs, organs comprised of tissues, tissues comprised of cells. Similarly, Forth is a hierarchical structure.
This structure creates what is called extensibility. It's like having a number of subroutines in BASIC, custom designed to perform any operation you could imagine, and calling them in the order in which they are to be executed. Extensibility makes Forth a very flexible programming environment. New commands are called words, and as they are defined, they are compiled into memory in a group called the dictionary. This compiled form of Forth executes at about ten to twenty times the speed of BASIC, or about half the speed of machine language. If this isn't fast enough, Forth comes with an assembler which permits critical routines to be coded in machine language. The assembler itself is written in Forth, which may give you some idea of the power of this language. With the assembler, Forth can be a high-level and a low-level language at the same time.
Forth code is also very compact. Once defined, a word can be used in many places in the application program. A "bottom-up" approach to program development allows very efficient program design. Words can be used in many applications. Once you have defined a word, it can be used in any programl with a suitable context.
NUTS AND BOLTS
The mechanics of Forth require getting used to, but they are not too difficult. Forth is a stack-oriented language, and uses RPN (Reverse Polish Notation) logic. The stack is used as a last-in, first-out storage area. To add four and three, you would type:
4 3 +
When you type 4 it goes on the stack. When you type 3 it goes on top of 4, pushing 4 down. When you type +, that's the signal to add the 4 and 3, replacing these with the value 7.
The stack holds results of arithmetical calculations, and passes parameters between words.
Suppose we have a frequent need to add three to the value on the top of the stack, We could create a new word for just that purpose:
3 + ;
Whenever 3PLUS was typed from the console, that operation would be performed. Let's take a look at the components of the above example.
The colon (:) is a Forth word which basically means "start defining". It takes the next group of characters and prepares to make a dictionary entry called 3PLUS.
The compilation process then takes the next group of characters (the 3) and tries to turn it into a number (in this case 3). The number is now compiled into the definition of 3PLUS! The plus sign, is a Forth word which means, "add the first two numbers on the stack together, and leave the result on the stack". The final character is the semicolon (;) which means "definition finished". Control now returns to the console.
This new word, 3PLUS, can now be used to add the value of three to any value on top of the stack. It can also be used in higher-level definitions such as:
2 + 3PLUS;
This would first add two, then three to the number on the top of the stack. If you don't understand this, don't worry. It just illustrates an important point: there is no limit to the number of ways you can structure your dictionary. Your library of commands can be as large and as varied as the memory size of your computer will allow.
Features of the ATARI not readily available from BASIC can be easily accessed at machine language speeds with Forth. This includes Player/ Missile graphics, versatile sound control, redefined character sets, and custom display lists. I am currently working on an arcade game which uses multi-tasking in the Vertical Blank Interrupt, several Display List Interrupts, ANTIC 4 graphics, and a new character set. These features were easily implemented in Forth, and can be quickly modified and improved. The program executes fast enough to provide quite a challenge at the highest level of difficulty.
A TASTE OF FORTH
Let's write a small program in both Forth and BASIC and see how they compare. This routine will produce a sweeping tone when [START] is pressed.
10 IF PEEK(53279)<>6 THEN GOTO 10
20 POKE 53761,168:REM Distortion = 10, Volume = 8
30 FOR FREQ =10 TO 200
40 POKE 53760,FREQ
50 NEXT FREQ
In Forth : WAIT BEGIN 53279 C@ 6 = ( LOOK FOR START KEY ) UNTIL; : TONE 168 53761 C! ( SET DISTORTION AND VOLUME ) 201 10 DO ( THE ACTUAL SWEEPING SOUND ) I 53760 C! LOOP ; : SWEEP ( PUT IT ALL TOGETHER ) WAIT TONE ;
To understand the Forth word SWEEP, several terms should be defined. Remember, every term in WAIT, TONE and SWEEP is either a FORTH or a number. The words contained in parentheses are comments only; they do not execute. In WAIT, C@ fetches the eight-bit value contained in the address on the top of the stack. After C@ is executed, the address is replaced with its contents. So, the definition of WAIT has this effect:
OPERATION STACK 53279 53279 C@ contents of 53279 6 contents, 6 = (compare) 1 if equal, 0 if not
The control structure BEGIN.....UNTIL loops back to BEGIN until the stack contains a true (non-zero) value when it reaches UNTIL. The word WAIT acts just like line 10 of the BASIC program.
In the word TONE, we reproduce the effect of the FOR/NEXT loop by using another control structure called DO . . . LOOL. The numbers that precede DO are the limit and the starting point, respectively. Our loop will execute by counting from ten to 200. When the index reaches 201, the loop terminates. Inside the loop, the index may be found with the Forth word I which leaves the current index on the stack. The final term to be explained is C!. It is the store operation opposite C@, and its function is this: take the second number on the stack and store it at the address at the top of the stack. The operation of TONE looks like this:
OPERATION STACK 168 168 53761 168, 53761 C! empty 201 10 201, 10 DO (begins loop) empty I (fetch index) 10 to 200 53761 10 to 200, 53761 C! empty LOOP (go back to DO if I < 201)
Finally, the words WAIT and TONE are combined to form SWEEP. One principle of Forth programming is that it is better to create many small, useful words than several large ones. WAIT could be used anywhere that requires the program to wait until [START] is pressed. We have another job for TONE below.
Both the BASIC and the Forth programs produce a rising tone. To hear the Forth tone well, however, a delay would have to be inserted in the DO.....LOOP structure.
Say your game project needs a succession of five of these sweeps to signal "red alert." In Forth, this could be accomplished using the DO . . . LOOP:
: REDALERT 5 0 DO TONE LOOP;
Will BASIC give you this kind of flexibility?
HOW TO START GOING FORTH
The best way to judge Forth is to read the best book on the subject: Starting FORTH, by Leo Brodie. It describes the language from the ground up, and will serve as a valuable reference after you start FORTH. This book is available from the FORTH Interest Group, PO Box 1105, San Carlos, CA 94070. FIG is an excellent resource for beginning and advanced programmers alike. They will send you a catalog upon request.
ValFORTH (available from Valpar International, 3801 East 34th Street, Tucson, AZ 85713) is a very versatile Forth package which can be purchased in modules.
There is a certain fanaticism in the Forth community about this powerful young language. It seems that once you start using Forth, you never turn back. This article touches only the surface of this language; there is much more ground to cover.
Forth is a language which is immediately useful, but it will never be fully explored. It allows every ATARI programmer to get the most out of his machine.