Home > Previous Page >  Forth for BASIC users.
Archive Search  
FORTH for BASIC users
Forth For BASIC users - Your Computer February 1983 page 68.

Will assemblers become a
thing of the past? Ian
Maclean thinks so and
helps to hasten the day by
showing you how to hit
the stack.
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:
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:
A !
where n is the number to be put in A. To recall the value from A back on to the stack we use
A @
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:
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:
Rem: A remark in Forth is very simple, the comment being closed within brackets:
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:

(main program)
: 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

Forth For BASIC users - Your Computer February 1983 page 69.

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:
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:
A@ 0=IF
These are the same. This can be repeated with 0> and 0<. The word If in Forth is considerably

Stack			Definition
: NUMBERS			Program name - type this word to run 
QUERY			Inputs value
LINE			Allows Forth word to be called
;			End of numbers word definition
:SQUARE			Program name
DUP			Duplicates number on top of the stack
*			Multiples the two numbers on the top and
			second stack positions
.			Outputs the number on top of the stack
;			End of Square Word definition 

: CUBE			Program name
DUP * SQUARE		Squares number and calls 'square'
			to square it again and print it out
;			End of cube word definition
: PATTERN			Program name
10 0 DO			Starts loop from 0 to 10
I			I retrieves the loop counter value
SPACES			Outputs 'I' number of spaces 
LOOP			Increments loop counter and jumps 
			to 'DO' if not 10
0 10 DO			Starts loop from 10 to 0
0 1-			Takes 1 from 0. Puts -1 on stack
+ LOOP			Adds -1 to loop counter; decrements loop 
			counter by 1
;			End of pattern word definition
Machine Company Price Where obtainable
ZX-81 QCP £10 11c, Hercies Road, Uxbridge, Middlesex
ZX-81 Artic £29.95 396 James Reckitt Avenue,
Hull, North Humberside HU8OJA
Atari Maplin £49.95 P.O. Box 3, Rayleigh, Essex
Vic Addda £38.95 Adda: high street stockists or
154 Victoria Road, London W3
Atom Acornsoft £17.50 4A Marcket Hill, Cambridge CB2 3NJ
BBC Level 9 £15 229 Hughden Road, High Wycombe,
Buckingamshire HP13 5PG
Dragon32 Microtanic £24.95 235 Friern Road, Dulwich, London
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.