This page (revision-5) was last changed on 03-Feb-2023 15:21 by Stefan Haubenthal 

This page was created on 04-Apr-2010 16:58 by Carsten Strotmann

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
5 03-Feb-2023 15:21 18 KB Stefan Haubenthal to previous Typo
4 27-Apr-2010 22:25 18 KB Stefan Haubenthal to previous | to last code ersetzt
3 04-Apr-2010 17:00 18 KB Carsten Strotmann to previous | to last
2 04-Apr-2010 16:59 18 KB Carsten Strotmann to previous | to last
1 04-Apr-2010 16:58 18 KB Carsten Strotmann to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 33 changed one line
If you are a person who is well-versed in BASIC and you have just come across the language FORTH, you may well want to know if you can translate your favourite programs into FORTH. The short answer is 'yes', and this tutorial will help you to do it. Although it has been writted with volksForth in mind, any remarks about FORTH should apply to other machines as well. Throughout this text, {{BASIC}} and {{FORTH}} words are highlighted. FORTH, like BASIC, has a vocabulary of words which you can either type in as commands or group together to make a program. FORTH has a word {{WORDS}} which prints the dictionary (that is all the words in the vocabulary) on the screen. In BASIC, you form a program by taking a collection of words and putting a line number in front. In FORTH, however, you define new words which then become part of the dictionary, just like the words already there in the volksForth. Like any of the original words, you can execute a new word by typing it in at the keyboard and you can use it in the definitions of other new words. You can think of FORTH words as being like subroutines and your final program as being a list of GOSUB statements- but they don't slow down your program as subroutines do. It is easy to debug programs written in this way because you can test each word separately to check that it does exactly what you want. The most common way of making a new word is the colon definition. Here is a very simple word which prints a message on the screen.
If you are a person who is well-versed in BASIC and you have just come across the language FORTH, you may well want to know if you can translate your favourite programs into FORTH. The short answer is 'yes', and this tutorial will help you to do it. Although it has been writted with volksForth in mind, any remarks about FORTH should apply to other machines as well. Throughout this text, <code>BASIC</code> and <code>FORTH</code> words are highlighted. FORTH, like BASIC, has a vocabulary of words which you can either type in as commands or group together to make a program. FORTH has a word <code>WORDS</code> which prints the dictionary (that is all the words in the vocabulary) on the screen. In BASIC, you form a program by taking a collection of words and putting a line number in front. In FORTH, however, you define new words which then become part of the dictionary, just like the words already there in the volksForth. Like any of the original words, you can execute a new word by typing it in at the keyboard and you can use it in the definitions of other new words. You can think of FORTH words as being like subroutines and your final program as being a list of GOSUB statements- but they don't slow down your program as subroutines do. It is easy to debug programs written in this way because you can test each word separately to check that it does exactly what you want. The most common way of making a new word is the colon definition. Here is a very simple word which prints a message on the screen.
At line 39 changed one line
The colon at the start says the next word is the name of a new word to be compiled into the dictionary and that everything following, as far as the semicolon, is the instructions to be executed when you use the new word. Here the name of the new word is MESSAGE and the instructions is ." (pronounced 'dot quote') which says 'print the following characters up to " on the screen'. When you type in MESSAGE it will print "This is volksForth". It will also print 'OK' afterwards to show that it has executed your commands without any problems. {{WORDS}} will now show that MESSAGE has been added to the dictionary. It also shows :, ." and ; which are all FORTH words in the volksForth Language. You can write the same thing in BASIC, like this
The colon at the start says the next word is the name of a new word to be compiled into the dictionary and that everything following, as far as the semicolon, is the instructions to be executed when you use the new word. Here the name of the new word is MESSAGE and the instructions is ." (pronounced 'dot quote') which says 'print the following characters up to " on the screen'. When you type in MESSAGE it will print "This is volksForth". It will also print 'OK' afterwards to show that it has executed your commands without any problems. <code>WORDS</code> will now show that MESSAGE has been added to the dictionary. It also shows :, ." and ; which are all FORTH words in the volksForth Language. You can write the same thing in BASIC, like this
At line 45 changed one line
and you can execute it by saying RUN, or RUN 10, or GOTO 10. If you want to use it more than once, you can add {{20 RETURN}} and say {{GOSUB 10}} to execute it. Some versions of BASIC allow you to put more than one statement in a line, in which case you would probably write {{10 PRINT "This isn't volksForth" : RETURN}} One of the advantages of the word MESSAGE over the set of line 10 is that MESSAGE is an English word and can convey an idea of what the word actually does - it prints a message. {{GOSUB 10}} doesn't mean anything in English, so you either have to remember what it does or list it. Putting in a comment, e.g. {{5 REM message}} helps you recognise what it does from the listing, but not when you are referring to it by a line number. You can put comments in FORTH words, too, by putting the text inside parentheses - {{(}} is a FORTH word that says 'ignore what follows till you come to )'.
and you can execute it by saying RUN, or RUN 10, or GOTO 10. If you want to use it more than once, you can add <code>20 RETURN</code> and say <code>GOSUB 10</code> to execute it. Some versions of BASIC allow you to put more than one statement in a line, in which case you would probably write <code>10 PRINT "This isn't volksForth" : RETURN</code> One of the advantages of the word MESSAGE over the set of line 10 is that MESSAGE is an English word and can convey an idea of what the word actually does - it prints a message. <code>GOSUB 10</code> doesn't mean anything in English, so you either have to remember what it does or list it. Putting in a comment, e.g. <code>5 REM message</code> helps you recognise what it does from the listing, but not when you are referring to it by a line number. You can put comments in FORTH words, too, by putting the text inside parentheses - <code>(</code> is a FORTH word that says 'ignore what follows till you come to )'.
At line 47 changed one line
With the volksForth decompiler, you can list any word you have defined, just as you can list any part of a BASIC program. {{SEE MESSAGE}} will write
With the volksForth decompiler, you can list any word you have defined, just as you can list any part of a BASIC program. <code>SEE MESSAGE</code> will write
At line 57 changed one line
( if you get an error message "SEE MESSAGE WHAT?", you need to load the SEE command with {{INCLUDE" D:SEE.F"}} )
( if you get an error message "SEE MESSAGE WHAT?", you need to load the SEE command with <code>INCLUDE" D:SEE.F"</code> )
At line 63 changed one line
BASIC uses infix notation. Instead of writing the operators in between the operands, {{2+3}} in FORTH you would say {{2 3 +}}.
BASIC uses infix notation. Instead of writing the operators in between the operands, <code>2+3</code> in FORTH you would say <code>2 3 +</code>.
At line 66 changed one line
So {{(2+4)*3}} becomes {{2 4 + *}} in postfix notation. There is a very common way of showing what words do to the stack. You list the operands that the word requires on the stack starting with the one lowest down and ending with the top. So with +, this is
So <code>(2+4)*3</code> becomes <code>2 4 + *</code> in postfix notation. There is a very common way of showing what words do to the stack. You list the operands that the word requires on the stack starting with the one lowest down and ending with the top. So with +, this is
At line 73 changed one line
A word can have any number of operands and leave any number of results. This makes it very easy to define your own functions. You don't have to declare a list of variables, you just write the definition bearing in mind that the numbers you want will be on the stack. e.g. to define a function which squares a number, in BASIC you would say {{DEF FN s(x)=x*x : REM x squared}} in FORTH, this becomes {{: SQUARE ( n - n squared) DUP * ;}}
A word can have any number of operands and leave any number of results. This makes it very easy to define your own functions. You don't have to declare a list of variables, you just write the definition bearing in mind that the numbers you want will be on the stack. e.g. to define a function which squares a number, in BASIC you would say <code>DEF FN s(x)=x*x : REM x squared</code> in FORTH, this becomes <code>: SQUARE ( n - n squared) DUP * ;</code>
At line 92 changed one line
| CHR$ | EMIT | Prints out the character whose ASCII value is on the stack. e.g. {{PRINT CHR$(32)}} or {{32 EMIT}} prints a space |
| CHR$ | EMIT | Prints out the character whose ASCII value is on the stack. e.g. <code>PRINT CHR$(32)</code> or <code>32 EMIT</code> prints a space |
At line 155 changed one line
| LET | | FORTH has variables just as BASIC does but you have to declare them before using them (like DIM and arrays). The word which does this is.... VARIABLE which puts the variable name in the dictionary along with space for a 2-byte number. e.g. VARIABLE SCORE Sets up a variable called 'SCORE'. You update its value with the word ! (pronounced 'store'). e.g. {{100 SCORE !}} makes 100 the value in STORE. You can put the current value of the variable on the stack using the word @ (pronounced 'Fetch') e.g. {{SCORE @}} puts 100 on the stack. See also the section on arrays for use of the FORTH word CREATE. |
| LET | | FORTH has variables just as BASIC does but you have to declare them before using them (like DIM and arrays). The word which does this is.... VARIABLE which puts the variable name in the dictionary along with space for a 2-byte number. e.g. VARIABLE SCORE Sets up a variable called 'SCORE'. You update its value with the word ! (pronounced 'store'). e.g. <code>100 SCORE !</code> makes 100 the value in STORE. You can put the current value of the variable on the stack using the word @ (pronounced 'Fetch') e.g. <code>SCORE @</code> puts 100 on the stack. See also the section on arrays for use of the FORTH word CREATE. |
At line 163 changed one line
| PRINT | . | Prints the number at the top of the stack on the screen. {{."}} Prints the subsequent characters on the screen. See introduction. e.g. {{PRINT "Hello"}} is in Forth {{." Hello" }}; {{TYPE}} Prints out a given number of bytes starting at a given address as ASCII characters. |
| PRINT | . | Prints the number at the top of the stack on the screen. <code>."</code> Prints the subsequent characters on the screen. See introduction. e.g. <code>PRINT "Hello"</code> is in Forth <code>." Hello" </code>; <code>TYPE</code> Prints out a given number of bytes starting at a given address as ASCII characters. |
At line 173 changed one line
FORTH doesn't have any array handling words of its own but it does allow you to set aide space in the dictionary for your own data. There is a word CREATE which puts a word name in the dictionary but nothing else. So CREATE ROW makes a word called ROW and when you type in 'ROW' it puts on the stack the address of the first byte in the dictionary after the definition of ROW. This may seem pointless, but there is another FORTH word, ALLOT which tags a specified number of bytes onto the end of the dictionary. This gives you a data field for your empty name, so if you now say 6 ALLOT you have made a 1-d array called ROW which is 6 bytes long. {{3 5 ROW + 1- C!}} stores 3 in the 5th element of ROW and {{5 ROW + 1- C@ }} reads it back again. (You need the 1- because the 1st element is at ROW+0.)
FORTH doesn't have any array handling words of its own but it does allow you to set aide space in the dictionary for your own data. There is a word CREATE which puts a word name in the dictionary but nothing else. So CREATE ROW makes a word called ROW and when you type in 'ROW' it puts on the stack the address of the first byte in the dictionary after the definition of ROW. This may seem pointless, but there is another FORTH word, ALLOT which tags a specified number of bytes onto the end of the dictionary. This gives you a data field for your empty name, so if you now say 6 ALLOT you have made a 1-d array called ROW which is 6 bytes long. <code>3 5 ROW + 1- C!</code> stores 3 in the 5th element of ROW and <code>5 ROW + 1- C@ </code> reads it back again. (You need the 1- because the 1st element is at ROW+0.)
At line 184 changed one line
One of the main uses for READ, DATA & RESTORE is for initialising arrays, so you can now define a word which will set up ROW (from the section on arrays)
One of the main uses for READ, DATA & RRESTORE is for initialising arrays, so you can now define a word which will set up ROW (from the section on arrays)
At line 212 added one line