I think that FORTH is a language,
that a grade school child can learn to use effectivily,
if it is presented in bit sized pieces,
with the proper motivation.
Charles H. Moore
STRENGTH IN FORTH PART ONE: HAS FORTH GOT A MOTHER? by C. Janssen
To begin with: FORTH is a computerlanguage, about 17 years of
age. It takes us back to the seventies..U.S.A...and an IBM-
1130........
It would be a waste of time to tell you the whole of FORTH's
history at this stage. Read about that in BYTE ( august 1980 ).
Whereas Charles H. Moore, the father of FORTH, himself wrote that
article, you can't accuse me of gossiping either. FORTH is more
than merely a computerlanguage; learning FORTH is equally
adapting an attitude to programming a computer. If you were
programming before e.g. in BASIC, you will have to modify your
way of thinking. If you weren't you will gradually develop that
attitude. "Keep it simple", is the adagium by which FORTH-
programming is best described. Simplicity leads to programs, that
are well lined-out, well structured, readable and flexible.
There is something more about FORTH you should know. In FORTH the
programmer is held responsible for what the computer will perform
at the end of all. FORTH is not a foreseeable language; it will
not think, where you failed to do so. So you can't blame FORTH
nor your computer when the outcome of your blood, sweat and tears
is rubbish. What kind of language is FORTH ? A good one in the
first place.
Still a queer one for whom is attached to BASIC or Pascal.
FORTH is an interactive, extensible, high-level language, very
fast and very compact and highly transportable. (For a curious
eye only: FORTH can also be described as an interpretive threaded
language. The threading can be direct or indirect. There are some
FORTH-impementations which are true compilers. They use
machinecode subroutine threaded code, instead of a list of
addresses. The exact meaning of all this will be explained, when
we will dig in the dictionary.)
INTERACTIVITY
What we are going to do next, is demonstrating some features of
FORTH to underline the description of FORTH, we gave above.
Why hesitate ? Grab your FORTH-disc, crumple it into your drive
and squeeze that mouse, or strike a key. (FORTH won't strike
back....not yet !!). I suppose you've got a manual with your
FORTH-system. Read it carefully on how to handle in your case,
adapting if minor differences occur. Such as: typing upper or
lowercase, maximumlength of words etc. etc.
In demonstrating things I will use words, which I won't explain
immediately. You don't mind that, I assume.
Now a word on typing FORTH.
As space allows the planets and stars to make their revolutions,
a space allows a FORTH-word to turn around and make its moves. So
between every word at least one space is a must. All output of
FORTH will be underlined. (In the text, not on your screen !!).
FORTH is interactive. You type, FORTH types back.
Now type 3 5 + . (Where I type , you press RETURN instead).
FORTH says OK. See ? You type, FORTH types back. Interactivity !!
Don't bother about that +-sign hanging around at the end of the
addition. You will learn "why" soon enough.
EXTENDING...
Now type VLIST OK. Or type WORDS OK. No, that was not The
Holy Bible nor modern poetry stumbling down your monitorscreen;
it was FORTH's dictionary. Remember that ! It is a very important
feature of FORTH. In some respects all those words resemble
statements, functions and directives in BASIC. Don't confuse the
FORTH-expression "word" with the 16-bit value that other
languages call a word and that in FORTH is called a cell. In
BASIC you get a closed, delimited package of the language. But
not so in FORTH........
Now type VLIST , and hit a key or something else. Anyway, try to
stop the listing in order to see the very beginning of VLIST, the
word on top of the dictionary: CREATE. (Your FORTH may have an
other word on top of course. It just depends......)
Keep it in mind. Now enter : HELLO ." Hideehi !" ; OK. Done ?
You didn't forget to type : and ; ? You must, you know.
Now show the dictionary in paged mode again. And be surprised !
On top of the dictionary isn't CREATE, but HELLO. By Jove !! You
just entered a new word into the dictionary, which is now
extended. Don't believe them Basics; FORTH will treat his
youngest child in the very same way as he treats his beloved
older ones. That's extensibility ! Enter HELLO Hideehi OK .
Now let's mix the new HELLO with some older words. Enter : HELLOS
3 0 DO HELLO CR LOOP ; OK . To prove my words, enter HELLOS
Hideehi
Hideehi
Hideehi OK . No discrimination, eh ? Quod erat demonstrandum.
ERRRORSSSSSsssssssht.....
Did you notice that FORTH always said OK when all ended well ?
Alas, some of us computerpeople do make mistakes, even using
FORTH !( Don't panic, they live abroad. I know one ).
Enter : FENCE ; MSG #4 FENCE is not unique OK. Now this is not
really an error; it is a warning and the only one FORTH gives.
The word FENCE was already placed in the dictionary, now it is
not longer unique. FORTH doesn't bother, you may redefine FENCE
and all other words as often as you like and in which way
whatsoever. FORTH always considers the last definition as the one
to be used in new definitions. But....you guessed, don't...FORTH
won't bother, I said, but you will !! Executing new FENCE, FORTH
will do nothing. And that's not we want a computer to do. So
let's forget about FENCE. Make a paged VLIST. FENCE is on top, as
you can see. Now enter FORGET FENCE OK. Make a paged VLIST
again. See, FORTH has completely forgotten about FENCE i.e. that
do-nothing-FENCE, as the original is still there. See for
yourself. Convinced ? Yes ? Thank you. So FORTH is extensible ànd
destructable as well.
Now enter : LUNATIC 5 3 = IF ." Well, Pythagoras ?" ; MSG #19
Conditionals not paired. (I.e you forgot THEN or ENDIF )
And not OK ? No, no, this time it is really an error. You didn't
complete a loop. And that's not OK. In this situation the FORTH-
system is likely to collapse, if the error was allowed to pass
undetected. So FORTH struggles for life. The error-detecting and
reporting facilities are poor in FORTH, as to allow FORTH to run
at maximum speed. For example, there is nothing to prevent you to
fill the entire dictionary with nulls. Responsible, remember ?!
On the other hand you can rebuild FORTH to your exact demands and
that's flexibility. Of course yòu won't need all this error-
stuff, but it is handy to know.... helping your friends.
HIGH-LEVEL
FORTH is a high-level language. They told me that. High-level
means here, that FORTH itself has been written in an other (low-
level) language, such as 68000 machine-code. In most
implementations of FORTH, part of it is written in machine-code,
part of it in...FORTH.
If high-level means, that a computerlanguage more or less takes
after the human language, then FORTH is all that too. FORGET
FENCE sounds quite human, isn't it ? The more human, as FENCE
indeed is a barrier. "Behind" it words are protected against
FORGETting.
TRANSPORTABILITY
Most FORTH-systems consists of three main parts: - FORTH's
precompiled dictionary;
- FORTH's Editor;
- FORTH's Assembler.
You already saw the dictionary. The editor will be spoken of in
the near future. The Assembler....I really don't know. I will of
course write about the pecularities of a FORTH-assembler, but the
assemblerlanguage itself ...well, read the course elsewhere in ST
NEWS. Besides, I have some other devotions too, you know !!
The third reason is now to be explained.
The FORTH Standard Team (a committee of main FORTH-users, amateur
as well as professional) now and then specifies very precisely
the requirements of a standard system: e.g. the 79-standard, the
83-standard. These requirements involve the minimum Required Word
Set as well as the resources of the host computer. On whatever
computer FORTH may run, the Required Word Set will be virtually
the same. The Assembler will differ, depending on the
microprocessor. I don't know a bit about the MC68000 yet. Clear ?
Not only the Assembler will differ, the hardware of a specific
computer sets its limits to e.g. graphics, disc-handling, the
operating system may have its characteristic effects on
implementing a FORTH-system, but using the required word set on a
mainframe will produce the same outcome as on a Aquarius.
More, the possibility of extending your system may enlarge the
portability between systems. Suppose your system lacks 2VARIABLE.
Well, define it ! In this way : 2VARIABLE CREATE 0 , 0 , ; SPEEDY
AND COMPACT
I brought up the Assembler in relation to FORTH's compactness and
speed of execution. In Acorn User ( June 1986 ) David Johnson-
Davies described how 10 different languages coped with the same
benchmark. They were all BBC-B implementations, so running on the
6502-processor. The results:
Language Code size (bytes) Time (secs)
Assembler 215 2.64
S.Pascal 424(machinecode) 19
FORTH 58 21
BCPL 60 (Clintcode) 42
ISO-Pascal 68 (BI-code) 70
ISO-Pascal (fl. pnt) 80 (BI-code) 79
BASIC 82 185
BASIC (fl. pnt) 67 248
Lisp 204 326
Microprolog 366 359
The table makes another point very clear. If you got to be faster
than your shadow, the only way of improving FORTH's speed is to
program your time-critical FORTH-routines in the FORTH-assembler.
Yes, FORTH is fast,compact and (again) versatile.
It was the ATARI-company to write a FORTH-language, in which they
programmed their world-famous games. In learning FORTH you are
not far away from home !
THE STACK AND RPN
Now enter 3 5 + OK. We've done that earlier. But we did not
care for the result at that moment. We will do now. Enter . .
Enter what? Yes, a dot is a FORTH-word. To clarify things a bit,
let us type FORTH-words in braces: { }. So enter {.} 8OK. See,
FORTH knew it ! You might have thought otherwise....
Nevertheless, numbercrunching is not very popular with
FORTH. For his arithmetic, FORTH uses a stack and R.P.N. That
struck you right in the face, didn't it ? Non-FORTH
creatures describe their arithmetic operations in infix notation,
i.e. the operator (+ - * / ) is placed between the operands (the
numbers): 3 + 5. In R.P.N. or postfix notation the operators are
fixed after the operands. Lisp uses the third possibility: prefix
notation. The operator advances the operands:( PLUS 3 5 ).
R.P.N. is short for Reverse Polish Notation. Why Polish ? Well,
it was a Polish logician, J. Lukasiewicz, who invented an
extremely concise and readily mechanised method to write logical
definitions. ( A computer is all logic, remember ?). The stack is
due to the reversed notation. A stack is a certain type of
computermemory. Almost all languages make use of a stack. Mostly
hidden from the programmer. As FORTH promised you complete
control of your machine, you may as well control the stack.
The FORTH-stack is a LIFO-stack. LIFO stands for Last In First
Out. The last item put on the stack, is the only one you can
easily move from the stack, without further manipulation. To
reach the fifth item, you have to remove all four items above.
Imagine a pile of trays. The one on top is the only one you can
comfortably remove, without breaking the rest. Forth has two
stacks. The one meant here and now is called computationstack or
parameterstack, as for the two possible actions: arithmetic and
parameterpassing.
A word may produce items (values and addresses), leave them on
the stack, where an other word can find them and use them to
fulfill its task, passing other items to other words...etc.
That's parameterpassing. Putting items onto the stack, the ones
already there are pushed further 'downwards'. The stack has
only one entrance, from on top.
There are no backdoors or side-entrances. Top of stack is a very
common expression in writing about the stack. I'll use the
abbreviation TOS (Top Of Stack). The other stack is called
returnstack. On this stack FORTH puts a list of pointers as to
keep track of what to do next on his dizzy trip through the
labyrinth of your Atari.
You may use the returnstack as some extra hand to temporarely
store numbers.
BEHAVIOURISM
When a FORTH-word is typed at the keyboard it is usually executed
as soon as the RETURN-key is pressed. Executing a word FORTH has
a run-time-behaviour. FORTH then carries out the various tasks,
the programmer wanted the computer to do when he was creating
that particular word. But earlier, when the programmer entered
(at last) his newly created word, FORTH had to turn to compile-
time-behaviour. FORTH then wrote the new word in a dictionary
form in the dictionary. This process is often termed as
compilation, but strictly accurate it would be to call it
interpretation. Compilation should produce native machinecode,
rather then a list of addresses. But as you will learn soon both
words are in the dictionary: INTERPRET and COMPILE. Does FORTH
confuse you ?
Pfeeeew ! That's enough for today. O yes, answering that silly
question in the headline: no !! FORTH has got a father, Charles
H. Moore.
SUMMARY
FORTH was invented by Charles H. Moore in 1969. Apart from being
a computer language, it is also an attitude to programming a
computer. FORTH is an interactive , extensible, high-level
language, fast, compact and highly transportable.
Statements, functions, directives, logical, arithmetical and
stack operators are called WORDS. A word is an indivisable
element through which FORTH executes. Indivisable here means:
nothing can be left out to achieve the goal the word is created
for. Words are stored in the DICTIONARY. A dictionary is not a
fixed entity. Words can be added, words can be deleted.
As the programmer is fully responsible, FORTH's error-messages
are limited. All number handling is carried out on the STACK.
This stack is also used to pass parameters from one word to
another.
On the RETURNSTACK FORTH holds the information (pointers) to keep
track of what to do next. The status of FORTH can be EXECUTION or
COMPILATION. Executing a word FORTH fulfills his predefined task.
Compiling a word FORTH writes a word as a new entry in the
dictionary in a particular form.
REMEMBER
R.P.N. - Reverse Polish Notation or postfix notation is
a way of writing down a arithmetic formula.
The operator(s) follow(s) the operands.
{VLIST} - Outputs the contents of the actual dictionary.
{WORDS} - Identical to {VLIST} in other systems.
{+} - Adds two (single-length) numbers on top of the
stack and replaces them by the result.
{.} - Outputs the top stack item, removing it from
the stack.
EXTRAS
This course is limited. The limits are set in the first place by
the know-how of me, the writer. There are several different
implementations of FORTH on the Atari ST and I don't have them
all at hand. Secondly there is the amount of time between one
issue of ST NEWS and the next one.....and ooooh waiting !!! In
the third place ST NEWS is not a FORTH-magazine.
So some additional reading may do you good. There are lots of
good books about FORTH. I just mention one title. That's because
I think, it's the best book ever written on computers and FORTH.
Although it is not adapted - as far as I know - to the '83-
standard, you will ENJOY reading it. The book: STARTING FORTH,
Leo Brodie, ISBN 0-13-842930-8 (hardcover), ISBN 0-13-842922-7
(paperback). And I can recommend you WATERSHIPDOWN, 'cause it's
fun.
EXERCISES
Save your strength for next time !! Best wishes !!
Disclaimer
The text of the articles is identical to the originals like they appeared
in old ST NEWS issues. Please take into consideration that the author(s)
was (were) a lot younger and less responsible back then. So bad jokes,
bad English, youthful arrogance, insults, bravura, over-crediting and
tastelessness should be taken with at least a grain of salt. Any contact
and/or payment information, as well as deadlines/release dates of any
kind should be regarded as outdated. Due to the fact that these pages are
not actually contained in an Atari executable here, references to scroll
texts, featured demo screens and hidden articles may also be irrelevant.