beanz Magazine

Forth

San José Public Library on Flickr

Learn why Arduino: Forth is actually #1!

In this article, we’re going to talk about an old but really interesting language, one that’s currently having a little bit of a revival on small embedded hardware like Arduino: Forth!

Forth is, as the title of this article implies, almost more of a machine than a language. It’s like a tiny computer with its own simple assembly language (see the past article for a brief introduction to assembly language), but instead of a complex model of memory and a ton of different instructions Forth just has one kind of memory—the stack—and a very small number of instructions. Unlike a processor, though, you can add new instructions yourself.

Why learn Forth? First, it’s one of those languages that will change your ideas around what a programming language can even be. The second is that it has a lot of applications in embedded systems. By embedded systems what we mean are machines that use things like Arduino microcontrollers or other low-power/low-speed/low-memory chips that aren’t like what you’d put inside a computer. This includes, well, basically everything around you that uses electricity: from your keyboard to your router.

Where does Forth come in? Forth is an incredibly simple language to implement. It simultaneously has an interpreter that allows for interactive development, and also is very tiny: it can fit in just a few kb of storage! Forth interpreters are also really easy to implement, which means that it’s easy to make a new one for different microcontrollers or small chips as they come out.

As an example of what I mean, consider the Collapse OS project. Collapse OS is a neat “what if?” exercise for how—in a post-apocalyptic story—someone could go from scavenged electronics parts to having proper computers. It’s an operating system written in Forth that could run on forty-plus year old computer chips. The Collapse idea is that if you have a copy of it and an old piece of hardware, maybe a game console from the 1980s, you’d have everything you need to build more computers and compile Collapse OS to run on them.

So how does Forth work and act? The easiest thing to do is get the Gforth interpreter which runs on every operating system. You can also, though, get started with the interpreter embedded into the Easy Forth tutorial—a tutorial I highly recommend if reading this article has whet your appetite at all!

Once you have a Forth interpreter you should be looking at a simple little command line that looks like this

Here, you type commands to the Forth interpreter/machine. In Forth terms, these commands are called words. Each word is separated by a space and you can put multiple words on a line.

Every word does something to the memory of the Forth machine, or the stack. If you haven’t seen a stack before, picture something like a stack of heavy books: you can either put something on top of it or take things off the top. You can’t grab something further down the stack until you’ve taken off all the things above it.

So basic words like numbers just put a number on top of the stack. If I type
10 20 30
and then hit enter the stack is going to have 30 on top, 20 just beneath it, and 10 beneath that.

Gosh it’d be nice to actually see what’s on the stack, right? We can do that by typing . and hitting enter, which spits out the top thing on the stack. Oh, well, actually the problem is that it has to take the item off the stack first, so you can’t use it in the future. Awkward.

There’s a built in word to print without consuming, but for fun let’s pretend it doesn’t exist and we can define our own word instead. Try typing
: pr dup . ;

This defines a new word called pr that duplicates the top thing on the stack—so you have one to spare—and then uses . to lift it off the stack and print it.
So now if we try
10 10 + pr

This does what we want! An important conceptual point is that Forth doesn’t have functions as such. Instead, a word can use arguments off the stack and with the simple syntax of Forth it looks like a funny kind of function application, where instead of writing “10 + 10” you write “10 10 +”. Unlike most programming languages, you’re not giving a function called “+” two numbers to compute with. You’re just executing the word “+” and it happens to use the top two elements of the stack.

Forth isn’t just a weird language for systems programming; people have used it for all sorts of things like making video games or webservers.

I wouldn’t say Forth is a language that you’re going to start using as your main programming language right away. It’s a small language with a small community, so you’ll be largely working on your own, which is hard. On the other hand, everything you learn from Forth will make you think differently about programming which—in turn—will make you a better programmer. If you want to read more about how Forth is weird and neat, there’s the classic book Thinking Forth that’s now available online for free here!

 

Learn More

Using Forth in embedded systems

https://hackaday.com/2017/01/27/forth-the-hackers-language/

The Collapse OS project

https://collapseos.org/

Gforth interpreter

https://www.gnu.org/software/gforth/

Easyforth tutorial

https://skilldrick.github.io/easyforth/

Ramen game engine in Forth

https://github.com/RogerLevy/RamenEngine

Designing webservers in Forth

https://bernd-paysan.de/httpd-en.html

What is Forth

https://www.forth.com/forth/

Overview of Forth

https://www.whoishostingthis.com/resources/forth-programming/

Forth programming language

http://encyclopedia.kids.net.au/page/fo/Forth_programming_language

Forth Programming Language

https://academickids.com/encyclopedia/index.php/Forth_programming_language

Turtle Logo in Forth

http://mathscitech.org/articles/turtle-logo-forth

Also In The October 2020 Issue

Meet one of modern mathematics greatest contributors, Leonhard Euler!

How can we apply the Konigsberg Bridge problem to real life and even make a game out of it? With Sprouts of course!

Wi-Fi is a weird word for sure! But what does it really mean?

Learn how Eugene Kaspersky turned his antivirus making hobby into so much more!

What in the world is a video redundancy? Find out that and more in this crash course on encoding!

Links from the bottom of all the October 2020 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for October 2020.

One of the first female programmers, Grace Hopper also worked as a mathematician and had an unusual career for women in the 1900s.

Need help proving your sibling has been sneaking into your room? This may be the perfect craft for you!

Bogons are not an evil race of aliens. But they do shine a light on one part of the internet little known outside of a few security technologists.