Forth Built-in Words

                                            Initial Stack
                                                            Ending Stack
           Duplicates the top item on the stack

                                                                       x x

           Copies the second stack item
                                                    x1 x2

                                                                 x1 x2 x1

           Copies the nth stack item (n is zero-based)


           Switches the top two stack items
                                                    x1 x2

                                                                    x2 x1

           Moves the third stack item to the top
                                                 x1 x2 x3

                                                                 x2 x3 x1

           Discards the top item on the stack

Forth also supports mathematical operators, which operate on the top two 
items of the stack and leave their result at the top of the stack.

Mathematical operators: +, -, *, /, MOD, MIN, MAX, others
Example, you can divide two numbers like this:

    84 2 /
    42 ok

Bitwise operators are also defined: AND, OR, XOR.

You can change the number base you're working in using the HEX and DECIMAL 

BASE is a variable that contains the current number base. To print out the 
current number base, for example, do this:

    BASE @ .
     10 ok

If you want to work in a number system other than HEX or DECIMAL, you can use 
BASE ! to store any base number. The following example shows two ways you can 
use Forth to convert numbers between different bases:

    HEX 20 DECIMAL . (20 in base 16 is 32 in base 10)
     32 ok
    8 BASE ! 20 DECIMAL . (20 in base 8 is 16 in base 10)
     16 ok

The open parenthesis ( is used to indicate a comment. The pbFORTH interpreter 
simply ignores the rest of the line after it sees the (.

Defining words

Writing programs in Forth is a matter of defining your own words in the 
dictionary. Word definitions begin with a colon and end with a semicolon.

A simple example:

    : threeTimes DUP DUP + + ;

The colon tells the Forth interpreter that the words that follow define a new 
word for the dictionary and should be stored to run later. The new word needs 
a name, threeTimes in this example, which is supplied immediately after the 
colon. Subsequent words will be executed when the new word is executed. The 
semicolon tells the interpreter that the new word definition has ended.

threeTimes duplicates the top item on the stack twice, so there are three 
copies of it, then adds them all together. Now that you've defined a new word, 
you can use it like any other Forth word:

    5 threeTimes .
    15 ok

Constants and Variables

To define a constant, push its value on the stack. Then use the word CONSTANT 
and supply a name. like this:


You can the use FULL anywhere you really mean 7, like this:


Variables are even easier to define. Just use the word VARIABLE and supply a 


Values are stored in variables using the ! word, pronounced "store":

    12 z !

The value of a variable can be retrieved and placed on the stack with the @ word:

    z @ .
    12 ok

Conditionals and Loops

Forth supports a standard IF statement, though the order is switched around from 
what you might be used to. The IF word comes in two different varieties:

     condition IF body THEN

The IF word looks at the condition on the stack. If the condition is true, the 
body between IF and THEN is executed. Table 6-3 lists some of the mathematical 
condition words that are available.

Mathematical Condition Words
           Compares the top two items on the stack; replaces them with true 
if the second item is less than the top item, false otherwise

           Compares the top two items on the stack; replaces them with true 
if they are equal, false otherwise

           Compares the top two items on the stack; replaces them with true 
if the second item is greater than the top item, false otherwise

           Replaces the top item of the stack with true if it is less than 
zero; false otherwise

           Replaces the top item of the stack with true if it is equal to 
zero; false otherwise

The following example prints out a message if the top item on the stack is 
less than 0:

    : negative 0< IF ." less than zero " THEN ;
    -2 negative
     less than zero ok
    2 negative

The 0< word examines the top value on the stack. The result of this comparison 
is examined by the IF word. If it is true, the body of the IF is executed. 
The . word tells pbFORTH to print text to the console, up until the  word. 
Finally, THEN marks the end of the body of the IF.

There's also a slightly more complicated IF word:

     condition IF trueBody ELSE falseBody THEN

Forth also includes some simple loops, including:

     limit start DO words LOOP

This loop performs the given words limit - start times. Internally, a loop 
index is given the value start. Each time through the loop, the index is 
increased by one. When it is equal to limit, the loop ends. You can put the 
value of the loop index on the stack with the I word. For example, the following 
shows how to define a word that prints the numbers from 1 to 10.

    : oneToTen 11 1 DO I . LOOP ;
     1 2 3 4 5 6 7 8 9 10 ok

     limit start DO words delta +LOOP

This variation on the basic DO LOOP adds the given delta value to the loop index 
instead of always adding one. You can use this type of loop to count backwards 
by supplying a negative delta.

     BEGIN body UNTIL

This loop performs its body until a false condition is left on the stack

     BEGIN body AGAIN

This variation on BEGIN UNTIL loops forever, executing its body each time 
through the loop.