Open side-bar Menu
 Embedded Software
Colin Walls
Colin Walls
Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor … More »

Go Forth!

April 18th, 2016 by Colin Walls

I was writing recently about my fondness for RPN [Reverse Polish Notation] and this reminded me of a programming language, designed specifically for real time and embedded applications, which has largely been forgotten: Forth. It is interesting to look at how Forth worked and the benefits it offered for embedded developers. I am not proposing that the language be revived and used for new developments, but I think there are valuable lessons to be learned.

My attention was originally draw to Forth when somebody told me that there was a programming language which facilitated the generation of code using less memory than an assembly language implementation of the same functionality. I did not believe them, but it hooked me in …

Forth is a Threaded Interpretive Language, which means that the code is compiled into an internal form, which is read by runtime software to give the effect of execution, much the same way that Java is normally implemented. It is possible to include native assembly language components as required.

A Forth program is comprised of a number of “words” – entities that are basically similar to functions in C. Each word consists of references to other words, which may be predefined or ones created by the developer. A word may also be an assembly language routine. In source form, a word is any sequence of non-space characters; when compiled, they become a list of addresses, which point to other words or assembly language.

A fundamental aspect of Forth is its use of a stack, which it uses for most data manipulation. Words tend to operate on the data on top of the stack, maybe removing it, and place results there.

Forth may be used interactively, where each line of code is executed when the RETURN key is pressed, or it may be compiled for create new words. For example, you could type this line:

3 dup + .

The interpreter would [probably] not find a word called “3“, so it would attempt to treat this as a constant and place the value on the stack. The word “dup” results in the duplication of the top stack item [so now there are two stack entries of the number 3]. It is probably obvious that “+” adds together the 2 top items on the stack and pushes the result [6] there. The last word, “.“, pops a value off the stack and displays it.

If this were an operation that you wanted to perform frequently, you could define a word of your own to do it, thus:

: show2times dup + . ;

The word “:” tells the interpreter to start compiling a word with the name that follows – in this case “show2times“. The “;” marks the end of the text to be compiled. Having done this, you can double and print 3 by typing:

3 show2times

From this short description and trivially simple example, I hope that you can see that Forth is conceptually quite different from C and other more familiar languages and also has a lot of flexibility and expressiveness. Many people criticize Forth for being a “write only language”, as it is very easy to write code which is impossible to read. Although I would agree that this is true, I promise that I could write some completely unintelligible [but valid] C, if required to do so.

Recently, I attended a seminar, where one of the speakers was discussing the history of their company over the last 25 years. He talked about their first project being done in Forth and commented that this was unlikely to be the way things would be done today. I wonder if Forth will ever see a revival. Or maybe there are people out there still using it …

Related posts:

One Response to “Go Forth!”

  1. juergen says:

    Well, Forth is being used now. And running now on the space station. Just have a look at and see some of the projects. There is an interesting Forth Compiler extension being introduced over the next 4 to 8 weeks. Just stay tuned.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Verific: SystemVerilog & VHDL Parsers
TrueCircuits: UltraPLL

Internet Business Systems © 2016 Internet Business Systems, Inc.
595 Millich Dr., Suite 216, Campbell, CA 95008
+1 (408)-337-6870 — Contact Us, or visit our other sites:
TechJobsCafe - Technical Jobs and Resumes EDACafe - Electronic Design Automation GISCafe - Geographical Information Services  MCADCafe - Mechanical Design and Engineering ShareCG - Share Computer Graphic (CG) Animation, 3D Art and 3D Models
  Privacy Policy