www.jupiter-ace.co.uk

Previous Page > Index of General Forth Information > Forth Feature BASIC/FORTH.


Forth Feature BASIC/FORTH from Personal Computing Today December 1982, page 44
 FEATURE
BASIC/FORTH
BEGINNERS GUIDE
TO PROGRAMMING
LANGUAGES
In the first part of our series on programming
languages we give you a guige to BASIC and FORTH.
BASIC
The BASIC computer language started life back in the 1960's as a humble teaching tool has since become one of the most popular computer languages throughout the world.
It was originally written by a professor at Dartmouth College as a means of introducing his students to computer programming, before taking them on to higher level languages such as FORTRAN and COBOL. From these humble beginnings BASIC was refined and modified as people found it could be used for serious programming applications, until it arrived in its various forms in the machines we use today.
The reasons for its wide acceptance seem to stem from the fact that it is a very accessible programming medium. With BASIC a complete beginner can have programs up and running in a matter of minutes and actually understand how the program is carrying out its functions. The term BASIC stands for Beginners All Symbolic Instruction Code. This refers to the way in which the machines instructions are presented to the operator in the form of English words. For instance, If you want to display something on the screen you will give the computer the instruction PRINT, or if you want to transfer to a different part of the program then you will instruct computer to GOTO that part of the program.
Despite the advantages that BASIC gives in terms of accessibility and ease of use, it does have its drawbacks, One of the biggest faults with BASIC as a language it that is has, over the years, developed various dialects. Every time a manufacturer updates   or  releases   a     new
machine they seize the opportunity to make some minor changes to the BASK:. in an effort to Improve it. This is all very well as it results in more powerful versions of the language but unfortunately it also means that programs written on one machine with one version of BASIC are not easily transferable to another machine running a different version of BASIC.

Go Slow
BASIC also suffers from being very slow when compared to some of the more elegant languages around. This is because most of the BASICs that machines use are interpreted as opposed to being compiled. When running a program with a BASIC interpreter the machine looks at each line in turn and then converts them from the BASIC that you have written into a form in which the computer can run. Because this happens every time you run the program and every time a statement is encountered, it does slow the operation of the machine down considerably.
To get round this some machines offer a piece of software known as a compiler. This performs the same actions on a program as the computer does when running the code, but instead of actually running the program, it leaves the code in the form which the computer can use, which can then be saved. This means that when this new code is loaded and run, the computer does not have to go through the tedious process of converting the code because it is already in the correct form. This speeds up the program execution by up to 10 times.
Despite all its shortcomings BASIC is still the best introduction
to computer programming around and it will take something very radical to displace it from its position at the top of the league.
Chris Palmer.

FORTH
FORTH is an extremely versatile language, which enables you to meet your own specific requirements. Defined words are used from the ROM to produce new words, which in turn can be utilised to make additional words. The final program will eventually be one word. In other words (pardon the punt) you can add FORTH commands to the language, which in BASIC is difficult, or virtually impossible, without having hefty machine code routines.
FORTH also has a great advantage over BASIC by virtue of its speed, which is approximately ten times faster. This is especially useful when writing games, fast sorting and data retrieval programs.
It is possible to use FORTH in direct mode. In BASIC in order to work out 10 + 4, you would type in:
PRINT 10 + 4

but in FORTH you type in:

10 4 + .

This may seem unusual, but it is more logical than BASIC. You are instructing the computer to put 10 and then 4 onto the stack, add them together, leaving the result on the Top Of the Stack (TOS) and then printing out the result (.), taking the number off the stack.
If you wished to leave a number on the stack, as well as printing it, you would use the word DUP (as . removes the number from TOS as well as printing it), which copies whatever is   on TOS

Forth Feature BASIC/FORTH from Personal Computing Today December 1982, page 45
FEATURE 
  
whatever is on TOS and puts this on top of the original number. So if we typed in:

10 4 + DUP .

14 would be printed out, but also 14 would be left on the stack.

+ . - , *, and / are only used on the top two numbers of the stack.
For example, to evaluate 20 X(16 - (4 X 19)), remembering that the operators will only manipulate the two top numbers of the stack, you have to input:

20 16 4 19 * - * .

which gives the correct result of
-1200, Let us look at the stack to see what is happening.

TOS 20 20 20 20 20 20 -1200

16 16 16 16 -60

4 4 76

19

Understand? We have seen what DUP does, let us now look at some other stack commands. SWAP reverses the top two numbers on the stack. so.

10 4 SWAP . .

would result in 10 and then 4 printed out and not the reverse. if we wanted 20 *((4 *19)- 16) as opposed to 20*(16 - (4 * 19) you would type in.

20 16 4 19 * SWAP - * .

which prints out 1200 as the result. Let use look again at the stack to see what is happening.

TOS 20 20 20 20 20 20 20 1200

16 16 16 16n 76 60

4 4 76 16

19

* SWAP - *

OVER duplicates the number that is Second On the Stack (20S) to the TOS.

If our problem was 4 *((5 *4)-5), by using OVER we only need to type in:

4 5 OVER OVER * - *
which is the same as:
4 5 4 5 * - *

- Let us look at the stack once again.

TOS 4 4 4 4 4 4 60

5555 -15

4 4 20

5

OVER OVER * - *

The last two stack operations that I shall mention are ROT and DROP. ROT rotates the rust three items on the stack, so:

1 2 3 ROT

causes the third of the stack (30S ?) to become TOS and 20S to become third of the stack and TOS to become 20S.

5 2 3 ROT * + .

will work out (5 *3) + 2.

DROP does as it suggests, dropping the number that is TOS.
Having looked at how to manipulate the stack, we can now start to compile our own words. We must always start a word definition with a ":" and end it with ";", putting in a space between each word.

: ADD + DUP . ;

ADD is the name of our new word, which will t the two numbers that are on the stack, DUP the result and then . the number left on TOS.

So if we now type in:

10 25 ADD .

35 will be printed out. We now have a new word, ADD, which is included in the dictionary and can
be used like any other word.

Due to limitations of space I have only been able to explain a restricted number of words used in FORTH, otherwise this article would become a volume on "How to learn FORTH"

I shall mention one last command though, DO . . .LOOP in the form:
(upper limit)(starting point) DO
(work to be done) LOOP

which is very similar to FOR ...NEXT in BASIC. DO. . . LOOP can only he used when defining words.

: R 100 0 DO LOOP:

is equivalent to:

10 FOR I= 1 TO loo
20 NEXT I

in BASIC.

If you need to use the index of the loop, you have to insert I, which puts the value of the index on the stack.

: R 100 0 DO I . LOOP ;

is the same as:

10 FOR I = 1 TO 100
20 PRINT I
30 NEXT I

Some BASIC programmers write unstructured programs, direct from the keyboard, but in FORTH it is not possible to easily write programs in this manner. FORTH programs are by far easier to write away from the keyboard. In FORTH, the final program. usually one word, consists of many other words, which can be written and tested in their own right.
David Harwood

Next month we look at the languages Pascal and Machine Code.