APPLICATIONSPROGRAMMATION ★ Amstrad Forth: GO Forth . . . and here's an easy introduction ★

Amstrad Forth|Computing with the Amstrad)Applications Programmation
★ Ce texte vous est présenté dans sa version originale ★ 
 ★ This text is presented to you in its original version ★ 
 ★ Este texto se presenta en su versión original ★ 
 ★ Dieser Text wird in seiner Originalfassung präsentiert ★ 

MANY readers, having learned and used Basic on the Amstrad, will have quickly discovered that it is not the ideal programming language for all situations and that some applications, such as arcade-type games, call for a faster, more compact means of programming.

One solution is to write such programs in machine code, but for most people this is a very difficult and time-consuming task. A far easier and more enjoyable way is to use one of the many other languages which are becoming available for the Amstrad.

These include Pascal, Forth and Logo and, with the addition of a disc drive, Lisp, Prolog, Fortran, C and many, many more.

Each has its own advantages and disadvantages in different situations and, while one language may seem ideal for one particular application, it may prove to be too slow or take up too much memory in others.

What is needed is clearly a good all rounder, ideally a language which is fast enough for most requirements and not too wasteful of memory, while also being relatively easy to learn to use.

The language most fitting these requirements is Forth, and it is not surprising that it is the most popular second language among home computer users. It is a fast, compact, general purpose language, ideally suited to a variety of uses, and despite its unusual vocabulary and structure it is by no means difficult to learn.

Forth started life around 1969 and was originally used to control the complex movements of large telescopes. Since then it has been used by a wide and varied spectrum of users for an equally wide variety of uses.

Its main strength stems from the fact that although it contains many of the superior programming features of high level languages - such as loop structures and complex conditionals — it produces extremely compact programs which run at high speeds, typically 10 times as fast as Basic. In addition to this, you can modify and extend the language to suit any application you might require.

Your first task of course, before you can try out any of the following examples, will be to type in the program, which is a complete implementation of Forth for use on the Amstrad.

Note that this program does not provide you with a real version of Forth, it merely simulates its operation. It works by converting each new word into a special internal format which is then interpreted by Basic whenever the word is executed.

Since Basic is itself interpreted by the Amstrad this means that any Forth programs created will run extremely slowly. However this version is almost identical to real Forth systems and, as such, it will enable you to experiment with this powerful language, using the techniques outline in this article. You will then be able to decide if Forth is the language for you and, if so, you can then buy one of the commercial versions available for the Amstrad.

Forth is not without its drawbacks, and these are mainly due to its unusual vocabulary and its reversed method of operation. All commands in Forth, or words as they are known, expect to have their arguments - the variables or numbers which they operate on - given to them before each command - not after as is the case with most languages.

For example, if we wanted to add two numbers together in Basic and print out their result, we would use a statement in the form:

PRINT 3 + 8

but in Forth we would write this as:

3 B + .

where the dot (.) is the Forth word for print. This form of arithmetic is known as Reverse Polish Notation, or RPN for short, and operates in conjunction with an arithmetic stack of numbers.

The way in which Forth interprets the above command is as follows. First the numbers to be added are put on to the stack - first the 3 then the 8 - then the Forth word + is executed.

This, like most Forth words, operates by taking numbers off the stack, processing them in some way, and returning the result to the stack for use by subsequent words.

In this case the top two numbers are removed from the stack and added together and the result of this addition is then put back on to the stack.

The next word - . - then removes the topmost number from the stack and prints it out to the screen. Forth then prints the message OK to show that the statement has been executed without error. Note that all Forth words must be separated from each other by a space.

These operations leave the stack in exactly the same state as it was before. This is a very important feature since it allows subsequent words to operate on values which were put on the stack before the above sequence of instructions was executed.

The top of the stack always contains the last number put there and if a new value is put on to the stack the old number is pushed down so that the new value is now the topmost item.

You may well be wondering how Forth knew that we only wanted to add two numbers together and not three or four or even more. The answer to this is that the word + always operates on exactly two numbers and always returns just one result. This is also true of most arithmetic operations in Forth, such as multiplication and division.

This does not prevent us from using complex expressions in Forth, it just means that we have to be careful in deciding how to express them. For example, if we wanted Forth to evaluate the expression:

13 + 2 * 9

we would start by multiplying 2 by 9 to get an intermediate result which we add to 1 5 to produce the final value.

An alternative method is to add 1 5 and 2 together and then multiply this result by 9, but this will give us a different answer and is not the usual way to evaluate expressions of this type - Basic, and most other languages, would use the first method.

To multiply 2 by 9 in Forth we must type:

2 9 *

which will leave the result - 18 - on the top of the stack. This could be tested by printing the top stack number using . but since we need this value for the next part of the calculation, we will leave it where it is.

Next we must add 15 to the value on top of the stack by typing:

15 +

This leaves the result as the new top stack item, which can then be printed out. So our complete evaluation becomes:

2 9 * 15 + .

which prints out the correct answer of 33. Note that most versions of Forth use only whole number, or integer, arithmetic, with numbers usually in the range -32768 to +32767, and cannot normally handle floating-point or decimal numbers.

This method of arithmetic is not as complicated as it may seem. The best way to learn it is to try using it in Forth and, after some practice, you will find it almost as easy to use as normal arithmetic and just as powerful.

The real power of Forth, however, comes from being able to add new words to its vocabulary and to re-define existing ones.

Supposing you preferred to use English words for arithmetic, instead of the symbols +, - and so on and would also like to use Basic's PRINT in place of Forth's dot (.). All you have to do is type:

: ADD + ;




to create your new arithmetic words, and:

: PRINT . ;

to enable you to use a standard PRINT command.

Our arithmetic expression could now be evaluated using:


which would have exactly the same effect as the previous example.

In fact the previous example would still work, since we haven't actually re-defined the original arithmetic words but have simply created additional names for them.

All new Forth words are defined in this way, by bracketing the statements between a colon and semicolon. The colon indicates to Forth that you are about to define a new word and it must be followed by the word's name.

Next come the actual Forth words which will be executed when the new word is used and these may be either standard words - such as the + and -of the previous example - or they can be other new words which have already been defined. Finally the whole definition is ended by a semi-colon. Quite complex words can be built up in this way, with some words being used in the definition of other words, which are themselves used in other definitions, and so on.

This is exactly how a program is constructed in Forth - by splitting each task into a series of smaller tasks. These can then be defined easily using standard Forth words, and all linked together in later definitions.

In the end the complete program might consist of just one word which need only be typed for all the associated words to be executed.

Forth programs, of course, do not just consist of arithmetic expressions - as in the previous examples - and many other standard words are provided.

These basic words, which come with all versions of Forth, are known as the core vocabulary, and include the facilities for implementing such features as variables and loops so that complex programs may be written.

You can see the complete vocabulary displayed by typing:


  • That's enough to be going on with this month. Next time we'll see how these Forth words are used.


★ PUBLISHER: Computing With The Amstrad
★ YEAR: 1986


» Amstrad  Forth    (Computing  with  the  Amstrad)    LISTINGDATE: 2013-09-15
DL: 77 fois
SIZE: 813Ko
NOTE: 4 pages/PDFlib v1.6

Je participe au site:
» Newfile(s) upload/Envoye de fichier(s)
★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...

» Applications » Background Music
» Applications » Restore (CPC Amstrad International)
» Applications » 6128 Emulation RSXxs
» Applications » Spooler (CPC Amstrad International)
» Applications » Interrupt Gesteuerte Musik (CPC Amstrad International)
» Applications » Profiler (Computing With the Amstrad)


L'alinéa 8 de l'article L122-5 du Code de la propriété intellectuelle explique que « Lorsque l'œuvre a été divulguée, l'auteur ne peut interdire la reproduction d'une œuvre et sa représentation effectuées à des fins de conservation ou destinées à préserver les conditions de sa consultation à des fins de recherche ou détudes privées par des particuliers, dans les locaux de l'établissement et sur des terminaux dédiés par des bibliothèques accessibles au public, par des musées ou par des services d'archives, sous réserve que ceux-ci ne recherchent aucun avantage économique ou commercial ». Pas de problème donc pour nous!

CPCrulez[Content Management System] v8.7-desktop/cache
Page créée en 122 millisecondes et consultée 1035 fois

L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent  du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.