The Forth Corner

Author(s): Gene G. Buza
Publication: Synchro-Sette
Pub Details: Synchro-Sette v2 n7 (Volume 2, Issue 7)
Date: August 1983
Pages: 9-10, 15
THE 4TH CORNER heading logo with cartoon of a crashed car
THE 4TH CORNER heading logo with cartoon of a crashed car

ELEMENTARY INSTRUCTION

In last month’s session, we made reference to the book STARTING FORTH by Leo Brodie. On page 60 is a routine that creates a large letter “F” out of asterisks. A similiar program to create a large letter “E” from dollar signs reads as follows:

0 ( LARGE LETTER-E)
1 : START EOFF CLS ;
2 : DOLLAR 36 EMIT ;
3 : DOLLARS 0 DO DOLLAR LOOP ;
4 : MARGIN CR 16 SPACES ;
5 : X1 MARGIN DOLLAR ;
6 : X2 MARGIN 10 DOLLARS ;
7 : E START X2 X1 X1 X2 X1 X1 X2
;

You will notice that there are line numbers, similar to a basic program. The program, if executed on most FORTH machines, would produce a large letter “E” indented 16 spaces. This will not occur on the ZX/TS machines however with the TREE-FORTH chip. The program works equally well with or without line numbers on the ZX/TS computers. Line numbers make it easier to visualize.

The reason is because, although the word (command) SPACE and SPACES are defined in the command dictionary of other FORTH interpreters, they do not exist in Tree Forth’s PluriForth version.

This does not mean that it can’t be done. All we have to do is define these two commands and insert them into the “dictionary”.

Let us examine the program first so that we can understand how it works. The computer would first be put into the “editor” mode by pressing the SHIFT and “1” keys.

Line #0 is formatted in a manner that works like the REM statement in BASIC. The parentheses tell the computer to ignore what is inside them. In line #1, a routine called START uses EOFF to unlock the bar that separates the 2 screens (editor and execution) and CLS clears the screen completely.

Line #2 tells the computer that “DOLLAR”, when called upon, is going to be a dollar sign. The number 36 is the ASCII code for the dollar sign.

Line #3 takes DOLLAR and formats it into having the capability of being duplicated into a string.

Line #4 is where we have a problem. SPACES normally acts like DOLLARS but with empty spaces instead of dollar signs. In a little while I will show how to create the word SPACES so that it acts this way and add it to the dictionary. Line #4 creates a routine called MARGIN that, when called upon, is a string of 16 spaces.

Line #5 creates a routine that places a dollar sign at the 17th column on the screen by first printing the MARGIN and then the DOLLAR sign and calls it S1.

Line #6 makes a routine like line #5 but with 10 dollar signs and utilizes the DOLLARS routine from line #3 and calls it S2.

Line #7 combines the previous routines in a manner that would create the large “E” on the screen.

Up to this point, all of these routines exist and if compiled from the “execution” screen, would not show anything on the screen. This is analogous to writing a program in BASIC but not RUNning it.

If this program is compiled on the ZX/TS computer, an error message stating an undefined word (SPACES) has been encountered. If the program works at all, the “E” will be printed without the margin.

The solution is simple! Use the editing feature found in the list on page 23 of the user’s documentation to create two new lines between #3 and #4 (SHIFT 9) and then renumber the line numbers from #4 on by increasing their value by 2. Now insert lines #4 and #5 so that the final listing looks as follows:

0 ( LARGE LETTER-E)
1 : START EOFF CLS ;
2 : DOLLAR 36 EMIT ;
3 : DOLLARS 0 DO DOLLAR LOOP ;
4 : SPACE 32 EMIT ;
5 : SPACES 0 DO SPACE LOOP ;
6 : MARGIN CR 16 SPACES ;
7 : X1 MARGIN DOLLAR ;
8 : X2 MARGIN 10 DOLLARS ;
9 : E START X2 X1 X1 X2 X1 X1 X2
;

In this program, we have used 6 existing commands: EOFF, CLS, EMIT, DO, LOOP and CR to create 9 new commands: START, DOLLAR, DOLLARS, SPACE, SPACES, MARGIN, X1, X2 and E, all of which are (or soon will be) in the FORTH dictionary, just like the existing commands.

You will notice that lines #4 and #5 are formatted exactly like lines #2 and #3. By the way, the CR in line #6 stands for Carriage Return, which causes the print to go to the beginning of the next line every time MARGIN is encountered.

So, as you can see, even though commands don’t exist in the PLURI-FORTH language, they can be created by the user to suit the needs of the program.

From the execution mode, you first have to compile the program into machine language by entering “CPL”. You will see the cursor in the upper screen scroll through the line numbers and the lines scroll in the lower screen. Every now and then, you may see an “ERROR R” message. Not to worry! “R” stands for redundant and means you are trying to compile a routine under a name where it has already been assigned, either in a previous compilation or it exists in the dictionary as an established dedicated PLURI-FORTH command. Do not assign names to commands that already are in the PLURI-FORTH language.

All that remains is to ENTER the letter “E” to execute the program (from the lower screen). You may first want to delete the word “START” from line #9 before compiling. If you made a mistake in entering the program, START will erase the program from the editor screen as well as erasing the editor screen itself. Once it is lost from the screen, there is no way of getting it back without re-typing it in.

The program, if it contains errors, will then have to be re-entered. This way, with START deleted, it will only run on the execution screen and can be tested that way. The program still remains on the upper portion (editor screen).

If it runs OK, re-enter START in line #9 and re-compile it. You may want to first enter “FORGET E” before re-compiling. This should remove all the “ERROR R” messages.

If you are entering a program into the editor screen, you may want to compile it before the screen fills up. If you don’t, the upper lines will scroll off the screen and are lost. You can compile the program with the “CPL” command as already mentioned from the lower screen or you can do it a line at a time by placing the cursor at the beginning of each line and pressing SHIFT “Q”. Remember if a program line extends to the next line, SHIFT “Q” will only compile the 32 characters in that line. The cursor will have to be brought to the next line and the process repeated.

A trivial item of interest – if you execute the program by typing “E” and then pressing the ENTER key, the large E is printed followed by the message “OK” at the next 2 character positions with the cursor at the beginning of the next line.

If you press the BREAK key instead of ENTER, the “OK” message is missing and the cursor is at the next character position.

These programs may seem very elementary in their function with the language being quite more complicated than BASIC. I tend to agree. It takes time and a lot of rhetoric just to explain a few basic concepts. I have yet to see a manual on FORTH that the raw beginner can really get his teeth into in a short amount of time. The same holds true for machine language but to a greater degree.

It seems that the aspiring FORTH programmer has a lot more to learn compared to BASIC and a longer time will be needed to have his/her efforts rewarded with sophisticated programs.

However, advantages such as speed, multi-tasking, separate data storage and retrieval, input-output control and much more powerful programming capabilities make this a language for the future. We intend to cover as many aspects of FORTH as possible in a tutorial manner presented in language for the beginner. We also encourage any and all reader donations on the subject.

PS – If any of you have the Tree-Forth chip and would like an alphabetized index for the documentation so that you can easily find subjects, write us a letter and send one dollar for postage and handling.

A more formal publication dedicated to FORTH for all computers is called FORTH Dimensions and is published 6 times a year at a cost of $2.50 per issue – $15.00/year U.S. – $27.00/year foreign air.

For more info contact:

FORTH Dimensions | Published by FORTH Interest Group | PO BOX 1105 | San Carlos, CA, 94070