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 …

4 Responses to “Go Forth!”

  1. Avatar juergen says:

    Well, Forth is being used now. And running now on the space station. Just have a look at http://www.mpeforth.com 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.

  2. Avatar juergen says:

    Forth and Forth on BBC MicroBit – Have a look at http://www.forth-ev.de. There is a lot information or you can just get the VFXTESTAPP.exe from there, start with the kids who learnt the Forth basics in 2 hours at http://www.forth-ev.de/index.php?page=2 and all the relevant data is at https://wiki.forth-ev.de/doku.php/en:projects:a-start-with-forth:start. The little application there can be used with PC, Tablet, Mobile . The MicroBit can run Mecrisp Forth now as well, have a look at https://wiki.forth-ev.de/doku.php/en:projects:microbit:start

  3. Avatar juergen says:

    You want to know a bit more about Forth – now problem.
    Read some of the eBooks while on the bus or the train.
    https://www.amazon.co.uk/Juergen-Pintaske/e/B00N8HVEZM
    Early Years – how it started
    Programming a problem Oriented Language – Looking inside
    Forth Lite Tutorial
    Dr. Ting’s eForth
    Starting Forth by Leo Brodie – the classic
    Stephen Pelc’s FORTH PROGRAMMING
    and more

Logged in as . Log out »




© 2024 Internet Business Systems, Inc.
670 Aberdeen Way, Milpitas, CA 95035
+1 (408) 882-6554 — 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 PolicyAdvertise