SOME FORTH WORDS AND OPERATIONS Forth Built-in Words Word Meaning Initial Stack Ending Stack DUP Duplicates the top item on the stack x x x OVER Copies the second stack item x1 x2 x1 x2 x1 PICK Copies the nth stack item (n is zero-based) … n … xn SWAP Switches the top two stack items x1 x2 x2 x1 ROT Moves the third stack item to the top x1 x2 x3 x2 x3 x1 DROP Discards the top item on the stack x 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 / ok . 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 words. 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 + + ; ok 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: 7 CONSTANT FULL ok You can the use FULL anywhere you really mean 7, like this: FULL 2 0 MOTOR_SET ok Variables are even easier to define. Just use the word VARIABLE and supply a name: VARIABLE z ok Values are stored in variables using the ! word, pronounced "store": 12 z ! ok 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 Word Description < 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 0< Replaces the top item of the stack with true if it is less than zero; false otherwise 0= 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 ; ok -2 negative less than zero ok 2 negative ok 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 ; ok oneToTen 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.