Hacker News new | past | comments | ask | show | jobs | submit login
Sectorforth: A 16-bit x86 Forth that fits in a boot sector (github.com/cesarblum)
114 points by cblum on Sept 27, 2020 | hide | past | favorite | 32 comments



Author here! This was a super fun project that consumed over a month of evenings and weekends. In the past couple of days I finished the examples, and I'm glad to finally put it out there.

I don't quite remember how I fell into the Forth rabbit hole, but I think I first heard about Collapse OS. Then I read yosefk's "My history with Forth & stack machines" [0], which is actually very critical of Forth. But when I saw if/then/else being defined in Forth itself, I was hooked.

I've always been fascinated by the idea of having a minimal kernel of primitives from which "everything" can be built. Before Forth, I had only seen that in the form of Lisp's "Maxwell equations of software", which is cool, but always left me a little disappointed because it is too abstract to build something that you can actually interact with - you can't break out of its esoteric nature. The same applies to brainf*ck and other extremely small environments.

With Forth, however, you can start from almost nothing, and start adding things like ifs, loops, strings, etc., things that look more like your day-to-day programming. I find that there's a lot of beauty in that.

Anyways, I hope the examples in the repository do not look too awful to experienced Forth programmers :D

[0] https://yosefk.com/blog/my-history-with-forth-stack-machines...


This is a cool project! You might also like

https://golangnews.org/2020/09/bootstrapping/

... a non-exhaustive list of projects that create super-minimal interpreters, compilers, or maybe operating systems with very few dependencies or prerequisites.


> a non-exhaustive list of projects that create super-minimal interpreters

One of the items on that page is described as "25k lines of asm". That's the problem with 'tiny'. It means different things to different people. This FORTH thing however appears to be an exception. You know you're playing the game at a different level when your entire app can be statically linked with all its dependencies and still be small enough to fit inside the master boot record of a floppy disk (i.e. less than 512 bytes). I just wish I could figure out how to use the thing.


> I just wish I could figure out how to use the thing.

We've talked over email. Hope my pointers were enough to get you unblocked :)


Here's the follow-up to our email efforts: https://justine.storage.googleapis.com/forth.mp4 It works!


This just appears to be a copy of the bootstrappable wiki:

https://bootstrappable.org/ https://bootstrapping.miraheze.org/

Any idea why it was copy-pasted to the Golang News blog without attribution?


No, sorry for linking to a source that wasn't original!


I'd really like to revive https://github.com/nathell/lithium sometime. It can't bootstrap itself yet, but "no dependencies, runnable on bare metal" was a goal from the start.


Here's my "one line" (very long) command to run it:

  nbdkit -U - data base64='6h54UAAAAAFAW/8363YFdwEhW48H620OdwNzcEBU62QXdwNycEBV61sgdwIwPViFwA+VwEiYUOtLKXcBK1tYAdhQ60A5dwRuYW5kW1gh2PfQUOswRHcEZXhpdIflXofl6yJUdwN0aWJqAOsYYncFc3RhdGVoABDrC2x3Az5pbmgCEOsArf/geXcEaGVyZWiTd+vxAHmHdwZsYXRlc3Roo3fr4f53lXcDa2V5tADNFlDr0qV3BGVtaXRY6CIB68WydwE66LcAVon+iz6Td6Gjd4k+o3eriMgMQKrzpLj/Jqu4/HeriT6Td8YGABABXuuUh+VWh+WDwASJxuuI8He/d4E7ix6jd4BnAr/GBgAQALhbd4s+k3eriT6Td+lm//wODg4fBxfrC7ghCbsEALkCAM0Qvf52vP7/sAC5BBC/AADzqug4AIseo3eF23Taid6trIjCqEB1DiQfOMh1CFFX86ZfWXQEix/r4Inwvnt4gOKAChYAEP7KdJn/4EJ4iz4CELn//7Ag865PgD0AdBC5///yrk+JPgIQ99FJKc/DsA3oPACwCug3AL8AALQAzRY8DXQdPAh0BuglAKrr7oP/AHTpT+gZALggCrkBAM0Q69u4IACr6AgAxwYCEAAA655QtwC0A80QWLQOswfNEDwIdQyE0nUItAKyT/7OzRDDVao=' --run 'qemu-system-i386 -hda $nbd'
Although HN breaks the lines awkwardly if you copy and paste it should work.


I tried to do something “interesting” with my own little lisp based on the “Maxwell’s Equations of Software” paper. I don’t know if I succeeded exactly (and like most such projects I left it in a somewhat unfinished state), but I did achieve the same “now we’ve defined comments!” kind of mystical experience as the author of the linked article.

Have fun! https://github.com/breckinloggins/vau

(Check out the prelude for satisfying doses of meta)


It's a very different kind of simplicity and self-hosting. Vau uses rich features of a managed programming language in order to achieve lispiness.

The true beauty of something like SectorForth is its mechanical empathy. It relies only on the machine code of the system, and while it has quirks and shortcomings to fit in 512 bytes, it has everything you would need to bootstrap into a fully-productive Forth.

Of course, if Oscar Toledo feels inclined to step in and furnish a 512 byte extensible Lisp that runs on an 8080, I won't be upset.


Yep. I was definitely cheating, and it’s more like “syntactical self hosting”. I’ve wanted to try my hand at a Forth like this but so far I haven’t started anything.


I also recently hacked up a simple FORTH-like system, but mine just emulates the effects making IF/ELSE/THEN and DO/LOOP be built-in primitives.

In short I let you define words, and work in a FORTH-like manner, but without the real extensibility of an actual FORTH system. That will be something I tackle next:

https://github.com/skx/foth/

Nice to see more FORTH stuff posted here!


I actually came across your project yesterday :) Great work!


This is awesome! I've had a similar history very recently and am trying to write a forth with a type system (I think I've found a way to do it with nearly zero overhead) for Civboot.org I have a few notes about it on the github page, and I've just made it through the jobesforth tutorial. Learning asm ain't easy!


Very cool! Does it have any games yet?


This guy has written a bunch of boot sector games, although not boot sector games in forth ... https://github.com/nanochess/Invaders



Awesome!


Leaving out numeric literals is an interesting choice to simplify your Forth kernel. Not many languages have sufficiently flexible naming rules to permit something like ": 3 1 2 + ;"

Stylistically, one of the main suggestions I took to heart early in my career with Forth was to outright ignore "rot" and "-rot"; they make the stack tricky to follow. I see that helloworld.f defines "rot", but it's only used in one place.


Related advice would be to annotate every stack effect with a comment, no exceptions.

Back when I was into Forth, I had this wild notion of adding a stack checker, defining two additional comment words which would be aware of the stack effect of all core words, and require a stack effect to be defined for every new piece of vocabulary.

Never did anything with it, don't write Forth anymore, doubt I will again. I still think it's a good idea though, so I like to put it out there.


Have you seen Factor? https://factorcode.org/


That's actually been done. It's called StrongForth.

http://www.arestlessmind.org/2009/02/03/intro.html


You beat me to it. See also this other page on StrongForth: https://www.stephan-becher.de/strongforth/

See also the Kitten programming language, which does something similar: https://kittenlang.org/


Yep, annotating the examples with stack effects is in my plans :)


Oh, interesting. I feel like I've gotten used to reasoning with rot and -rot. Didn't know there was advice out there not to use it.


It's not any more or less intuitive to me than nip and over.

Where there's three or more items to be manipulated there's always at least ambiguity which way rot is supposed to rotate according to codebase author. With more than three items it's easy to see what can be done but no unambiguous and meaningful names. The only consistent stack word names that work 4 items deep are 2dup and 2drop.


Heh, I was thinking about this idea literally last week, nice to see someone's done it :)


Now all we need is a networking layer and we'll be all set. evil grin


Best[1] I could do was a non-networked forth that easily fit in a jumbo frame. Maybe one day I'll think of a trick to fit it in a normal MTU, but it's more likely that networking will widen in the meantime.

[1] I chose the constraint that CODE words were fine, but the kernel had to already contain the dictionary implementation: no "magic numbers" known to both the forth and kernel source. The motivation for that constraint was the "three instruction forth" which I would call a "monitor program" instead.


The old Sun workstations had a firmware in Forth that could run networking (very basic for bootp/tftp)


STOICAL does networking, and a whole lot more... but it looks like it hasn't been touched since 2002. 8(

http://stoical.sourceforge.net/documentation.php




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: