DIY Forth On Arduino

On a recent rainy afternoon, [Thanassis Tsiodras] decided to build his own Forth for the Arduino to relieve the boredom. One week of intense hacking later, he called it done and released his project as MiniForth on GitHub. [Thanassis] says he was inspired by our series of Forth articles from a few years back, and his goal was to build a Forth interpreter / compiler from scratch, put it into a Blue Pill microcontroller. That accomplished, he naturally decides to squeeze it into an Arduino Uno with only 2K of RAM.

Even if you are ambivalent about the Forth language, [Thanissis]’s project has some great ideas to check out. For example, he’s a big proponent of Makefile automation for repetitive tasks, and the project’s Makefile targets implements almost every task needed for development, building and testing his code.

Some development and testing tasks are easier to perform on the host computer. To that end, [Thanassis] tests his programs locally using the simavr simulator. The code is also portable, and he can compile it locally on the host and debug it using GDB along with Valgrind and AddressSanitizer to check for memory issues. He chose to write the program in C++ using only zero-cost abstractions, but found that compiling with the ArduinoSTL was too slow and used too much memory. No problem, [Thanassis] writes his own minimalist STL and implements several memory-saving hacks. As a final test, the Makefile can also execute a test suite of Forth commands, including a FizzBuzz algorithm, to check the resulting implementation.

Here’s a short video of MiniForth in action, blinking an LED on an UNO, and the video below the break shows each of the various Makefile tasks in operation. If you want to learn more, check out Elliot Williams’s Forth series which inspired [Thanassis] and this 2017 article discussing several different Forth implementations. Have you ever built your own compiler? Let us know in the comments below.

31 thoughts on “DIY Forth On Arduino

  1. How does this Forth implementation compare with Ting’s eForth as Arduino Sketch?
    Using the “39 steps” it has been tested a few times to implement and run

    For more background on Forth see

    and if you still want more, there is the Forth Bookshelf on amazon,

    Starting Forth by Leo Brodie,
    and Chuck Moore’s own book about the language he invented

    A lot less satellites would be out there without Forth.

    David Sands described Forth and why it is not known that well very nicely,
    saying roughly:

    Normally you use the language that you know or that fits best
    or that is standard in your company.

    when it gets difficult and interactivity is required for testing
    then you get out your Forth for solving this part of your problem.

    Not always used but where required and known by the few that need it.

    A very special tool in your toolbox

    Or you can interface Forth to your language via Sockpuppet

    and from May 2016

    And an overview on Forth INC.

  2. While studying engineering, a friend and I created a BASIC interpreter for the RCA 1802 CMOS processor. We sold it to a local RCA distributor and it became the best-selling BASIC interpreter in the area. Due to lack of development resources it was handwritten in assembler and hand-translated into machine code entered into the machine on a hex keypad. After suprisingly few bugs we got it working within a month.

    Years later, for my final Computer Science project, I created a BASIC interpreter written entirely in C. It used recursive descent to compile the code into a stack-based Polish Notation “machine language” that was executed by a super-optimised interpreter (also written in C).

    Since then I have created quite a few recursive descent parsers to solve irritating repetitive tasks, handle configuration files and in general interpreting complex data structures.

    On a side note regarding FORTH: if I remember correctly, SUN Microsystems used FORTH to embed drivers into their extension cards for their workstations and servers, making the cards immediately useable without having to install these drivers from tape or CD, which was quite efficient in the days before the Internet.

    1. SUN machines had OpenFirmware written in Forth so those Forth drivers could probably be used without even booting main OS. Same is OLPC laptop – you press escape at boot time and enter OpenFirmware Forth interpreter and can start coding, there is even fullscreen emacs editor built in.
      Actually I have OLPC 1 laptop somewhere with dead CAFE chip – so no camera and no builtin flash memory => it boots directly to Forth prompt with no boot device found. Reminds me old 8bit computers just with Forth instead of BASIC.

      1. OpenBoot and OpenFirmware on SUN and and Macs. On either one you can drop into the Forth during boot by holding a key combination. Modern Linux uses a version of OpenBoot. Device Tree is part of the SUN system.

        I recall the work at SUN was almost entirely done by Mitch Bradley and later by Firmworks (his business I think). He was in the Silicon Valley Forth Group and wrote some Forth books IIRC.

      2. Mitch Bradley did both. SUN OpenFirmware and OLPC firmware. Same on a Mac. Powermacs will boot into the Forth by holding some keys. Command+Option+o+f while booting.

    2. The IPS software by Karl Meinzer was used in some OSCAR satellites. It was Forth-like, too and ran on the COSMAC CPU.

      Considering the limited resources, IPS is/was one of the most sophisticated systems ever created and truely deserves the German engineering tag. ;)

      Really, IPS was special at the time. It was interactive, for example. Commands could be issued directly, like in a BASIC interpreter, without halting the whole thing. This was very important, because this made it possible to “fix things” in time. The OSCARs weren’t geo stationary, you know, so there was only a small window of time during each pass.

    1. If you do not know Forth obviously – it is the same as for any other language.
      Any badly written code is unreadable
      independant of language – especially if badly or not documented.
      It would be interesting to know how readable YOUR code is.

      1. The best languages are self documenting. If your code requires external documentation (i.e. besides readable code itself), you just asking for maintenance induced problems (i.e. the documentation gets out of sync with the code…and the lazy maintainer goes with the documentation…changes the code…then doesn’t get around to updating the documentation).

    2. As Forth is an excellent DSL (Domain Specific Language) development system, part of the so-called unreadability is just that it is so easy to create words in Forth, since it is word-centric, and each word is the expansion of the language of describing the problem, and coding the solution. Perhaps there is also a tendency to just code until it works, and not go back and comment what the interactive development (exploration) uncovered, what was found, and what was the solution. We need reminders to get us to go back to our completed code and comment it until it’s clear.

  3. The use of %10010 for binary and $3F20 for hex instead of changing mode using BINARY or HEX or DECIMAL to set the radix is rather un-Forthish. How does it affect the simplicity of the text parsing of the interpreter?

    1. Actually, while that practice may be “un-Forthish”, as you say, it is actually more modern programming practice, because it does not rely on STATE, but would be successful in the up-and-coming paradigms of Functional Programming, multithreaded, multiprocessing, multi-core, reentrancy, etc. What happens when one thread changes the Forth virtual machine to “Hex” mode, and another thread which thinks that the VM is in BINARY mode interprets a “101”? Instead of 5 it will get back 257 instead. In our increasingly multithreaded world, it’s better to have our code not relying upon the state of the VM for its interpretation. Therefore, we SHOULD use %10010 for binary and $3F20 for hex — it’s a better way in the new world of being interrupted anywhere and anytime, by any number of other threads, even copies of ourselves. Just try to debug THAT doing it the Old-Forth-State-ish way. Especially in a microcontroller, when interrupts may happen any time, this just makes sense.

  4. I architected a system of embedded controllers for OEMs, called – whimsically – SPLat.

    It was inspired partly by FORTH and partly by Hewlett Packard RPN calculators. It was developed in the 90’s when a microcontroller with 512 bytes of RAM and 4K of OTP EPROM was the norm, so it had to be very parsimonious in its resource usage.

    I wrote the IDE while my partner wrote the embedded interpreter. The language uses a virtual machine, interpreting byte codes at runtime. We built in a lot of “comfort” protections like contact debounce so it could be used by customers with little or no electronics knowledge. I am proud to say a number of people around the world developed successful businesses using the SPLat product, some of which are still going 25 years later.

    It’s not a compiler, more like an assembler. The initial IDE (SPLat/PC) includes an accurate simulator for debugging, because with the modest processors we had in 1993 interactive debugging with the real thing was out of the question (or at least not within our skillsets!). Being a runtime byte code interpreter the programs were very compact, but slow. Since I retired my successors have a team developing a brand new IDE that will support realtime interactive debugging.

    In subsequent iterations, as we got more powerful processors, we implemented a cooperative multitasking system that allows up to 32 concurrent tasks with close to zero code overhead. So for example a simple Pause instruction would pause the task in question with no effect on the other tasks. We also had instructions to for example wait for an input to come on with an optional timeout.

    And why HP calculators? My very first microprocessor project, in the early 70s, was an industrial instrument that needed to do a shedload of statistical calculations on raw data from a radiation detector. So I needed floating point maths. There was no such thing as libraries in those days, so I had to write my own FP maths routines. So I got the boss to buy an HP65 programmable calculator and tested my equations/calculations on that. Then I manually transferred them to the target processor (National Semi PACE). That’s why I made my own FP maths package emulate the HP65’s stack architecture, so the transfer would be easy. It’s also why I became interested in FORTH and 20 years later built my SPLat product around the same ideas.

  5. A COMPLETE FORTH AS ONE PAGE ON YOUR WALL! I have it here, was a nice little project.
    I just remembered the link to the page on
    OK, it is eForth, but a nice way to get started and to understand.
    Just a few lines of Assembler and the rest written in Forth.
    OK , it is a large page,
    but you can print it out on your printer and use a bit of sticky tape …
    Just 13 pages

  6. Please be aware,
    that here this person
    Peter Forth
    uses identity fraud – here and elsewhere,
    here uses the identity of a Professor Emeritus at a US univerity to look great.
    His real name – or is it fake as well? is Peter Forth
    More details you can find on Comp Lang Forth.
    After I had published my Forth Bookshelf, he had the idea to collect what is available for download.
    Fine for me.
    In a few cases he ripped the books I had published, placed them somewhere and then just linked to it
    to escape copyright fraud.
    Where we have found this, we asked to have it deleted – and it was done.

    A disgusting personality – but there we are.
    I assume he will soon send another post under a new fake name.

      1. I was not thanking you, I was thanking a great pdf collection of originals. You are the guy denounced for frauding S. Pelc ,Ting, Brodie , you call yourself co-author :) of those books. Tell the truth. Are you not selling PD documents from github ? Now “copyrighted” under a fake account on Amazon, damnifying the whole community. Me and other damnifieds already posted about this dishonest practice

        1. Is this again Peter Forth ?
          And his new FAKE Personality posting more lies?
          I had been expecting this.

          I did not look for a thank you. How did you get this idea?
          I just added the link for anybody interested, just read what I posted.

          I call myself editor/publisher in this context here if you can read.
          This is a fun project I started when promoting Forth.
          How can anybody have a fake account on amazon as you state.
          To get what you buy deliveried to a fake address – or not arriving at all?

          These books are not copyrighted by me as you interpret it wrongly.
          The version on amazon is protected as published.
          It is actually the other way around.
          And for your understanding as you seem to require it:
          I asked all of the authors – including Chuck, Ting and Forth INC for example, for permission to publish them on amazon as part of my Forth Bookshelf – which they agreed to.
          Forth INC. for example agreed to an eBook version, but did not want a print book version.
          Most of this material had been available on the net for free long before I started this activity,
          and is now available on the net, no change here – how did you actually get this wrong idea?

          And this Peter Forth that you must be, then based on my idea, started a link list to this material.
          No comments, no information added. This is the link you posted.
          Just have a look at the copyright issues this Peter Forth had with the Managing Director of MPE Forth on Comp Lang Forth.and in other places.
          For him everthing must be free.
          Does it mean this is how he does his shopping?

          Sorry for this post, but things should be put in context, and I hope this post stays and is not
          rightfully deleted as his last post here, where Peter Forth impersonated a Professor Dr. from a University.
          The posts here should be fun and additional information for others – not what he tries to start here.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.