
One of the hardest things for me to find was a method of using the FORTH language to perform simple calculator functions through a written program. The logical operators (+, -, * and /) would allow direct calculations such as:
2 5 + . <7 displayed>
7 15 * . <105 displayed>
These types of calculations would take the first two numbers, separated by an empty space, and perform the function designated by the logical operator that followed them and display the answer as directed by the period.
This type of calculation could be executed directly from the execution (lower) screen or from a program line written on the console screen and then executed from the execution screen.
To write a program that would allow numeric variables to find the totals was something else. A program in BASIC to perform the addition feat would be as follows:
10 PRINT "ENTER FIRST NUMBER?"
20 INPUT A
30 PRINT "ENTER SECOND NUMBER?"
40 INPUT B
50 PRINT A+B
It would not be to difficult to write additional programs or expand on this program to include the other logical operators. All four functions can be combined in a single FORTH program listing as follows:
0 ( MATH PROGRAM )
1 : READ ." ? " S@ ># DROP ;
2 0 VARIABLE A
3 0 VARIABLE B
4 : A1 ." ENTER FIRST AMOUNT " READ
A ! ;
5 : A2 ." ENTER SECOND AMOUNT " READ
B ! ;
6 : ADD EOFF CLS A1 A2 A @ B @ + .
;
7 : SUB EOFF CLS A1 A2 A @ B @ - .
;
8 : MUL EOFF CLS A1 A2 A @ B @ * .
;
9 : DIV EOFF CLS A1 A2 A @ B @ / .
;
Once it is compiled (CPL from the execution screen), lines 6 thru 9 will perform the calculations. You might want to leave out the EOFF command until you are sure the syntax (mistakes) is correct in the listing. Also, do not press ENTER after the last character is entered on the consol screen. Otherwise the first line will be scrolled up off the screen and be lost. Use SHIFT/EDIT to enter the execution screen.
If you have been working with FORTH, you probably know that it is very unforgiving regarding improper program entry. Certain commands become unoperational and the cursor may refuse to blink if an incorrect program is attempted to be compiled.
ADD will add, SUB will subtract the second number from the first, etc. These are actually four different programs but all use the same elementary routines except for the logical operators.
Lines 2 & 3 initialize the variables A & B to be equal to 0. Line 1 defines the command READ to be a device like BASIC’s INPUT command. The characters following the ” ? ” store the keyboard entry on a stack momentarily and then transfer the number into either variable A or B with lines 4 and 5.
The @ in lines 6 thru 9 actually attach the correct number to the variables A & B where they are coupled with the proper mathematical operator to give the correct result. Without the @ in the formula, all sorts of incorrect answers will result.
In other FORTH news, Soft-Magic is coming out with a word processor for the TREE FORTH chip. We hope to do a review of it in this column soon.