Forth is an untyped programming language that heavily relies on the stack: data will be stored onto the stack before they are operated upon. Forth is the perfect programming language for hardware engineers:

  1. Forth is a very good alternative to Assembly and C. In fact, Forth allows programmers to control stack, memory addresses and other low level elements in a simple way.
  2. Forth is easy to debug as it can run interactively and it does not require the programmer to master a debugger.
  3. Developing Forth applications straight on the target machine is easy which speed the development process up.
  4. Forth comes with its own decompiler which is able to show the source code of any previously implemented word (the Forth equivalent for function). Words that are implemented in Forth assembler will show this code when decompiled.
  5. Forth is still one of the best tools available to those who need to reverse engineer undocumented devices.
  6. Forth can enjoy a very active community, it is very well maintained and it is still used on many critical projects such as satellite tracking applications, industrial automation, avionic and space applications and much more!

Retrieving and setting up Gforth

Although Forth source code can be stored into files in order to be compiled and executed, the examples shown in this articles are to be run interactively. All examples were tested using the GNU implementation of Forth, named Gforth which can be retrieved from the Git repository executing the following:

$> git clone https://git.savannah.gnu.org/git/gforth/

The user should read the ‘INSTALL.md’ file in order to know how to build and install GForth. At this present stage, the following are to be executed on Linux to get the Forth environment up and running:

$> source ./install-deps.sh
$> ./BUILD-FROM-SCRATCH
# The following is not mandatory
$> sudo make install

Learning how to use Gforth interactively

Start the environment by running ‘gforth’ and exit the program by typing ‘bye’ or pressing the ‘Ctrl+d’ key combination. Typing numbers will place them onto the stack:

6 7 8  ok 3

Type ‘.s’ to print the content of the stack and its size (computer output is appended right after ‘.s’):

.s <3> 6 7 8  ok 3

The line above states that the stack contains three elements: 6, 7 and 8 having the rightmost element on top of the stack. As this language is a stack based one, arithmetic operators always apply to the top two stack items. Therefore, given the numbers 10, 2 and 3, to get the number 6 as the result of ‘2 * 3’ the following sequence of command is to be issued:

10 2 3
*
.s <2> 10 6  ok 2

The two most important facts are the following:

  1. Multiplication ignored the leftmost element, as expected.
  2. After the multiplication the stack only contained two values: the ignored one and the final result.

How to manipulate the stack in Forth

As operators manipulate the top of the stack, this might need to be rearranged every now and then by using any of the following stack oriented keywords:

  • drop (executes a pop)
    • 10 20 30 → 10 20
  • dup (duplicates top element of the stack)
    • 10 20 30 → 10 20 30 30
  • over (copies the second element to the top)
    • 10 20 30 → 10 20 30 20
  • swap (swaps the top and the second element)
    • 10 20 30 → 10 30 20
  • rot (swaps the third and first element)
    • 10 20 30 → 20 30 10
  • nip (removes the second element)
    • 10 20 30 → 10 30
  • tuck (copies the top of the stack before the second element)
    • 10 20 30 → 10 30 20 30
  • many more…

Defining and using variables in Forth

Even though most of the work can be done on the stack without having to use variables, they can still be defined as they may be useful to store values that have to be accessed by multiple words. The following line defines a variable: variable price

The next line assign a value to it which is stored at the memory location pointed by the variable:

15000 price !

The next line copies the variable onto the top of the stack:

price @

Therefore, the final result of the following will be 27000:

variable price
15000 price !
12000
price @
+
.s <1> 27000 ok 1

When to use Forth

As stack based language, Forth can:

  1. Implement words that can be called anywhere in the program as each function will make sure to save any important parameter before leaving the stack.
  2. Implement re-entrant words as they only rely on the stack.
  3. Simplify development as programmers will only need to focus on the stack rather than having to check on a potentially large number of variables and their visibility.

Watch this tutorial on YouTube

YouTubeThumbnail

Previous Post Next Post