目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 69 篇 · 共 80 篇 · 86%

SICP:5.1.1 A Language for Describing Register Machines

发布于 2026-09-18 12:42 · 最后编辑于 2026-09-18 13:28 · 版本 v2 · 字数 5,235 · 预计阅读 11 分钟 👁 12 次阅读

SICP:5.1.1 A Language for Describing Register Machines

本文逐段翻译 SICP 第二版 §5.1.1《A Language for Describing Register Machines》,保留 register-machine language、GCD machine specification、test/branch/goto/assign/perform syntax、Exercise 5.2 以及 Figure 5.4 原始 GIF;图片原始尺寸为 282×268。

TL;DR

  • Data-path 和 controller diagrams 适合简单 machines,但难以描述 Lisp interpreter 这类大型 machines,因此需要 textual register-machine language。
  • Machine description 分为 data paths 与 controller:前者定义 registers、assignment buttons 和 operations,后者定义 instructions 与 labels。
  • Controller instructions 包括 button push、test、conditional branch、unconditional goto,以及用于 side-effecting actions 的 perform。
  • 为了可读性,实际章节中主要使用把 operation definitions 直接写入 controller 的 compact notation。

目录

章节说明
5.1.1 A Language for Describing Register Machinestextual machine language
Describing data pathsregisters 与 operations
Describing the controllerinstructions 与 labels
A GCD machine specification分离式 description
Integrated controller notationcompact notation 与 trade-offs
Exercise 5.2factorial machine description
Actionsread、print 与 perform

5.1.1 A Language for Describing Register Machines

Data-path 和 controller diagrams 足以表示 GCD 这样的 simple machines,但对于 Lisp interpreter 这样的 large machines,它们就显得 unwieldy。为了能够处理 complex machines,我们将创建一种 language,以 textual form 呈现 data-path 与 controller diagrams 所包含的全部信息。

我们先从一种直接 mirror diagrams 的 notation 开始。

Describing data paths

我们通过描述 registers 和 operations 来定义 machine 的 data paths。

描述一个 register 时,我们给它一个 name,并指定控制对它进行 assignment 的 buttons。我们为每个 button 给出一个 name,并指定在该 button 控制下进入 register 的 data source。Source 可以是 register、constant 或 operation。

描述一个 operation 时,我们给出它的 name,并指定它的 inputs(registers 或 constants)。

Describing the controller

我们把 machine 的 controller 定义为一串 instructions,并配合标识 entry points 的 labels。Instruction 可以是下面几种之一:

  • 需要 push 的 data-path button 的 name,用来给 register assign value。这对应 controller diagram 中的一个 box。
  • 执行指定 test 的 test instruction。
  • 根据前一个 test 的 result,跳转到 controller label 所标识位置的 conditional branch(branch instruction)。Test 与 branch 合起来对应 controller diagram 中的一个 diamond。如果 test 为 false,controller 继续执行 sequence 中的下一条 instruction;否则,controller 继续执行 label 后面的 instruction。
  • 指定 controller label 的 unconditional branch(goto instruction),执行从该 label 处继续。

Machine 从 controller instruction sequence 的开头开始执行,到达 sequence 末尾时停止。除非 branch 改变 control flow,否则 instructions 按照列出的顺序执行。

A GCD machine specification

下面的 description 用上述 language 指定 GCD machine 的 data paths 与 controller:

scheme
(data-paths
 (registers
  ((name a)
   (buttons ((name a<-b) (source (register b)))))
  ((name b)
   (buttons ((name b<-t) (source (register t)))))
  ((name t)
   (buttons ((name t<-r) (source (operation rem))))))

 (operations
  ((name rem)
   (inputs (register a) (register b)))
  ((name =)
   (inputs (register b) (constant 0)))))

(controller
 test-b                    ; label
 (test =)                  ; test
 (branch (label gcd-done)) ; conditional branch
 (t<-r)                    ; button push
 (a<-b)                    ; button push
 (b<-t)                    ; button push
 (goto (label test-b))     ; unconditional branch
 gcd-done)                 ; label

Figure 5.3 展示了以这种方式描述的 GCD machine。这个例子只能初步体现这种 description 的 generality,因为 GCD machine 是一个非常 simple 的 case:每个 register 只有一个 button,并且每个 button 和 test 在 controller 中只使用一次。

Integrated controller notation

遗憾的是,上面的 description 很难阅读。要理解 controller instructions,我们必须不断回看 button names 和 operation names 的定义;要理解 buttons 做什么,又可能必须回看 operation names 的定义。

因此,我们会转换 notation,把 data-path 与 controller descriptions 的 information 组合起来,让它们可以在同一处被看到。

在这种 description 中,我们用 behavior definitions 替换 arbitrary button names 和 operation names。也就是说,不再在 controller 中写“push button t<-r”,然后在 data paths 中另行写“button t<-r 把 rem operation 的 value assign 给 register t”以及“rem operation 的 inputs 是 registers a 和 b 的 contents”,而是在 controller 中直接写“push the button that assigns to register t the value of the rem operation on the contents of registers a and b”。

同样,我们不再在 controller 中单独写“perform the = test”,再在 data paths 中写“= test 操作 registers b 的 contents 与 constant 0”,而是在 controller 中直接写“perform the = test on the contents of register b and the constant 0”。我们省略 data-path description,只保留 controller sequence,于是 GCD machine 可以描述为:

scheme
(controller
 test-b
 (test (op =) (reg b) (const 0))
 (branch (label gcd-done))
 (assign t (op rem) (reg a) (reg b))
 (assign a (reg b))
 (assign b (reg t))
 (goto (label test-b))
 gcd-done)

这种 description 比 Figure 5.3 中的形式更容易阅读,但也有 disadvantages:

  • 对大型 machines 来说,它更加 verbose,因为每当 data-path element 在 controller instruction sequence 中被引用时,都要重复完整的 description。(GCD example 中这不是问题,因为每个 operation 和 button 只使用一次。)此外,重复 data-path descriptions 会遮蔽 machine 的 actual data-path structure;对于大型 machine,不容易看出有多少 registers、operations 和 buttons,以及它们如何互相连接。
  • Machine definition 中的 controller instructions 看起来像 Lisp expressions,很容易忘记它们并不是 arbitrary Lisp expressions,而只能记法化 legal machine operations。例如,operations 只能直接作用于 constants 和 registers 的 contents,不能直接作用于其他 operations 的 results。

尽管有这些 disadvantages,本章仍会始终使用这种 register-machine language,因为我们更关注理解 controllers,而不是理解 data paths 中的 elements 与 connections。不过要记住,data-path design 对设计 real machines 至关重要。

Exercise 5.2

使用 register-machine language 描述 Exercise 5.1 中的 iterative factorial machine。

Actions

现在修改 GCD machine,使我们可以在 terminal 中输入要计算 GCD 的 numbers,并打印 answer。我们不讨论如何构造能够 read 和 print 的 machine,而是假设(就像使用 Scheme 中的 read 和 display 时一样)它们作为 primitive operations 已经可用。

read 与此前使用的 operations 类似:它产生一个可以存入 register 的 value。但 read 不从任何 registers 接收 inputs;它的 value 取决于 machine 之外发生的事情。我们允许 machine 的 operations 具有这种 behavior,因此像表示其他计算 value 的 operations 一样表示和记法化 read 的使用。

另一方面,print 与此前使用的 operations 有 fundamental difference:它不产生要存入 register 的 output value。虽然它有 effect,但这个 effect 不作用于我们正在设计的 machine 的某个 part。我们把这种 operation 称为 action。

在 data-path diagram 中,action 与计算 value 的 operation 一样,都用包含 action name 的 trapezoid 表示。Arrows 从任何 inputs(registers 或 constants)指向 action box。我们还为 action 关联一个 button。按下 button 会使 action 发生。

为了让 controller push 一个 action button,我们使用一种新的 instruction:perform。因此,打印 register a contents 的 action,在 controller sequence 中表示为:

scheme
(perform (op print) (reg a))

Figure 5.4 展示了新 GCD machine 的 data paths 与 controller。Machine 不在打印 answer 后停止,而是重新开始,于是它会反复读取一对 numbers、计算它们的 GCD 并打印 result。这种 structure 类似 Chapter 4 interpreters 中使用的 driver loops。

ch5 Z G 3

Figure 5.4: A GCD machine that reads inputs and prints results.

scheme
(controller
 gcd-loop
 (assign a (op read))
 (assign b (op read))
 test-b
 (test (op =) (reg b) (const 0))
 (branch (label gcd-done))
 (assign t (op rem) (reg a) (reg b))
 (assign a (reg b))
 (assign b (reg t))
 (goto (label test-b))
 gcd-done
 (perform (op print) (reg a))
 (goto (label gcd-loop)))

小结

Register-machine language 将 diagrams 中的 data paths 与 controller 转换成 textual representation。分离式 description 适合明确展示 machine structure;integrated controller notation 更便于阅读与理解 controller flow。perform 则扩展了 instruction language,使 machine 能够表示不产生 register value、但具有 external effect 的 actions。

参考

  • Abelson, Sussman, and Sussman, Structure and Interpretation of Computer Programs, 2nd ed., §5.1.1 “A Language for Describing Register Machines”.
  • MIT Press HTML full text
← 返回列表
← 上一篇 SICP:Chapter 5 导论与 5.1 Designing Register Machines 下一篇 → SICP:5.1.3 Subroutines

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论