Sectorforth 是一个 16 位 x86 Forth 系统,可置于 512 字节的引导扇区内 (2020)。
Sectorforth is a 16-bit x86 Forth that fits in a 512-byte boot sector (2020)

原始链接: https://github.com/cesarblum/sectorforth

**sectorforth** 是一个极简的 16 位 x86 Forth 实现,旨在放入 512 字节的引导扇区内。受 1996 年 Usenet 讨论的启发,该项目仅提供 15 个基础构建块(包括 8 个原语、5 个状态变量和 2 个 I/O 操作),从而实现了极致的紧凑性。 尽管体积很小,sectorforth 仍包含一个冒号编译器,允许用户定义新词并从零开始构建复杂功能。它特意去除了自动数字转换功能,将该任务交给用该语言编写的更复杂的解释器来处理。 该系统使用 NASM 开发,专为裸机执行而设计,可通过 QEMU 轻松构建和测试。虽然它缺乏标准的“ok”提示符和稳健的错误处理,但它是理解线程代码和底层 Forth 实现的教学工具。它最适合已经熟悉 Forth 或对极简可引导软件架构感兴趣的人。

```Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Sectorforth 是一个可装入 512 字节引导扇区的 16 位 x86 Forth 系统 (2020) (github.com/cesarblum) 17 分,由 sigalor 发布于 5 小时前 | 隐藏 | 过往 | 收藏 | 3 条评论 帮助 Saltloaf 20 分钟前 | 下一条 [-] 在 512 字节内实现八个原语和一个冒号编译器,很好地展示了如何从几乎零基础开始引导。从那里构建出来的示例是最好的部分。 回复 gabrielsroka 1 小时前 | 上一条 | 下一条 [-] 2020 回复 jxnsneb 59 分钟前 | 上一条 [-] 考虑到它大量使用了 BIOS 例程,而包括基础解释器在内的整个操作系统都能装入几个 KiB,这并不令人印象深刻。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:```
相关文章

原文

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).

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.

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
Variable Description
state 0: execute words; 1: compile word addresses to the dictionary
tib Terminal input buffer, where input is parsed from
>in Current parsing offset into terminal input buffer
here Pointer to next free position in the dictionary
latest Pointer to most recent dictionary entry

sectorforth was developed using NASM 2.15.01. Earlier versions of NASM are probably capable of compiling it, but that hasn't been tested.

To compile sectorforth, just run make:

That will produce a compiled binary (sectorforth.bin) and a floppy disk image (sectorforth.img) containing the binary in its boot sector.

The makefile contains two targets for running sectorforth in QEMU:

  • debug starts QEMU in debug mode, with execution paused. That allows you to set up a remote target in GDB (target remote localhost:1234) and set any breakpoints you want before sectorforth starts running.
  • run simply runs sectorforth in QEMU.

Up to 4KB of input can be entered per line. After pressing return, the interpreter parses one word at a time an interprets it (i.e. executes it or compiles it, according to the current value of the state variable).

sectorforth does not print the ok prompt familiar to Forth users. However, if a word is not found in the dictionary, the error message !! is printed in red, letting you know an error happened.

When a word is not found in the dictionary, the interpreter's state is reset: the data and return stacks, as well as the terminal input buffer are cleared, and the interpreter is placed in interpretation mode. Other errors (e.g. compiling an invalid address in a word definition and attempting to execute it) are not handled gracefully, and will crash the interpreter.

Comments throughout the code assume familiarity with Forth and how it is commonly implemented.

If you're not familiar with Forth, read Leo Brodie's Starting Forth.

If you're not familiar with how Forth is implemented on x86, read the assembly code for Richard W.M. Jones' jonesforth.

sectorforth draws a lot of inspiration from jonesforth, but the latter does a much better job at explaining the basics in its comments.

For an excellent introduction to threaded code techniques, and to how to implement Forth in different architectures, read Brad Rodriguez's Moving Forth.

联系我们 contact @ memedata.com