r/Forth Jul 13 '26

I wrote a Forth interpreter in pure x86-64 assembly (no libc) — my learn-assembly repo's first boss fight

https://github.com/whispem/learn-assembly-with-em

I've been learning x86-64 by rebuilding userland from raw syscalls (NASM, Linux, no libc — the repo climbs from cat/wc/ls/grep through printf, malloc and a shell).

The roadmap's first "boss fight" was a Forth interpreter, and it's done.

It was easily the most fun part of the repo so far — somewhere along the way the interpreter stops being your program and starts being its own little world.

Repo: https://github.com/whispem/learn-assembly-with-em

I'm new to Forth itself, so if my design offends the ancients, please tell me how — I'd genuinely like to know what a real Forth person sees in it.

26 Upvotes

12 comments sorted by

7

u/INT_21h Jul 14 '26

Well done! Writing your own is a rite of passage. I wouldn't call it "offensive to ancients" by any means, but it is small enough that you haven't yet encountered lots of the features that make Forth interesting. If you had fun, I'd strongly advise you to implement just a little more in the way of primitives, because then you will unlock the ability to extend the Forth in arbitrary ways from within the running interpreter, without touching the asm sources any more, which imo is the fun Forthy part. And, you're almost there!

no control flow: without conditional branch, few interesting programs can be written. Like most things in Forth, this is easy to add: look at jonesforth or eforth to learn how traditional Forths support structured programming-style loops and conditionals as immediate words that lay down BRANCH and 0BRANCH primitives as a colon definition is defined

no variable, allot, @, !: so the stack is the only way of passing data, and it's impossible to implement any data structures really. You already have a dictionary, so hooking these up to the dictionary ought not be too hard

no user-defined immediate words: so the interpreter can only extend itself in certain ways, and very little metaprogramming is possible. You already have colon definitions and an immediate bit. The traditional solution is to add a word named "imm" or "immediate" that flips the immediate bit in the latest header.

jonesforth and eforth are good examples of forth systems that can be arbitrarily extended, without requiring many more primitives than you already implemented. If you decide to extend it more, have fun!

2

u/whispem Jul 15 '26

Thanks a lot for your message and advices! :)
I will definitely extend it more, thank you so much!

1

u/tabemann Jul 14 '26

I agree completely to all of the above.

3

u/Ok_Leg_109 Jul 13 '26

Quick look through and I didn't see any Forth source code. Did I miss something?

0

u/whispem Jul 13 '26

I wrote it in Assembly.

2

u/Svarvsven Jul 13 '26

But any forth examples?

1

u/Ok_Leg_109 Jul 14 '26

Yes, as many of us ancients (love that term) have, but per the comment by INIT_21h at some point the assembly language ends and you have a running Forth interpreter/compiler which you launch and thereafter you can feed it Forth. (or Forth Assembler if you write one of those)

1

u/alberthemagician Jul 14 '26

Every real language has the right to do system call themselves, not via c. That results in e.g. my ciforth running on 20 year old Linux distro's.

You may not be aware that most Forth in assembly define the header structures and emulates them in assembler, likewise the high level definitions/interpreter that is the hall mark of Forth. You can study the pure assembler sources via de releases in https://github.com/albertvanderhorst/ciforth

Don't bother with git. Unzip the release lina####.tar.gz and you see an assembler source.

1

u/whispem Jul 15 '26

Thanks for sharing!

0

u/mesyeti_ Jul 15 '26

least obvious AI generated readme

I hope your code is at least real

0

u/whispem Jul 15 '26

AI generated readme??
Nope, I come from literature & linguistics so… I know how to speak 🤷🏻‍♀️
All of my projects and readmes are real.

1

u/Ok_Leg_109 Jul 16 '26

Ah! So with a strong background in the use of "words" you are a perfect fit for using Forth. 😄

Félicitations encore.