|Previous Page > Listings Index > Forth for BASIC users.|
FORTH for BASIC users
Will assemblers become a
thing of the past? Ian
Maclean thinks so and
helps to hasten the day by
showing you how to hit
ALL OF A sudden Forth seems to have taken off in a big way; not only have we seen the arrival of the Jupiter Ace - the first-ever Forth home computer but the ZX-81, Atom, BBC and Vic can all now use Forth, making it the second most popular high-level micro language. Speed is a major element in its appeal, and its speed is due to the fact that Forth is a compiled language, that is, the high-level words are translated into a pseudo-machine code and then run in this form giving up to 90 per cent of the speed of machine code. Could assemblers soon become a thing of the past? I think the answer is undoubtedly yes, within the next five or ten years.
A good Forth analogy is given by the balls they put into plastic pipes in bingo. The number is put on to the top and can only be taken from the top of the pile. A stack is a pile of numbers worked on a last-in, first-out system. In Forth, the reserved words Do, If, Int, take their arguments from the stack; a simple Basic-Forth example:
BEEP 50, 100 100 50 BEEP
Although Beep is not standard Basic or Forth, it demonstrates the principle admirably. The Basic word Beep is interpreted during Running, the interpreter comes across the word Beep and commences to look for the two numbers and the comma which it requires to function. In the meantime, the Forth compiler has put the two numbers on the stack and already executed the word Beep.
Before I proceed with the comparisons there is one further idea which the Basic user has to come to terms with, and that is that there is no actual program. Each sub-program is defined and given a name by which it can be called and executed, for example, a routine called Part 1 would have the following structure:
: PART 1
Note the space between the colon - which signified a word definition - and the name. Also notice that the word is terminated with a semi-colon.
On then to the Forth words which you can put in your word definitions.
Let: The Basic word Let is used in assigning a value to a
variable. In Forth there are no pre-defined variables, so before you can define a word using a variable, you have to define it. To set up variable A, for example, with an initial value of zero we type:
0 VARIABLE A
Now the computer knows what you are talking about when you refer to A. To put a number in a variable, A again, we type:
where n is the number to be put in A. To recall the value from A back on to the stack we use
This may seem strange at first, but so did Basic when I first started programming.
Goto: This word does not exist in Forth, nor does it have any direct equivalent. The nearest thing to it is Gosub - this is easy. To call a routine, the routine is first defined as a word, then the word is simply incorporated as with any other Forth word. So in this way a program may be initially written as several subroutines and end up defined by a single word and Run as such.
For-Next: These words are replaced in Forth by Do and Loop or + Loop. The limit and initial value of the loop are put on to the stack first, before the word Do:
10 0 DO
This is the same as
FOR variable =0 TO 10
At the end of a loop we can replace the Basic word next with either Loop or + Loop. Loop means "Jump back to Do and add 1 to the loop counter". + Loop means "jump back to Do and add the value on the stack to the loop counter". In other words, + Loop is used for step values other than 1 and is added at the end of a loop like so:
2 + LOOP
This means "step 2" although other values could be used instead of two.
Our loop now looks like this:
(limit) (initial value)
(step value) + LOOP
In Forth, there are several kinds of loop, a further two, in fact. These are conditional loops;
Begin - Until and Begin While. These two types are quite simple, but come in very handy.
Firstly, Begin-Until; this is a conditional loop which will repeat the routine it encloses until a positive value is left on the top of the stack before Until. Since we know how to read variables, we can control a Begin-Until loop with one, A, for example:
A @ UNTIL
The same applies to the Begin-While loop which will repeat the loop While the value before While is positive. Again, if the variable A is controlling the loop, it would look like this:
A @ WHILE
Rem: A remark in Forth is very simple, the comment being closed within brackets:
(THIS IS A COMMENT)
Be careful here, Forth has no mathematical parentheses, so if you attempt anything along those lines you will end up with a jumbled comment.
Input: Forth's equivalent of Basic's Input is Query, which opens up the input buffer for a numerical value to be entered.
Forth also has another kind of input statement, though: Line, which will take an alphanumeric input from the buffer and execute it as a "program" line, before returning back to the main flow. I use it at the end of games, for example, one called Game:
: YES GAME ;
: NO ;
In this example Line allows the user to call a word; in this case it would follow the prompt "play again". By typing Yes the user calls the word Yes which in turn executes Game again. If he types No then he executes the word No which Quits the program and hands the keyboard back for
keyboard back for programming.
Print: Forth has two very different words for printing characters. Firstly, there is ." which is the equivalent of Print and is used in much the same way:
is the same as
Note the space between." and the first character of the string. To get on to the next line we use a carriage return CR, thus:
." HELLO" CR
is the same as
In some cases it is only necessary to print out one character, here we use Emit. Emit will print out after the last print position the ASCII character whose value is on the stack. For example:
prints an 'A'. To Emit a space we can use a special word called
Further to this, to emit several spaces Forth contains a word called Spaces, which takes the number of spaces to be emitted from the top of the stack. So, to emit ten spaces we type:
If-Then If-Then in Forth is the same in theory as Basic's version, but is somewhat 'jumbled'. As a rough guide:
IF A=0 THEN
These are the same. This can be repeated with 0> and 0<. The word If in Forth is considerably
different from Basic's version as it will execute the routine if it finds a positive value on the stack. This positive value is usually 1 or 0 as this is what 0=, 0< and 0> produce:
0= puts a 1 on the stack if the value formerly on the stack is zero.
0< puts a 1 on the stack if the value formerly on the stack is negative.
0> puts a 1 on the stack if the value formerly on the stack is positive.
The word Else can also be used so:
(routine executed if
stack value is positive)
(routine executed if
stack value zero or less)
Finally, a program - listing 1 - written to demonstrate some of the terms briefly covered in this article. Don't be put off by its apparent complexity as all of the words used have been explained.