Est. 1970 Intermediate

Forth

A stack-based, extensible programming language known for its simplicity, interactivity, and use in embedded systems, firmware, and space exploration.

Created by Charles H. Moore

Paradigm Stack-based, Procedural, Concatenative, Extensible
Typing Typeless (words operate on stack)
First Appeared 1970
Latest Version Gforth 0.7.3 (2014), ANS Forth (1994)

Forth is a unique programming language that challenges conventional programming paradigms. Its stack-based architecture, extreme simplicity, and interactive development style have made it a favorite for embedded systems, real-time applications, and situations where resources are limited.

History & Origins

In the late 1960s, Charles H. “Chuck” Moore was frustrated with the overhead of existing programming languages. While working at Mohasco Industries in Amsterdam, New York, he began developing a personal programming system on an IBM 1130 with just 8K of memory.

Moore called it “Fourth” because he considered it a fourth-generation language. However, the IBM 1130 only allowed five-character identifiers, so it became FORTH. The name stuck, though it’s now typically written as “Forth.”

The NRAO Years

In 1970, Moore brought Forth to the National Radio Astronomy Observatory (NRAO), where he used it to control radio telescopes. Elizabeth Rather learned the language and became its first user besides Moore. Together, they founded FORTH, Inc. in 1971, the first commercial Forth vendor.

What Made Forth Different

Forth was revolutionary in several ways:

  1. Interactive development - Write and test code immediately, no compilation cycle
  2. Extensibility - The language is built from the same primitives users employ
  3. Minimal overhead - The entire system could fit in kilobytes, not megabytes
  4. Direct hardware access - Perfect for embedded systems and real-time control

Why Forth Still Matters

1. The Stack-Based Model

Forth uses a data stack for passing parameters between words (functions). This eliminates named parameters and creates a very different mental model:

1
2
3
\ Traditional: add(2, 3)
\ Forth:
2 3 +    \ Push 2, push 3, add them

This “reverse Polish” notation (like HP calculators) seems strange at first but becomes natural. Each word takes its input from the stack and leaves its output there.

2. Extensibility

In Forth, you define new words using existing words. There’s no distinction between built-in commands and user definitions:

1
2
3
4
: SQUARE ( n -- n² )  DUP * ;
: CUBE   ( n -- n³ )  DUP SQUARE * ;

5 CUBE .  \ prints 125

The entire language, including control structures, is built this way.

3. Interactive Development

Forth systems are interactive by nature. You type a word, and it executes immediately. This encourages incremental development and testing:

ok 2 3 + .
5 ok
ok : GREET ." Hello!" ;
ok GREET
Hello! ok

4. Minimal Resource Requirements

A complete Forth system can run in less than 8KB of memory. This made it invaluable for early microcomputers and continues to make it relevant for resource-constrained embedded systems.

Forth Dialects and Implementations

ImplementationDescriptionStatus
GforthGNU Forth, reference ANS implementationVery active
SwiftForthCommercial, FORTH Inc.Active
VFX ForthHigh-performance commercialActive
pForthPortable Forth in CMaintained
MecrispForth for ARM Cortex-MActive

Modern Forth Implementations

Gforth (GNU Forth)

The most widely used open-source Forth:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install on macOS
brew install gforth

# Install on Ubuntu/Debian
sudo apt install gforth

# Run interactively
gforth

# Run a file
gforth program.fth -e bye

Online Interpreters

Several web-based Forth interpreters exist for quick experimentation without installation.

Key Concepts

The Stack

Forth uses two stacks:

  • Data stack - for parameters and results
  • Return stack - for return addresses (and temporary storage)
1
2
3
1 2 3     \ Stack: 1 2 3 (3 on top)
+         \ Stack: 1 5
*         \ Stack: 5

Words

Everything in Forth is a “word” - a named operation. Defining new words extends the language:

1
2
3
: DOUBLE 2 * ;     \ Multiply by 2
: TRIPLE 3 * ;     \ Multiply by 3
5 DOUBLE TRIPLE .  \ prints 30

Stack Notation

Forth uses a standard notation for stack effects:

1
2
3
4
\ ( before -- after )
: SWAP ( a b -- b a ) ... ;
: DUP  ( n -- n n ) ... ;
: DROP ( n -- ) ... ;

Language Features

Defining Words

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
\ Variables
VARIABLE counter
0 counter !        \ Store 0
counter @ .        \ Fetch and print

\ Constants
42 CONSTANT answer
answer .           \ prints 42

\ Words with local variables (some Forths)
: QUADRATIC { a b c x -- result }
    a x * x * b x * + c + ;

Control Structures

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
\ IF-ELSE-THEN
: SIGN ( n -- )
    DUP 0 > IF ." positive"
    ELSE DUP 0 < IF ." negative"
    ELSE ." zero"
    THEN THEN DROP ;

\ Loops
: COUNT-DOWN ( n -- )
    BEGIN DUP 0 > WHILE
        DUP . 1-
    REPEAT DROP ;

: SQUARES ( n -- )
    1 DO I I * . LOOP ;

String Handling

1
2
3
4
5
\ Print a string
." Hello, World!"

\ String operations vary by Forth dialect
S" Hello" TYPE

Getting Started

Forth source files typically use .fs, .fth, or .4th extensions. The basic structure is simply definitions followed by execution:

1
2
3
4
5
\ Define words
: GREET ." Hello from Forth!" CR ;

\ Execute
GREET

Continue to the Hello World tutorial to write your first Forth program.

Timeline

1968
Charles Moore begins developing Forth at Mohasco Industries
1970
Forth used at National Radio Astronomy Observatory (NRAO)
1971
FORTH, Inc. founded by Moore and Elizabeth Rather
1978
First Forth on Intel 8086 chip
1983
Forth-83 standard published
1984
MacFORTH - first development system for Macintosh 128K
1994
ANS Forth standard (ANSI X3.215-1994) published
2012
Forth-2012 standard extends ANS Forth
2014
Philae spacecraft lands on comet using Forth-controlled systems

Notable Uses & Legacy

Space Exploration

Forth powered systems on the Philae lander (Rosetta mission) that landed on a comet in 2014, and has been used in numerous NASA missions due to its reliability and small footprint.

Open Firmware

The Open Firmware boot standard used by Apple (PowerPC Macs), Sun Microsystems, and IBM uses Forth as its user interface and scripting language.

Embedded Systems

Forth's minimal memory requirements and interactive development make it ideal for microcontrollers, industrial automation, and real-time systems.

PostScript

Adobe's PostScript page description language was heavily influenced by Forth's stack-based architecture and extensibility model.

GreenArrays Chips

Charles Moore designed the GreenArrays GA144 - a chip containing 144 independent Forth processors, demonstrating Forth's suitability for parallel computing.

Language Influence

Influenced By

Lisp ALGOL

Influenced

PostScript Factor Joy Cat RPL

Running Today

Run examples using the official Docker image:

docker pull forthy42/gforth:latest

Example usage:

docker run --rm -v $(pwd):/app -w /app forthy42/gforth gforth hello.fth -e bye

Topics Covered

Last updated: