# Z-machine

Z-machine 是一台虚拟机，一台只在软件中实现的假想计算机，由 [Infocom](https://www.edgepedia.org/zh/infocom) 于 1979 年设计，目的是让该公司的互动小说文字冒险游戏能在当时任何个人电脑上运行。为 Z-machine 编译的游戏以 Z 代码的“故事文件”形式存储，任何为真实计算机编写的解释器都能运行它，因此将游戏移植到新平台只需编写一个新解释器，而不必重写游戏<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup><sup> • </sup><sup>[2](https://www.inform-fiction.org/manual/html/s41.html)</sup>。这台机器从未以电路形式存在过；相反，20 世纪 80 年代和 90 年代制造的几乎每一台真实计算机都被教会了模拟它<sup>[2](https://www.inform-fiction.org/manual/html/s41.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 创建时间 | 1979 年，匹兹堡 Joel Berez 母亲的咖啡桌上；设计者包括 Joel Berez、Marc Blank、P. David Lebling 和 Tim Anderson<sup>[2](https://www.inform-fiction.org/manual/html/s41.html)</sup> |
| 目的 | 可移植地运行 Infocom 的文字冒险游戏：任何合法的故事文件在任何解释器上的行为都完全确定<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup> |
| 版本 | 共有八个版本；故事文件的第一个字节给出它必须运行的版本<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup> |
| 故事文件大小限制 | 128K（版本 1-3）、256K（版本 4-5）、512K（版本 6）、320K（版本 7）、512K（版本 8）<sup>[3](https://inform-fiction.org/zmachine/standards/z1point0-1997/sect01.html)</sup> |
| 内存上限 | 动态内存加静态内存合计不得超过 64K 减 2 字节<sup>[3](https://inform-fiction.org/zmachine/standards/z1point0-1997/sect01.html)</sup> |
| 存档格式 | Quetzal，由 Martin Frost 设计，允许在一个解释器上保存的游戏在另一个解释器上继续；最小存档文件开销为 36 字节<sup>[4](https://www.inform-fiction.org/zmachine/standards/quetzal/index.html)</sup> |
| 标准 | 《Z-Machine 标准文档》1.0（1997 年）和 1.1（2001 年提出，2006 年定稿，2014 年并入正文）<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup><sup> • </sup><sup>[5](https://inform-fiction.org/zmachine/standards/)</sup> |
| 遗产目录 | Infocom 的 Zilch 编译器编译的约 130 个故事文件留存至今，自 1993 年以来，又有许多用 Inform 设计系统制作的故事文件问世<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup> |

## 在 Infocom 的起源

Z-machine 于 1979 年在[匹兹堡](https://www.edgepedia.org/zh/pittsburgh)的一张咖啡桌上诞生，是一台假想计算机，其程序就是冒险游戏。它非常适合这项任务，以惊人的紧凑方式实现复杂的游戏：游戏长度可能仍有 100K，对当时家用电脑的内存来说太大了，而 Z-machine 似乎首次在微型计算机上使用了虚拟内存。同样超前的是高效保存和恢复整个执行状态的能力<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup>。

Infocom 的游戏用一种名为 ZIL 的高级语言编写，即 Zork 实现语言。Z-machine 设计完成后，Joel Berez 和 Marc Blank 编写了一个两阶段编译器，先将 ZIL 转换为汇编代码，再转换为 Z-machine 字节码。Marc Blank 随后为 DECsystem-20 编写了第一个 Z-machine 模拟器，即 Z-machine 解释程序（ZIP）。1980 年 Scott Cutler 完成了 TRS-80 Model I 的 ZIP，1981 年 Bruce Daniels 完成了 Apple II 的 ZIP<sup>[6](https://ifarchive.org/if-archive/infocom/info/infocom-paper.pdf)</sup>。

**可移植性作为商业资产。** 游戏文件存储为由 ZIP 解释的 Z-machine 字节码，因此要让软件在新平台上运行，Infocom 只需编写一个 ZIP。这一点很重要，因为 Atari、Apple、Commodore、IBM、NEC、Radio Shack 等公司都有个人电脑型号在市场上销售；到 1982 年，Apple II 占据了超过 50% 的市场，其余部分则分散在其他平台之间<sup>[6](https://ifarchive.org/if-archive/infocom/info/infocom-paper.pdf)</sup>。Z-machine 的可移植性使 Infocom 在家用电脑泡沫时期比竞争对手更具优势<sup>[7](https://zspec.jaredreisinger.com/d-history)</sup>。

## 架构

Z-machine 的内存映射是一个字节数组，分为三个区域：动态内存、静态内存和高内存。动态内存从字节地址 0 开始，在游戏过程中可读可写，存放变量、对象表和其他变化的数据；其前 64 个字节是文件头，其中第 0 字节存放故事文件所需的 Z-machine 版本号。静态内存可读，但游戏不能写入。高内存存放例程和静态字符串，可以调用或打印，但不能逐字节检查<sup>[3](https://inform-fiction.org/zmachine/standards/z1point0-1997/sect01.html)</sup>。

该设计既约束了最高性能也约束了最低性能，这正是游戏能 100% 可移植的原因：任何合法程序的行为都完全确定。例如，[动态内存分配](https://www.edgepedia.org/zh/dynamic-memory-allocation)是不可能的<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup>。最严重的限制是动态内存加静态内存合计不得超过 64K 减 2 字节<sup>[3](https://inform-fiction.org/zmachine/standards/z1point0-1997/sect01.html)</sup>。

指令由操作码编号、可选的操作数类型字段、0 到 8 个操作数以及可选的存储和分支部分组成。操作数有四种类型：大常量（2 字节）、小常量（1 字节）、变量（1 字节）和省略<sup>[8](https://inform-fiction.org/zmachine/standards/z1point1/sect04.html)</sup>。完整指令集包含 119 个操作码，今天普遍使用的操作码名称是 1994 年至 1995 年间由反汇编器 Txd 的作者 Mark Howell 和 Inform 汇编层的作者 Graham Nelson 商定的；它们与 Infocom 未公开的名称并不对应<sup>[9](https://inform-fiction.org/zmachine/standards/z1point1/sect14.html)</sup><sup> • </sup><sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup>。

该机器有一个用于求值和例程调用的栈，存储在解释器的私有内存中，位于 Z-machine 内存映射之外。规范保证游戏的递归例程调用加上其自身栈使用永远不需要达到 1024 字的用量，但后来的游戏需要大得多的栈；现代解释器如 Windows Frotz 允许 32768，nfrotz 允许 61440<sup>[10](https://www.inform-fiction.org/zmachine/standards/z1point1/sect06.html)</sup>。

## 版本

1979 年构想的 Z-machine 现在称为版本 1，此后又有七个版本<sup>[2](https://www.inform-fiction.org/manual/html/s41.html)</sup>：

- **版本 1 和 2**：早期草案设计，仅用于 Zork 三部曲的首次发行；Inform 无法生成它们。
- **版本 3**：Infocom 的标准格式，其限制包括最多 255 个对象、32 个特征标志、每个对象最多 4 个属性数组条目、调用例程时最多 3 个参数，以及故事文件不得超过 128K。
- **版本 4**：部分升级版，现在很少使用。
- **版本 5**：Infocom 的高级设计，通常由 Inform 使用，限制提高到 65,535 个对象、48 个特征标志、32 个属性条目、7 个调用参数和 256K 文件。
- **版本 6**：与版本 5 类似，但支持图片。
- **版本 7**：中间版本，现已不推荐使用。
- **版本 8**：与版本 5 相同，只是故事文件最长可达 512K；大多数最大的 Inform 游戏使用它<sup>[2](https://www.inform-fiction.org/manual/html/s41.html)</sup>。

尽管版本 3 可以容纳 128K 的故事文件，但在 1982-4 年的实际限制约为 110K，即当时目标机器上典型的磁盘容量<sup>[7](https://zspec.jaredreisinger.com/d-history)</sup>。最后一批纯文字版本 5 故事文件出现于 1992 年<sup>[11](https://christham.net/cv/article/infotaskforce/)</sup>。

## 逆向工程与标准化

Infocom 关于 ZIL 和 Z-machine 的规范随着公司并入 [Activision](https://www.edgepedia.org/zh/activision) 而丢失<sup>[12](https://ifarchive.org/if-archive/infocom/compilers/inform6/manuals/inform_faq.pdf)</sup>。1987 年，悉尼大学的五个人拆解了 Infocom 的虚拟机，以了解其工作原理。David 在第一代 Apple Macintosh 上用 C 语言编写了一个全新的解释器，于 1987 年 5 月 25 日编译并运行；据此后人们所能考证，这是第一个可移植的 Z-machine 解释器。1987 年 6 月 2 日发生了一次意外上传，同年 8 月 1 日发布了官方 1.0 版，代码于 1991 年进入 Infocom 档案 FTP 服务器<sup>[11](https://christham.net/cv/article/infotaskforce/)</sup>。在 20 世纪 80 年代末和 90 年代初，包括 Infocom Task Force 在内的团体通过这些努力，制作出了可用的工具，用于反汇编故事文件（txd）、提取文本（Infodump）、运行它们（InfoTaskForce 和 Zip）以及创建它们（Inform）<sup>[5](https://inform-fiction.org/zmachine/standards/)</sup>。

到 20 世纪 90 年代中期，该格式已被充分理解，但文档匮乏。正式标准的第一稿（0.2）于 1996 年编成，被接受的 1.0 标准可追溯至 1997 年 5 月至 9 月。该格式被认为成熟且稳定；人们普遍认为不宜用例如 32 位架构来重构 Z-machine，部分原因是 Glulx 虚拟机已经解决了这个问题。没有正式的标准委员会；标准在 Z-Machine 邮件列表上讨论，决定由普遍共识作出<sup>[5](https://inform-fiction.org/zmachine/standards/)</sup>。标准 1.1 增加了更好的颜色支持，使游戏可以访问 32,768 种颜色，而不是之前的 11 种，并还有其他改进<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup>。

## 保存游戏：Quetzal

Z-machine 游戏中的游戏状态由动态内存内容、栈、程序计数器和例程调用状态组成。保存游戏时必须存储整个状态，尽管存档文件格式本身并未由 Z-machine 标准规定<sup>[10](https://www.inform-fiction.org/zmachine/standards/z1point1/sect06.html)</sup>。实践中解释器使用 Quetzal，这是 Martin Frost 制定的通用存档文件格式标准，旨在让不同解释器交换存档游戏；玩家可以在一个解释器上保存，比如桌面机器，然后在另一个上继续<sup>[4](https://www.inform-fiction.org/zmachine/standards/quetzal/index.html)</sup><sup> • </sup><sup>[5](https://inform-fiction.org/zmachine/standards/)</sup>。

Quetzal 是一种 IFF 风格的分块格式。必需的分块是 IFhd，通过发行号、序列号和校验和标识故事文件；CMem 或 UMem，存放压缩或简单转储的动态内存；以及 Stks，存放栈帧。最小三块情况下的总开销为 36 字节<sup>[4](https://www.inform-fiction.org/zmachine/standards/quetzal/index.html)</sup>。Quetzal 不是 Z-Machine 标准的强制部分，因为它不影响故事文件的行为，但它作为高度推荐的可选附加项附在标准之后<sup>[4](https://www.inform-fiction.org/zmachine/standards/quetzal/index.html)</sup>。

## Cornerstone 与 Infocom 的终结

1983-5 年间，Infocom 将资源投入 [Cornerstone](https://www.edgepedia.org/zh/cornerstone)，这是其创始人的一个雄心勃勃的数据库项目，使用了与 Z-machine 相同的一些可移植虚拟机理念<sup>[7](https://zspec.jaredreisinger.com/d-history)</sup>。Cornerstone 于 1985 年发布，运行在自己的虚拟机上，这使它以速度慢而臭名昭著，而其可移植性优势意义不大，因为 IBM 兼容机已经赢得了商业平台市场。资金被转移到数据库项目，导致游戏部门缺乏资源，一年后 Infocom 不得不与 Activision 合并以维持生存；Activision 于 1989 年关闭了该工作室<sup>[13](https://www.pcgamer.com/gaming-industry/the-ill-advised-business-software-that-helped-sink-videogame-pioneers-infocom-now-runs-on-modern-pcs-its-a-new-age-for-aficionados-of-failed-1985-database-products/)</sup>。

## 解释器与后来的使用

Infocom 的原始解释器用汇编语言为 6502 和 Z-80 等处理器编写，作为历史源代码留存下来<sup>[14](https://github.com/erkyrath/infocom-zcode-terps)</sup>。现代开源解释器包括 Frotz，最初由 Stefan Jokisch 于 1995-1997 年编写，支持 Z-machine 的所有版本，只是版本 6 仅得到半支持<sup>[15](https://github.com/DavidGriffith/frotz)</sup>。Infocom 的游戏作品仍在销售，玩家既可使用原始解释器，也可使用较新的免费解释器来游玩，该机器也可以在浏览器中运行<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup>。自 1993 年以来，Inform 设计系统——其编译器以 Z-machine 为目标——制作的故事文件比 Infocom 自己编译的多得多<sup>[1](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)</sup>。

## References

1. [The Z-Machine Standards Document, Preface (Standard 1.1), Graham Nelson et al.](https://www.inform-fiction.org/zmachine/standards/z1point1/preface.html)
2. [The Inform Designer's Manual, 4th edition, §41: Architecture and assembly, Graham Nelson](https://www.inform-fiction.org/manual/html/s41.html)
3. [The Z-Machine Standards Document 1.0, §1: The memory map](https://inform-fiction.org/zmachine/standards/z1point0-1997/sect01.html)
4. [The Quetzal Z-Machine Saved Game Standard, Martin Frost](https://www.inform-fiction.org/zmachine/standards/quetzal/index.html)
5. [Inform: Z-Machine Standards overview page](https://inform-fiction.org/zmachine/standards/)
6. [Infocom paper (infocom-paper.doc), IF Archive](https://ifarchive.org/if-archive/infocom/info/infocom-paper.pdf)
7. [Appendix D: A short history of the Z-machine, Z-Machine Standards Document](https://zspec.jaredreisinger.com/d-history)
8. [The Z-Machine Standards Document 1.1, §4: How instructions are encoded](https://inform-fiction.org/zmachine/standards/z1point1/sect04.html)
9. [The Z-Machine Standards Document 1.1, §14: Complete table of opcodes](https://inform-fiction.org/zmachine/standards/z1point1/sect14.html)
10. [The Z-Machine Standards Document 1.1, §6: Variables and stack](https://www.inform-fiction.org/zmachine/standards/z1point1/sect06.html)
11. [Reverse engineering Infocom games: the first open source Z-Machine interpreter, Chris Tham (1992)](https://christham.net/cv/article/infotaskforce/)
12. [Inform 6: Frequently Asked Questions, IF Archive](https://ifarchive.org/if-archive/infocom/compilers/inform6/manuals/inform_faq.pdf)
13. [The ill-advised business software that helped sink videogame pioneers Infocom now runs on modern PCs, PC Gamer](https://www.pcgamer.com/gaming-industry/the-ill-advised-business-software-that-helped-sink-videogame-pioneers-infocom-now-runs-on-modern-pcs-its-a-new-age-for-aficionados-of-failed-1985-database-products/)
14. [Infocom Z-code interpreters, historical source code repository, GitHub](https://github.com/erkyrath/infocom-zcode-terps)
15. [Frotz: Infocom-style interactive fiction player, GitHub](https://github.com/DavidGriffith/frotz)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/z-machine

English original: https://www.edgepedia.org/z-machine
