Skip to content
Tech News
← Back to articles

Sectorforth is a 16-bit x86 Forth that fits in a 512-byte boot sector (2020)

read original more articles
Why This Matters

Sectorforth exemplifies how a minimalistic 16-bit x86 Forth can be compressed into just 512 bytes, fitting within a boot sector. Its design emphasizes simplicity and self-sufficiency, allowing users to build complex functionalities from a small set of primitives, which is especially valuable for embedded systems and bootloaders. This project highlights the potential for highly compact, customizable interpreters in the evolving landscape of low-level computing and firmware development.

Key Takeaways

sectorforth

sectorforth is a 16-bit x86 Forth that fits in a 512-byte boot sector.

Inspiration to write sectorforth came from a 1996 Usenet thread (in particular, Bernd Paysan's first post on the thread).

Batteries not included

sectorforth contains only the eight primitives outlined in the Usenet post above, five variables for manipulating internal state, and two I/O primitives.

With that minimal set of building blocks, words for branching, compiling, manipulating the return stack, etc. can all be written in Forth itself (check out the examples!).

The colon compiler ( : ) is available, so new words can be defined easily (that means ; is also there, of course).

Contrary to many Forth implementations, sectorforth does not attempt to convert unknown words to numbers, since numbers can be produced using the available primitives. The two included I/O primitives are sufficient to write a more powerful interpreter that can parse numbers.

Primitives

Primitive Stack effects Description @ ( addr -- x ) Fetch memory contents at addr ! ( x addr -- ) Store x at addr sp@ ( -- sp ) Get pointer to top of data stack rp@ ( -- rp ) Get pointer to top of return stack 0= ( x -- flag ) -1 if top of stack is 0, 0 otherwise + ( x y -- z ) Sum the two numbers at the top of the stack nand ( x y -- z ) NAND the two numbers at the top of the stack exit ( r:addr -- ) Pop return stack and resume execution at addr key ( -- x ) Read key stroke as ASCII character emit ( x -- ) Print low byte of x as an ASCII character

... continue reading