Arm Community
Arm Community
  • Site
  • User
  • Site
  • Search
  • User
Arm Community blogs
Arm Community blogs
Embedded and Microcontrollers blog 8th - a gentle introduction to a modern Forth
  • Blogs
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
More blogs in Arm Community blogs
  • AI blog

  • Announcements

  • Architectures and Processors blog

  • Automotive blog

  • Embedded and Microcontrollers blog

  • Internet of Things (IoT) blog

  • Laptops and Desktops blog

  • Mobile, Graphics, and Gaming blog

  • Operating Systems blog

  • Servers and Cloud Computing blog

  • SoC Design and Simulation blog

  • Tools, Software and IDEs blog

Tags
  • Raspberry Pi
  • Android
  • apple
  • Embedded Linux
  • Embedded
  • Programming Languages
  • cryptography
  • Windows
  • Linux
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

8th - a gentle introduction to a modern Forth

Ron Aaron
Ron Aaron
September 27, 2017

Introduction

8th is a secure, cross-platform programming language based on Forth which lets you concentrate on your application’s logic instead of worrying about differences between platforms. It lets you write your code once, and simultaneously produce applications running on multiple platforms. Its built-in encryption helps protect your application from hackers. Its interactive nature makes debugging and testing your code much easier.

8th platforms

As of this writing it supports 32-bit and 64-bit variants of:

  • Windows, macOS and Linux for desktop or server systems
  • Android (32-bit Arm only) and iOS for mobile systems
  • Raspberry Pi (Raspbian etc) for embedded Linux Arm-based systems.  

Besides all that, 8th is simply fun to use!

Before you can use it, you need to get a copy. Follow the installation instructions (unzip the file you've received) and get comfortable in your OS'es console...

Forths and 8ths

What's Forth?

Originally, the Forth language was developed in the early 1970s to control telescope hardware in an observatory.  It has since found its way into many embedded applications (spacecraft, robotics, industrial control systems, automotive and many more). Its primary advantages historically have been extremely small size (around 2K code+data for a capable interpreter/compiler), interactive nature, and very good performance. 

The simplicity and elegance of the Forth REPL has been one of the language's main draws, being extremely easy to implement and extremely useful for testing and debugging. A typical implementation will simply parse whitespace-delimited input (from the keyboard, a file, a socket, etc.) and look up each "word" so parsed in its dictionary of known words.  If the word is found, the code it represents it is executed. Otherwise, an attempt is made to convert the word to a number.  If that fails, the REPL may complain.

Numbers or other data are placed on a stack, and  processed in RPN (or "postfix") manner.  So:

10 2 / 

results in 5, because the 10 was pushed first, then the 2 was pushed, and the "/" word popped those items off, divided 10 by 2, and pushed the resultant 5 back onto the stack.

There is no fixed syntax.  nstead, any whitespace-delimited bit of text is treated as a possible "word" to be looked-up and executed.

New words are added to the dictionary using the somewhat cryptic word ":" and terminated with the equally cryptic ";".  For example:

: add2 2 + ;

This sequence creates a new "word" named add2 which adds 2 to the number which was on the top of the stack (TOS, hereafter) when "add2" was invoked. The colon (":") word begins compilation of whatever words are found until the terminating semi-colon (";"), which is why these are sometimes called "colon-defs". The effect is to create a new word in the dictionary which executes the sequence of instructions desired.

8th is a "modern Forth"

8th differs from more traditional Forths in a number of ways. First of all, it is strongly typed and has a plethora of useful types (dynamic strings, arrays, maps, queues and more). Traditional Forths have "cells", which are numbers (typically sized as appropriate for the CPU) which may be treated as addresses (e.g. strings for instance) or just numbers - the programmer has to manage all the complexity and bear in mind what each cell represents.

8th's strings and arrays grow as needed, much as they do in many other high-level languages. Memory is allocated or deallocated as needed, behind the scenes - the programmer cannot allocate memory directly. In traditional Forths, strings are fixed in size and manipulating them properly can be complex. In 8th you can, for example:

"Hello, " "world" + . cr

This will print "Hello, world".  The "+" concatenates the two strings and the "." prints whatever is in TOS. "cr" prints out a "carriage-return" (actually it prints the end-of-line sequence appropriate for the platform on which it's running).

8th is designed around the principles of least surprise and ensuring security. Once you grok RPN and the Forthish way of doing things, you'll find it strives to be consistent in how the built-in words behave. When you wish to deploy your application, you can create one which is signed and encrypted such that any modification of the app renders it incapable of being run (rather than being modified in a possibly harmful way).

What about embedded?

Glad you asked!  As mentioned in the introduction, 8th currently supports Raspberry Pi and other embedded Linux Arm-based systems. On RPI 8th gives you easy access to GPIO and I2C for example:

18 false hw:gpio \ tell the system you want pin 18, not read-only

  true hw:gpio!  \ write '1' or 'true' to the GPIO pin we opened

  0.1 sleep      \ delay a tenth of a second

  hw:gpio@ . cr  \ read the pin and print what was read

Some of the benefits for embedded work with 8th include:

  • Write on-board programs using 8th,  but also write any related programs (desktop/mobile/server) using 8th
  • Easy access to GPIO, I2C, external libraries, and more
  • Secure, encrypted deploy package means your IP is more secure
  • Built-in support libraries, no need for complex deployment
  • Higher-level than C/C++/Java, easier and faster to write complex applications
  • Simple syntax doesn’t get in the way of the developer
  • Good performance

Coming up...

That's all for this introduction. I hope you're intrigued by 8th and will read the links posted and perhaps browse or even join the 8th forum as well.  

In my next blog post, I'll start showing how to use 8th to your advantage. You can read it by clicking on the link below:

8th: Grokking the REPL

8th dev

Anonymous
Parents
  • Ron Aaron
    Ron Aaron over 6 years ago

    8th's first AGL (Automotive Grade Linux) version released today.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Comment
  • Ron Aaron
    Ron Aaron over 6 years ago

    8th's first AGL (Automotive Grade Linux) version released today.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Children
No Data
Embedded and Microcontrollers blog
  • Adapting Kubernetes for high-performance IoT Edge deployments

    Alexandre Peixoto Ferreira
    Alexandre Peixoto Ferreira
    In this blog post, we address heterogeneity in IoT edge deployments using Kubernetes.
    • August 21, 2024
  • Evolving Edge Computing and Harnessing Heterogeneity

    Alexandre Peixoto Ferreira
    Alexandre Peixoto Ferreira
    This blog post identifies heterogeneity as an opportunity to create better edge computing systems.
    • August 21, 2024
  • Demonstrating a Hybrid Runtime for Containerized Applications in High-Performance IoT Edge

    Chris Adeniyi-Jones
    Chris Adeniyi-Jones
    In this blog post, we show how a hybrid runtime and k3s can be used to deploy an application onto an edge platform that includes an embedded processor.
    • August 21, 2024