JSyn - modular synthesis API for Java.
JMSL - Java Music Specification Language
PortAudio - cross platform audio I/O API for 'C'

Forth Tutorial

Translations: Chinese by Letoh

by Phil Burk of

Table of Contents

The intent of this tutorial is to provide a series of experiments that will introduce you to the major concepts of Forth. It is only a starting point. Feel free to deviate from the sequences I provide. A free form investigation that is based on your curiosity is probably the best way to learn any language. Forth is especially well adapted to this type of learning.

This tutorial is written for the PForth implementation of the ANS Forth standard. I have tried to restrict this tutorial to words that are part of the ANS standard but some PForth specific words may have crept in.

In the tutorials, I will print the things you need to type in upper case, and indent them. You can enter them in upper or lower case. At the end of each line, press the RETURN (or ENTER) key; this causes Forth to interpret what you've entered.

Forth Syntax

Forth has one of the simplest syntaxes of any computer language. The syntax can be stated as follows, "Forth code is a bunch of words with spaces between them." This is even simpler than English! Each word is equivalent to a function or subroutine in a language like 'C'. They are executed in the order they appear in the code. The following statement, for example, could appear in a Forth program:

Notice that WAKE.UP has a dot between the WAKE and UP. The dot has no particular meaning to the Forth compiler. I simply used a dot to connect the two words together to make one word. Forth word names can have any combination of letters, numbers, or punctuation. We will encounter words with names like:

They are all called words. The word $%%-GL7OP is a legal Forth name, although not a very good one. It is up to the programmer to name words in a sensible manner.

Now it is time to run your Forth and begin experimenting. Please consult the manual for your Forth for instructions on how to run it.

Stack Manipulation

The Forth language is based on the concept of a stack. Imagine a stack of blocks with numbers on them. You can add or remove numbers from the top of the stack. You can also rearrange the order of the numbers. Forth uses several stacks. The DataStack is the one used for passing data between Forth words so we will concentrate our attention there. The Return Stack is another Forth stack that is primarily for internal system use. In this tutorial, when we refer to the "stack," we will be referring to the Data Stack.

The stack is initially empty. To put some numbers on the stack, enter:

Let's now print the number on top of the stack using the Forth word ' . ', which is pronounced " dot ". This is a hard word to write about in a manual because it is a single period.


You should see the last number you entered, 9182 , printed. Forth has a very handy word for showing you what's on the stack. It is .S , which is pronounced "dot S". The name was constructed from "dot" for print, and "S" for stack. (PForth will automatically print the stack after every line if the TRACE-STACK variable is set to TRUE.) If you enter:

you will see your numbers in a list. The number at the far right is the one on top of the stack.

You will notice that the 9182 is not on the stack. The word ' . ' removes the number on top of the stack before printing it. In contrast, ' .S ' leaves the stack untouched.

We have a way of documenting the effect of words on the stack with a stack diagram. A stack diagram is contained in parentheses. In Forth, the parentheses indicate a comment. In the examples that follow, you do not need to type in the comments. When you are programming, of course, we encourage the use of comments and stack diagrams to make your code more readable. In this manual, we often indicate stack diagrams in bold text like the one that follows. Do not type these in. The stack diagram for a word like ' . ' would be:

. ( N -- , print number on top of stack )

The symbols to the left of -- describe the parameters that a word expects to process. In this example, N stands for any integer number. To the right of --, up to the comma, is a description of the stack parameters when the word is finished, in this case there are none because 'dot' "eats" the N that was passed in. (Note that the stack descriptions are not necessary, but they are a great help when learning other peoples programs.)

The text following the comma is an English description of the word. You will note that after the -- , N is gone. You may be concerned about the fact that there were other numbers on the stack, namely 23 and 7 . The stack diagram, however, only describes the portion of the stack that is affected by the word. For a more detailed description of the stack diagrams, there is a special section on them in this manual right before the main glossary section.

Between examples, you will probably want to clear the stack. If you enter 0SP, pronounced "zero S P", then the stack will be cleared.

Since the stack is central to Forth, it is important to be able to alter the stack easily. Let's look at some more words that manipulate the stack. Enter:

You will notice that there are two copies of 777 on the stack. The word DUP duplicates the top item on the stack. This is useful when you want to use the number on top of the stack and still have a copy. The stack diagram for DUP would be:

DUP ( n -- n n , DUPlicate top of stack )

Another useful word, is SWAP. Enter:

The stack diagram for SWAP would be:

SWAP ( a b -- b a , swap top two items on stack )

Now enter:

The word OVER causes a copy of the second item on the stack to leapfrog over the first. It's stack diagram would be:

OVER ( a b -- a b a , copy second item on stack )

Here is another commonly used Forth word:

DROP ( a -- , remove item from the stack )

Can you guess what we will see if we enter:

Another handy word for manipulating the stack is ROT. Enter:

The stack diagram for ROT is, therefore:

ROT ( a b c -- b c a , ROTate third item to top ) 

You have now learned the more important stack manipulation words. You will see these in almost every Forth program. I should caution you that if you see too many stack manipulation words being used in your code then you may want to reexamine and perhaps reorganize your code. You will often find that you can avoid excessive stack manipulations by using local or global VARIABLES which will be discussed later.

If you want to grab any arbitrary item on the stack, use PICK . Try entering:

PICK makes a copy of the Nth item on the stack. The numbering starts with zero, therefore:

PICK ( ... v3 v2 v1 v0 N -- ... v3 v2 v1 v0 vN ) 

(Warning. The Forth-79 and FIG Forth standards differ from the ANS and Forth '83 standard in that their PICK numbering starts with one, not zero.)

I have included the stack diagrams for some other useful stack manipulation words. Try experimenting with them by putting numbers on the stack and calling them to get a feel for what they do. Again, the text in parentheses is just a comment and need not be entered.

DROP ( n -- , remove top of stack ) 

?DUP ( n -- n n | 0 , duplicate only if non-zero, '|' means OR ) 

-ROT ( a b c -- c a b , rotate top to third position ) 

2SWAP ( a b c d -- c d a b , swap pairs ) 

2OVER ( a b c d -- a b c d a b , leapfrog pair ) 

2DUP ( a b -- a b a b , duplicate pair ) 

2DROP ( a b -- , remove pair ) 

NIP ( a b -- b , remove second item from stack ) 

TUCK ( a b -- b a b , copy top item to third position ) 


Start each problem by entering: Then use the stack manipulation words you have learned to end up with the following numbers on the stack: Answers to the problems can be found at the end of this tutorial.


Great joy can be derived from simply moving numbers around on a stack. Eventually, however, you'll want to do something useful with them. This section describes how to perform arithmetic operations in Forth.

The Forth arithmetic operators work on the numbers currently on top of the stack. If you want to add the top two numbers together, use the Forth word + , pronounced "plus". Enter:

This style of expressing arithmetic operations is called Reverse Polish Notation, or RPN. It will already be familiar to those of you with HP calculators. In the following examples, I have put the algebraic equivalent representation in a comment.

Some other arithmetic operators are - * / . Enter:

Some combinations of operations are very common and have been coded in assembly language for speed. For example, 2* is short for 2 * . You should use these whenever possible to increase the speed of your program. These include:

Try entering:

One thing that you should be aware of is that when you are doing division with integers using / , the remainder is lost. Enter:

This is true in all languages on all computers. Later we will examine /MOD and MOD which do give the remainder.

Defining a New Word

It's now time to write a small program in Forth. You can do this by defining a new word that is a combination of words we have already learned. Let's define and test a new word that takes the average of two numbers.

We will make use of two new words, : ( "colon"), and ; ( "semicolon") . These words start and end a typical Forth definition. Enter:

Congratulations. You have just written a Forth program. Let's look more closely at what just happened. The colon told Forth to add a new word to its list of words. This list is called the Forth dictionary. The name of the new word will be whatever name follows the colon. Any Forth words entered after the name will be compiled into the new word. This continues until the semicolon is reached which finishes the definition.

Let's test this word by entering:

Once a word has been defined, it can be used to define more words. Let's write a word that tests our word.. Enter:

Try combining some of the words you have learned into new Forth definitions of your choice. If you promise not to be overwhelmed, you can get a list of the words that are available for programming by entering:

Don't worry, only a small fraction of these will be used directly in your programs.

More Arithmetic

When you need to know the remainder of a divide operation. /MOD will return the remainder as well as the quotient. the word MOD will only return the remainder. Enter:

Two other handy words are MIN and MAX . They accept two numbers and return the MINimum or MAXimum value respectively. Try entering the following:

Some other useful words are:

ABS ( n -- abs(n) , absolute value of n ) 

NEGATE ( n -- -n , negate value, faster then -1 * ) 

LSHIFT ( n c -- n<<c , left shift of n ) 

RSHIFT ( n c -- n>>c , logical right shift of n ) 

ARSHIFT ( n c -- n>>c ) , arithmetic right shift of n ) 

ARSHIFT or LSHIFT can be used if you have to multiply quickly by a power of 2 . A right shift is like doing a divide by 2. This is often faster than doing a regular multiply or divide. Try entering:

Arithmetic Overflow

If you are having problems with your calculation overflowing the 32-bit precision of the stack, then you can use */ . This produces an intermediate result that is 64 bits long. Try the following three methods of doing the same calculation. Only the one using */ will yield the correct answer, 5197799.

Convert Algebraic Expressions to Forth

How do we express complex algebraic expressions in Forth? For example: 20 + (3 * 4)

To convert this to Forth you must order the operations in the order of evaluation. In Forth, therefore, this would look like:

Evaluation proceeds from left to right in Forth so there is no ambiguity. Compare the following algebraic expressions and their Forth equivalents: (Do not enter these!)

If any of these expressions puzzle you, try entering them one word at a time, while viewing the stack with .S .


Convert the following algebraic expressions to their equivalent Forth expressions. (Do not enter these because they are not Forth code!)

Use the words you have learned to write these new words:

Answers to the problems can be found at the end of this tutorial.

Character Input and Output

The numbers on top of the stack can represent anything. The top number might be how many blue whales are left on Earth or your weight in kilograms. It can also be an ASCII character. Try entering the following:

You should see the word "Hi" appear before the OK. The 72 is an ASCII 'H' and 105 is an 'i'. EMIT takes the number on the stack and outputs it as a character. If you want to find the ASCII value for any character, you can use the word ASCII . Enter:

Here is a complete ASCII chart.

Notice that the word CHAR is a bit unusual because its input comes not from the stack, but from the following text. In a stack diagram, we represent that by putting the input in angle brackets, <input>. Here is the stack diagram for CHAR.

CHAR ( <char> -- char , get ASCII value of a character ) 

Using EMIT to output character strings would be very tedious. Luckily there is a better way. Enter:

The word ." , pronounced "dot quote", will take everything up to the next quotation mark and print it to the screen. Make sure you leave a space after the first quotation mark. When you want to have text begin on a new line, you can issue a carriage return using the word CR . Enter:

You can emit a blank space with SPACE . A number of spaces can be output with SPACES . Enter:

For character input, Forth uses the word KEY which corresponds to the word EMIT for output. KEY waits for the user to press a key then leaves its value on the stack. Try the following.

[Note: On some computers, the input if buffered so you will need to hit the ENTER key after typing your character.]

EMIT ( char -- , output character ) 

KEY ( -- char , input character ) 

SPACE ( -- , output a space ) 

SPACES ( n -- , output n spaces ) 

CHAR ( <char> -- char , convert to ASCII ) 

CR ( -- , start new line , carriage return ) 

." ( -- , output " delimited text ) 

Compiling from Files

PForth can read read from ordinary text files so you can use any editor that you wish to write your programs.

Sample Program

Enter into your file, the following code.

Now save the file to disk.

The text following the \ character is treated as a comment. This would be a REM statement in BASIC or a /*---*/ in 'C'. The text in parentheses is also a comment.


"INCLUDE" in Forth means to compile from a file.

You can compile this file using the INCLUDE command. If you saved your file as WORK:SAMPLE, then compile it by entering:

Forth will compile your file and tell you how many bytes it has added to the dictionary. To test your word, enter:

Your two words, SQUARE and TEST.SQUARE are now in the Forth dictionary. We can now do something that is very unusual in a programming language. We can "uncompile" the code by telling Forth to FORGET it. Enter:

This removes SQUARE and everything that follows it, ie. TEST.SQUARE, from the dictionary. If you now try to execute TEST.SQUARE it won't be found.

Now let's make some changes to our file and reload it. Go back into the editor and make the following changes: (1) Change TEST.SQUARE to use 15 instead of 7 then (2) Add this line right before the definition of SQUARE:

Now Save your changes and go back to the Forth window.

You're probably wondering what the line starting with ANEW was for. ANEW is always used at the beginning of a file. It defines a special marker word in the dictionary before the code. The word typically has "TASK-" as a prefix followed by the name of the file. When you ReInclude a file, ANEW will automatically FORGET the old code starting after the ANEW statement. This allows you to Include a file over and over again without having to manually FORGET the first word. If the code was not forgotten, the dictionary would eventually fill up.

If you have a big project that needs lots of files, you can have a file that will load all the files you need. Sometimes you need some code to be loaded that may already be loaded. The word INCLUDE? will only load code if it isn't already in the dictionary. In this next example, I assume the file is on the volume WORK: and called SAMPLE. If not, please substitute the actual name. Enter:

Only the first INCLUDE? will result in the file being loaded.


Forth does not rely as heavily on the use of variables as other compiled languages. This is because values normally reside on the stack. There are situations, of course, where variables are required. To create a variable, use the word VARIABLE as follows:

This created a variable named MY-VAR . A space in memory is now reserved to hold its 32-bit value. The word VARIABLE is what's known as a "defining word" since it creates new words in the dictionary. Now enter:

The number you see is the address, or location, of the memory that was reserved for MY-VAR. To store data into memory you use the word ! , pronounced "store". It looks like an exclamation point, but to a Forth programmer it is the way to write 32-bit data to memory. To read the value contained in memory at a given address, use the Forth word @ , pronounced "fetch". Try entering the following:

This sets the variable MY-VAR to 513 , then reads the value back and prints it. The stack diagrams for these words follows:

@ ( address -- value , FETCH value FROM address in memory ) 

! ( value address -- , STORE value TO address in memory )

VARIABLE ( <name> -- , define a 4 byte memory storage location)

A handy word for checking the value of a variable is ? , pronounced "question". Try entering:

If ? wasn't defined, we could define it as:

Imagine you are writing a game and you want to keep track of the highest score. You could keep the highest score in a variable. When you reported a new score, you could check it aginst the highest score. Try entering this code in a file as described in the previous section:

Save the file to disk, then compile this code using the INCLUDE word. Test your word as follows:

The Forth words @ and ! work on 32-bit quantities. Some Forths are "16-bit" Forths. They fetch and store 16-bit quantities. Forth has some words that will work on 8 and 16-bit values. C@ and C! work characters which are usually for 8-bit bytes. The 'C' stands for "Character" since ASCII characters are 8-bit numbers. Use W@ and W! for 16-bit "Words."

Another useful word is +! , pronounced "plus store." It adds a value to a 32-bit value in memory. Try:

Forth also provides some other words that are similar to VARIABLE. Look in the glossary for VALUE and ARRAY. Also look at the section on "local variables" which are variables which only exist on the stack while a Forth word is executing.

A word of warning about fetching and storing to memory: You have now learned enough about Forth to be dangerous. The operation of a computer is based on having the right numbers in the right place in memory. You now know how to write new numbers to any place in memory. Since an address is just a number, you could, but shouldn't, enter:

The 253000 would be treated as an address and you would set that memory location to 73. I have no idea what will happen after that, maybe nothing. This would be like firing a rifle through the walls of your apartment building. You don't know who or what you are going to hit. Since you share memory with other programs including the operating system, you could easily cause the computer to behave strangely, even crash. Don't let this bother you too much, however. Crashing a computer, unlike crashing a car, does not hurt the computer. You just have to reboot. The worst that could happen is that if you crash while the computer is writing to a disk, you could lose a file. That's why we make backups. This same potential problem exists in any powerful language, not just Forth. This might be less likely in BASIC, however, because BASIC protects you from a lot of things, including the danger of writing powerful programs.

Another way to get into trouble is to do what's called an "odd address memory access." The 68000 processor arranges words and longwords, 16 and 32 bit numbers, on even addresses. If you do a @ or ! , or W@ or W! , to an odd address, the 68000 processor will take exception to this and try to abort.

Forth gives you some protection from this by trapping this exception and returning you to the OK prompt. If you really need to access data on an odd address, check out the words ODD@ and ODD! in the glossary. C@ and C! work fine on both odd and even addresses.


If you have a number that is appearing often in your program, we recommend that you define it as a "constant." Enter:

We just defined a word called MAX_CHARS that returns the value on the stack when it was defined. It cannot be changed unless you edit the program and recompile. Using CONSTANT can improve the readability of your programs and reduce some bugs. Imagine if you refer to the number 128 very often in your program, say 8 times. Then you decide to change this number to 256. If you globally change 128 to 256 you might change something you didn't intend to. If you change it by hand you might miss one, especially if your program occupies more than one file. Using CONSTANT will make it easy to change. The code that results is equally as fast and small as putting the numbers in directly. I recommend defining a constant for almost any number.

Logical Operators

These next two sections are concerned with decision making. This first section deals with answering questions like "Is this value too large?" or "Does the guess match the answer?". The answers to questions like these are either TRUE or FALSE. Forth uses a 0 to represent FALSE and a -1 to represent TRUE. TRUE and FALSE have been capitalized because they have been defined as Forth constants. Try entering:

You will notice that the first line printed a 0, or FALSE, and the second line a -1, or TRUE. The equal sign in Forth is used as a question, not a statement. It asks whether the top two items on the stack are equal. It does not set them equal. There are other questions that you can ask. Enter:

In California, the drinking age for alcohol is 21. You could write a simple word now to help bartenders. Enter:

The word FLAG in the stack diagram above refers to a logical value.

Forth provides special words for comparing a number to 0. They are 0= 0> and 0< . Using 0> is faster than calling 0 and > separately. Enter:

For more complex decisions, you can use the Boolean operators OR , AND , and NOT . OR returns a TRUE if either one or both of the top two stack items are true.

AND only returns a TRUE if both of them are true.

NOT reverses the value of the flag on the stack. Enter:

Logical operators can be combined.

Here are stack diagrams for some of these words. See the glossary for a more complete list.

< ( a b -- flag , flag is true if A is less than B )

> ( a b -- flag , flag is true if A is greater than B )

= ( a b -- flag , flag is true if A is equal to B )

0= ( a -- flag , true if a equals zero )

OR ( a b -- a||b , perform logical OR of bits in A and B )

AND ( a b -- a&b , perform logical AND of bits in A and B )

NOT ( flag -- opposite-flag , true if false, false if true )


1) Write a word called LOWERCASE? that returns TRUE if the number on top of the stack is an ASCII lowercase character. An ASCII 'a' is 97 . An ASCII 'z' is 122 . Test using the characters " A ` a q z { ".

Answers to the problems can be found at the end of this tutorial.

Conditionals - IF ELSE THEN CASE

You will now use the TRUE and FALSE flags you learned to generate in the last section. The "flow of control" words accept flags from the stack, and then possibly "branch" depending on the value. Enter the following code.

You can see that when a TRUE was on the stack, the first part got executed. If a FALSE was on the stack, then the first part was skipped, and the second part was executed. One thing you will find interesting is that if you enter:

the value on the stack will be treated as true. The flow of control words consider any value that does not equal zero to be TRUE.

The ELSE word is optional in the IF...THEN construct. Try the following:

Many Forths also support a CASE statement similar to switch() in 'C'. Enter:

See CASE in the glossary for more information.


1) Write a word called DEDUCT that subtracts a value from a variable containing your checking account balance. Assume the balance is in dollars. Print the balance. Print a warning if the balance is negative.

Answers to the problems can be found at the end of this tutorial.


Another useful pair of words is BEGIN...UNTIL . These are used to loop until a given condition is true. Try this:

This word will count down from N to zero.

If you know how many times you want a loop to execute, you can use the DO...LOOP construct. Enter:

This will print "ba" followed by four occurrences of "na". The ending value is placed on the stack before the beginning value. Be careful that you don't pass the values in reverse. Forth will go "the long way around" which could take awhile. The reason for this order is to make it easier to pass the loop count into a word on the stack. Consider the following word for doing character graphics. Enter:

If you want to access the loop counter you can use the word I . Here is a simple word that dumps numbers and their associated ASCII characters.

If you want to leave a DO LOOP before it finishes, you can use the word LEAVE. Enter:

Please consult the manual to learn about the following words +LOOP and RETURN . FIXME

Another useful looping construct is the BEGIN WHILE REPEAT loop. This allows you to make a test each time through the loop before you actually do something. The word WHILE will continue looping if the flag on the stack is True. Enter:


1) Rewrite SUM.OF.N using a DO LOOP.

2) Rewrite SUM.OF.N using BEGIN UNTIL.

3) For bonus points, write SUM.OF.N without using any looping or conditional construct!

Answers to the problems can be found at the end of this tutorial.

Text Input and Output

You learned earlier how to do single character I/O. This section concentrates on using strings of characters. You can embed a text string in your program using S". Note that you must follow the S" by one space. The text string is terminated by an ending " .Enter:

Note that TEST leaves two numbers on the stack. The first number is the address of the first character. The second number is the number of characters in the string. You can print the characters of the string as follows.

CHAR+ advances the address to the next character. You can print the entire string using TYPE.

It would be nice if we could simply use a single address to describe a string and not have to pass the number of characters around. 'C' does this by putting a zero at the end of the string to show when it ends. Forth has a different solution. A text string in Forth consists of a character count in the first byte, followed immediately by the characters themselves. This type of character string can be created using the Forth word C" , pronounced 'c quote'. Enter:

The number that was printed was the address of the start of the string. It should be a byte that contains the number of characters. Now enter:

You should see a 14 printed. Remember that C@ fetches one character/byte at the address on the stack. You can convert a counted Forth string to an address and count using COUNT.

The word COUNT extracts the number of characters and their starting address. COUNT will only work with strings of less than 256 characters, since 255 is the largest number that can be stored in the count byte. TYPE will, however, work with longer strings since the length is on the stack. Their stack diagrams follow:

CHAR+ ( address -- address' , add the size of one character )

COUNT ( $addr -- addr #bytes , extract string information ) 

TYPE ( addr #bytes -- , output characters at addr )

The $addr is the address of a count byte. The dollar sign is often used to mark words that relate to strings.

You can easily input a string using the word ACCEPT. (You may want to put these upcoming examples in a file since they are very handy.) The word ACCEPT receives characters from the keyboard and places them at any specified address. ACCEPT takes input characters until a maximum is reached or an end of line character is entered. ACCEPT returns the number of characters entered. You can write a word for entering text. Enter:

Enter a string which should then be echoed. You could use this in a program that writes form letters.

ACCEPT ( addr maxbytes -- numbytes , input text, save at address ) 

You can use your word INPUT$ to write a word that will read a number from the keyboard. Enter:

This word will return a single-precision number and a TRUE, or it will just return FALSE. The word NUMBER? returns a double precision number if the input string contains a valid number. Double precision numbers are 64-bit so we DROP the top 32 bits to get a single-precision 32 bit number.

Changing Numeric Base

For day-to-day life, the numbering system we use is decimal, or "base 10." That means each digit get multiplied by a power of 10. Thus a number like 527 is equal to (5*100 + 2*10 + 7*1). The use of 10 for the numeric base is a completely arbitrary decision. It no doubt has something to do with the fact that most people have 10 fingers (including thumbs). The Babylonians used base 60, which is where we got saddled with the concept of 60 minutes in an hour. Computer hardware uses base 2, or "binary". The computer number "1101" is equal to (1*8 + 1*4 + 0*2 + 1*1). If you add these up, you get 8+4+1=13 . The binary number "10" is (1*2 + 0*1), or 2. Likewise the numeric string "10" in any base N is N.

Forth makes it very easy to explore different numeric bases because it can work in any base. Try entering the following:

Another useful numeric base is hexadecimal. which is base 16. One problem with bases over 10 is that our normal numbering system only has digits 0 to 9. For hex numbers we use the letters A to F for the digits 10 to 15. Thus the hex number "3E7" is equal to (3*256 + 14*16 + 7*1). Try entering:

A variable called BASE is used to keep track of the current numeric base. The words HEX , DECIMAL , and BINARY work by changing this variable. You can change the base to anything you want. Try:

You are now in base 7 . When you fetched and printed the value of BASE, it said "10" because 7, in base 7, is "10".

PForth defines a word called .HEX that prints a number as hexadecimal regardless of the current base.

You could define a word like .HEX for any base. What is needed is a way to temporarily set the base while a number is printed, then restore it when we are through. Try the following word:

Answers to Problems

If your answer doesn't exactly match these but it works, don't fret. In Forth, there are usually many ways to the same thing.

Stack Manipulations


Logical Operators



Back to pForth Home Page