compile mode and the word “;” (semicolon) ends compile
mode. Immediate Forth words execute even in compile
mode, whereas non-immediate Forth words have their
addresses compiled into the word being defined.
Many things make Forth unique, but two things need
to be specifically pointed out.
Forth is a stack-oriented language, meaning that all
arguments to and values returned from Forth words are
unnamed, untyped, and generally located on a stack.
Forth uses Reverse Polish Notation (RPN) for expression
evaluation like many HP calculators.
Let’s use the Forth word DUP as an example. DUP
duplicates the value currently on the top of the data stack.
DUP takes its input value from the stack and pushes two
copies of that value back onto the stack.
Using RPN for expression evaluation takes getting used
to when you’re used to normal algebraic or infix evaluation.
The algebraic expression 2 + 5 3 evaluates as 2 + ( 5 3),
whereas the RPN evaluation becomes 2 5 3 +. Expression
conversion to RPN is easy if you keep the following in mind:
• Identifiers are used in the same order.
• Operators are used in the same order.
• Operators follow identifiers.
Forth leaves all data typing issues to the programmer.
Data typing is usually handled by having Forth words
defined for each data type. For example, the standard
addition operator “+” expects its two arguments to be
integers and integers only. If floating-point addition is
required in an application, for example, a special operator
like “F+” might be defined.
Forth words use a stack diagram for documentation.
DUP’s stack diagram is as follows:
( n — n n )
The value on the left after the paren is what is on the
data stack before DUP is executed. In this case, a number n
and the two items before the closing paren are a picture of
the stack after execution, indicating two copies of n. It is
important to understand that on both sides of the “—” the
top of the stack is always to the extreme right.
Versions of Forth are available for almost all
microprocessors and microcontrollers. In the past Forth, was
generally one of the first programming languages
implemented for new microprocessors because it is easy to
implement. Code up a handful of Forth primitives for I/O
for the new microprocessor and the remainder of the
language written in Forth just runs.
I thought it might be useful to compare and contrast
these two development environments to gauge their
strengths and weaknesses. For those that don’t know,
Wiring is the name of the computer language that runs on
the Arduino hardware.
The biggest difference between Wiring and Forth is in
how software is developed. With Wiring, code is written on
an external computer; compiled and linked into an
executable on the external computer; and when considered
error free, downloaded to the Arduino hardware for
execution and debugging. The serial monitor built into the
IDE (Integrated Development Environment), along with print
statements in the code, are the means of debugging the
code. When an error is detected, the offending code is
found and fixed, then the above process is repeated. In a
large program, this process is repeated over and over.
Hosting the software development tools on the Arduino
itself as a means of shortening the turn-around time is
Forth is different in that all required software
development tools are built directly into the language itself
and are available all of the time on the target system. You
still need an external computer to interact with Forth during
program development, but its only function is as a serial
terminal and mass storage of the Forth source code. This
difference may seem subtle, but it is important.
In Wiring, when you identify an error, you correct it
and then re-Flash the complete software image back to the
target hardware. With Forth, you just recode the Forth
word in error, and re-Flash it — a much faster alternative.
With Arduino code, you write a program using the key
words and functionality provided by the Wiring language
and supporting libraries. A program in Forth is actually an
extension of the language itself. Much of the functionality
of the Wiring language is hidden from the user, whereas in
Forth nothing is inaccessible. Don’t like how a language
feature like a loop structure works in Wiring? You are out
of luck. In Forth, you can write the perfect loop structure
and it becomes part of the language.
Don’t get me wrong, the Arduino/Wiring combination
is a winner and I use it regularly as frequent readers of
Nuts & Volts will attest. It has introduced many people to
54 SERVO 02.2014