Opcodes revisited
I personally found the opcode tables very hard to interpret, despite the section that tries to explain things. After looking at some output from Txd, however, the tables make a little more sense… but they are still (to me) awkwardly constructed, not easy to interpret, and not exhaustive.
Further, S4 delves into how the bitfields in an opcode should be interpreted, but then the tables in S14 don’t list the instructions by opcode number, instead (kind of?) falling back to the raw opcode value. This raises the question whether the opcode number really has any meaning or not. Through some trial-and-error, I’ve determined that “opcode number” as defined in S4.3 is not sufficient in and of itself to define a Z-Machine instruction. Why? Because instructions often require a particular number of operands, and more than one of the operand-count values (0OP, 1OP, 2OP, VAR) can have the same “opcode number” value. Further, because the two high-bits of the opcode define the form and not the operand count—and in fact the operand count might be implied by form and operand type!—it is not at all trivial to describe “what does instruction xxx look like?” both concisely and completely.
When trying to author an interpreter, however, you very much need to know how to answer that question. To that end, what follows is a re-creation of the opcode tables, keyed off the operand count (or form for extended opcodes!) and opcode number, but with every variant listed explicitly. Although the table below uses Count as a header, it’s also used for “EXT” for extended opcode instructions. The original tables started with 2OP, but I believe that was an unexplicit side-effect of $$00
-prefixed opcodes being long-form 2OP ones. Since there will necessarily be an intermingling of opcode value (to keep like opcode numbers together), this list uses the order 0OP, 1OP, 2OP, VAR, EXT. In this table, Num is the opcode number, and should represent an instruction in the context of a particular Count (with the caveat that it might change across Z-machine versions). The Dec., Hex, and Binary columns are the complete opcode value, to aid in looking at hex dumps and writing an interpreter. (For the original table’s TYPE:Decimal identifiers, look at Count:Decimal here.) The Form and Args (operands with a shorter label!) columns are abbreviated to S,L,V,E for Short, Long, Variable, and Extended for the form, and als to S,L,V for each operand for Small constant, Large constant, and Variable. Also, note that per-version variations might look like they are specific to particular decimal/hex opcodes, but they typically aren’t; Rather than duplicate the entire set of opcodes for multi-variant instructions, they are only listed once (see 1OP:15 as an example). Truly, both the decimal/hex/binary/form/operand-types and the per-version instruction variations should be thought of as orthogonal nested tables, but the layout is already rather complex.
Count | Num | Dec. | Hex | Binary | Form | Args | V | St | Br | Instruction and syntax |
---|---|---|---|---|---|---|---|---|---|---|
0OP opcodes |
||||||||||
0OP |
0 |
176 |
|
|
S |
|||||
0OP |
1 |
177 |
|
|
S |
|||||
0OP |
2 |
178 |
|
|
S |
|
||||
0OP |
3 |
179 |
|
|
S |
|
||||
0OP |
4 |
180 |
|
|
S |
1/- |
||||
0OP |
5 |
181 |
|
|
S |
1 |
* |
|
||
4 |
|
|||||||||
5 |
illegal |
|||||||||
0OP |
6 |
182 |
|
|
S |
1 |
* |
|
||
4 |
|
|||||||||
5 |
illegal |
|||||||||
0OP |
7 |
183 |
|
|
S |
|||||
0OP |
8 |
184 |
|
|
S |
|||||
0OP |
9 |
185 |
|
|
S |
1 |
||||
5/6 |
* |
|
||||||||
0OP |
10 |
186 |
|
|
S |
|||||
0OP |
11 |
187 |
|
|
S |
|||||
0OP |
12 |
188 |
|
|
S |
3 |
||||
4 |
illegal |
|||||||||
0OP |
13 |
189 |
|
|
S |
3 |
|
|||
0OP:14 is 190 ( |
||||||||||
0OP |
15 |
191 |
|
|
S |
5/- |
||||
1OP opcodes |
||||||||||
Count |
Num |
Dec. |
Hex |
Binary |
Form |
Args |
V |
St |
Br |
Instruction and syntax |
1OP |
0 |
128 |
|
|
S |
L |
* |
|
||
144 |
|
|
S |
S |
||||||
160 |
|
|
S |
V |
||||||
1OP |
1 |
129 |
|
|
S |
L |
* |
* |
|
|
145 |
|
|
S |
S |
||||||
161 |
|
|
S |
V |
||||||
1OP |
2 |
130 |
|
|
S |
L |
* |
* |
|
|
146 |
|
|
S |
S |
||||||
162 |
|
|
S |
V |
||||||
1OP |
3 |
131 |
|
|
S |
L |
* |
|
||
147 |
|
|
S |
S |
||||||
163 |
|
|
S |
V |
||||||
1OP |
4 |
132 |
|
|
S |
L |
* |
|
||
148 |
|
|
S |
S |
||||||
164 |
|
|
S |
V |
||||||
1OP |
5 |
133 |
|
|
S |
L |
|
|||
149 |
|
|
S |
S |
||||||
165 |
|
|
S |
V |
||||||
1OP |
6 |
134 |
|
|
S |
L |
|
|||
150 |
|
|
S |
S |
||||||
166 |
|
|
S |
V |
||||||
1OP |
7 |
135 |
|
|
S |
L |
|
|||
151 |
|
|
S |
S |
||||||
167 |
|
|
S |
V |
||||||
1OP |
8 |
136 |
|
|
S |
L |
4 |
* |
|
|
152 |
|
|
S |
S |
||||||
168 |
|
|
S |
V |
||||||
1OP |
9 |
137 |
|
|
S |
L |
|
|||
153 |
|
|
S |
S |
||||||
169 |
|
|
S |
V |
||||||
1OP |
10 |
138 |
|
|
S |
L |
|
|||
154 |
|
|
S |
S |
||||||
170 |
|
|
S |
V |
||||||
1OP |
11 |
139 |
|
|
S |
L |
|
|||
155 |
|
|
S |
S |
||||||
171 |
|
|
S |
V |
||||||
1OP |
12 |
140 |
|
|
S |
L |
|
|||
156 |
|
|
S |
S |
||||||
172 |
|
|
S |
V |
||||||
1OP |
13 |
141 |
|
|
S |
L |
|
|||
157 |
|
|
S |
S |
||||||
173 |
|
|
S |
V |
||||||
1OP |
14 |
142 |
|
|
S |
L |
* |
|
||
158 |
|
|
S |
S |
||||||
174 |
|
|
S |
V |
||||||
1OP |
15 |
143 |
|
|
S |
L |
1/4 |
* |
|
|
159 |
|
|
S |
S |
5 |
|
||||
175 |
|
|
S |
V |
||||||
2OP opcodes |
||||||||||
Count |
Num |
Dec. |
Hex |
Binary |
Form |
Args |
V |
St |
Br |
Instruction and syntax |
2OP |
0 |
0 |
|
|
L |
S,S |
― |
― |
― |
― |
32 |
|
|
L |
S,V |
||||||
64 |
|
|
L |
V,S |
||||||
96 |
|
|
L |
V,V |
||||||
192 |
|
|
V |
? |
||||||
2OP |
1 |
1 |
|
|
L |
S,S |
* |
|
||
33 |
|
|
L |
S,V |
||||||
65 |
|
|
L |
V,S |
||||||
97 |
|
|
L |
V,V |
||||||
193 |
|
|
V |
? |
||||||
2OP |
2 |
2 |
|
|
L |
S,S |
* |
|
||
34 |
|
|
L |
S,V |
||||||
66 |
|
|
L |
V,S |
||||||
98 |
|
|
L |
V,V |
||||||
194 |
|
|
V |
? |
||||||
2OP |
3 |
3 |
|
|
L |
S,S |
* |
|
||
35 |
|
|
L |
S,V |
||||||
67 |
|
|
L |
V,S |
||||||
99 |
|
|
L |
V,V |
||||||
195 |
|
|
V |
? |
||||||
2OP |
4 |
4 |
|
|
L |
S,S |
* |
|
||
36 |
|
|
L |
S,V |
||||||
68 |
|
|
L |
V,S |
||||||
100 |
|
|
L |
V,V |
||||||
196 |
|
|
V |
? |
||||||
2OP |
5 |
5 |
|
|
L |
S,S |
* |
|
||
37 |
|
|
L |
S,V |
||||||
69 |
|
|
L |
V,S |
||||||
101 |
|
|
L |
V,V |
||||||
197 |
|
|
V |
? |
||||||
2OP |
6 |
6 |
|
|
L |
S,S |
* |
|
||
38 |
|
|
L |
S,V |
||||||
70 |
|
|
L |
V,S |
||||||
102 |
|
|
L |
V,V |
||||||
198 |
|
|
V |
? |
||||||
2OP |
7 |
7 |
|
|
L |
S,S |
* |
|
||
39 |
|
|
L |
S,V |
||||||
71 |
|
|
L |
V,S |
||||||
103 |
|
|
L |
V,V |
||||||
199 |
|
|
V |
? |
||||||
2OP |
8 |
8 |
|
|
L |
S,S |
* |
|
||
40 |
|
|
L |
S,V |
||||||
72 |
|
|
L |
V,S |
||||||
104 |
|
|
L |
V,V |
||||||
200 |
|
|
V |
? |
||||||
2OP |
9 |
9 |
|
|
L |
S,S |
* |
|
||
41 |
|
|
L |
S,V |
||||||
73 |
|
|
L |
V,S |
||||||
105 |
|
|
L |
V,V |
||||||
201 |
|
|
V |
? |
||||||
2OP |
10 |
10 |
|
|
L |
S,S |
* |
|
||
42 |
|
|
L |
S,V |
||||||
74 |
|
|
L |
V,S |
||||||
106 |
|
|
L |
V,V |
||||||
202 |
|
|
V |
? |
||||||
2OP |
11 |
11 |
|
|
L |
S,S |
|
|||
43 |
|
|
L |
S,V |
||||||
75 |
|
|
L |
V,S |
||||||
107 |
|
|
L |
V,V |
||||||
203 |
|
|
V |
? |
||||||
2OP |
12 |
12 |
|
|
L |
S,S |
|
|||
44 |
|
|
L |
S,V |
||||||
76 |
|
|
L |
V,S |
||||||
108 |
|
|
L |
V,V |
||||||
204 |
|
|
V |
? |
||||||
2OP |
13 |
13 |
|
|
L |
S,S |
|
|||
45 |
|
|
L |
S,V |
||||||
77 |
|
|
L |
V,S |
||||||
109 |
|
|
L |
V,V |
||||||
205 |
|
|
V |
? |
||||||
2OP |
14 |
14 |
|
|
L |
S,S |
|
|||
46 |
|
|
L |
S,V |
||||||
78 |
|
|
L |
V,S |
||||||
110 |
|
|
L |
V,V |
||||||
206 |
|
|
V |
? |
||||||
2OP |
15 |
15 |
|
|
L |
S,S |
* |
|
||
47 |
|
|
L |
S,V |
||||||
79 |
|
|
L |
V,S |
||||||
111 |
|
|
L |
V,V |
||||||
207 |
|
|
V |
? |
||||||
Count |
Num |
Dec. |
Hex |
Binary |
Form |
Args |
V |
St |
Br |
Instruction and syntax |
2OP |
16 |
16 |
|
|
L |
S,S |
* |
|
||
48 |
|
|
L |
S,V |
||||||
80 |
|
|
L |
V,S |
||||||
112 |
|
|
L |
V,V |
||||||
208 |
|
|
V |
? |
||||||
2OP |
17 |
17 |
|
|
L |
S,S |
* |
|
||
49 |
|
|
L |
S,V |
||||||
81 |
|
|
L |
V,S |
||||||
113 |
|
|
L |
V,V |
||||||
209 |
|
|
V |
? |
||||||
2OP |
18 |
18 |
|
|
L |
S,S |
* |
|
||
50 |
|
|
L |
S,V |
||||||
82 |
|
|
L |
V,S |
||||||
114 |
|
|
L |
V,V |
||||||
210 |
|
|
V |
? |
||||||
2OP |
19 |
19 |
|
|
L |
S,S |
* |
|
||
51 |
|
|
L |
S,V |
||||||
83 |
|
|
L |
V,S |
||||||
115 |
|
|
L |
V,V |
||||||
211 |
|
|
V |
? |
||||||
2OP |
20 |
20 |
|
|
L |
S,S |
* |
|
||
52 |
|
|
L |
S,V |
||||||
84 |
|
|
L |
V,S |
||||||
116 |
|
|
L |
V,V |
||||||
212 |
|
|
V |
? |
||||||
2OP |
21 |
21 |
|
|
L |
S,S |
* |
|
||
53 |
|
|
L |
S,V |
||||||
85 |
|
|
L |
V,S |
||||||
117 |
|
|
L |
V,V |
||||||
213 |
|
|
V |
? |
||||||
2OP |
22 |
22 |
|
|
L |
S,S |
* |
|
||
54 |
|
|
L |
S,V |
||||||
86 |
|
|
L |
V,S |
||||||
118 |
|
|
L |
V,V |
||||||
214 |
|
|
V |
? |
||||||
2OP |
23 |
23 |
|
|
L |
S,S |
* |
|
||
55 |
|
|
L |
S,V |
||||||
87 |
|
|
L |
V,S |
||||||
119 |
|
|
L |
V,V |
||||||
215 |
|
|
V |
? |
||||||
2OP |
24 |
24 |
|
|
L |
S,S |
* |
|
||
56 |
|
|
L |
S,V |
||||||
88 |
|
|
L |
V,S |
||||||
120 |
|
|
L |
V,V |
||||||
216 |
|
|
V |
? |
||||||
2OP |
25 |
25 |
|
|
L |
S,S |
4 |
* |
|
|
57 |
|
|
L |
S,V |
||||||
89 |
|
|
L |
V,S |
||||||
121 |
|
|
L |
V,V |
||||||
217 |
|
|
V |
? |
||||||
2OP |
26 |
26 |
|
|
L |
S,S |
5 |
|
||
58 |
|
|
L |
S,V |
||||||
90 |
|
|
L |
V,S |
||||||
122 |
|
|
L |
V,V |
||||||
218 |
|
|
V |
? |
||||||
2OP |
27 |
27 |
|
|
L |
S,S |
5 |
|
||
59 |
|
|
L |
S,V |
6 |
|
||||
91 |
|
|
L |
V,S |
||||||
123 |
|
|
L |
V,V |
||||||
219 |
|
|
V |
? |
||||||
2OP |
28 |
28 |
|
|
L |
S,S |
5/6 |
|
||
60 |
|
|
L |
S,V |
||||||
92 |
|
|
L |
V,S |
||||||
124 |
|
|
L |
V,V |
||||||
220 |
|
|
V |
? |
||||||
2OP |
29 |
29 |
|
|
L |
S,S |
― |
― |
― |
― |
61 |
|
|
L |
S,V |
||||||
93 |
|
|
L |
V,S |
||||||
125 |
|
|
L |
V,V |
||||||
221 |
|
|
V |
? |
||||||
2OP |
30 |
30 |
|
|
L |
S,S |
― |
― |
― |
― |
62 |
|
|
L |
S,V |
||||||
94 |
|
|
L |
V,S |
||||||
126 |
|
|
L |
V,V |
||||||
222 |
|
|
V |
? |
||||||
2OP |
31 |
31 |
|
|
L |
S,S |
― |
― |
― |
― |
63 |
|
|
L |
S,V |
||||||
95 |
|
|
L |
V,S |
||||||
127 |
|
|
L |
V,V |
||||||
223 |
|
|
V |
? |
||||||
VAR opcodes |
||||||||||
Count |
Num |
Dec. |
Hex |
Binary |
Form |
Args |
V |
St |
Br |
Instruction and syntax |
VAR |
0 |
224 |
|
|
V |
? |
1 |
* |
|
|
4 |
|
|||||||||
VAR |
1 |
225 |
|
|
V |
? |
|
|||
VAR |
2 |
226 |
|
|
V |
? |
|
|||
VAR |
3 |
227 |
|
|
V |
? |
|
|||
VAR |
4 |
228 |
|
|
V |
? |
1 |
|
||
4 |
|
|||||||||
5 |
* |
|
||||||||
VAR |
5 |
229 |
|
|
V |
? |
|
|||
VAR |
6 |
230 |
|
|
V |
? |
|
|||
VAR |
7 |
231 |
|
|
V |
? |
* |
|
||
VAR |
8 |
232 |
|
|
V |
? |
|
|||
VAR |
9 |
233 |
|
|
V |
? |
1 |
|
||
6 |
* |
|
||||||||
VAR |
10 |
234 |
|
|
V |
? |
3 |
|
||
VAR |
11 |
235 |
|
|
V |
? |
3 |
|
||
VAR |
12 |
236 |
|
|
V |
? |
4 |
* |
|
|
VAR |
13 |
237 |
|
|
V |
? |
4 |
|
||
VAR |
14 |
238 |
|
|
V |
? |
4/- |
|
||
6 |
|
|||||||||
VAR |
15 |
239 |
|
|
V |
? |
4 |
|
||
6 |
|
|||||||||
VAR |
16 |
240 |
|
|
V |
? |
4/6 |
|
||
VAR |
17 |
241 |
|
|
V |
? |
4 |
|
||
VAR |
18 |
242 |
|
|
V |
? |
4 |
|
||
VAR |
19 |
243 |
|
|
V |
? |
3 |
|
||
5 |
|
|||||||||
6 |
|
|||||||||
VAR |
20 |
244 |
|
|
V |
? |
3 |
|
||
VAR |
21 |
245 |
|
|
V |
? |
5/3 |
|
||
VAR |
22 |
246 |
|
|
V |
? |
4 |
* |
|
|
VAR |
23 |
247 |
|
|
V |
? |
4 |
* |
* |
|
VAR |
24 |
248 |
|
|
V |
? |
5/6 |
* |
|
|
VAR |
25 |
249 |
|
|
V |
? |
5 |
|
||
VAR |
26 |
250 |
|
|
V |
? |
5 |
|
||
VAR |
27 |
251 |
|
|
V |
? |
5 |
|
||
VAR |
28 |
252 |
|
|
V |
? |
5 |
|
||
VAR |
29 |
253 |
|
|
V |
? |
5 |
|
||
VAR |
30 |
254 |
|
|
V |
? |
5 |
|
||
VAR |
31 |
255 |
|
|
V |
? |
5 |
* |
|
|
EXT opcodes |
||||||||||
Count |
Num |
Dec. |
Hex |
Binary |
Form |
Args |
V |
St |
Br |
Instruction and syntax |
EXT |
― |
190 |
|
|
E |
Extended opcode sentinel value |
||||
EXT |
0 |
0 |
|
|
E |
? |
5 |
* |
|
|
EXT |
1 |
1 |
|
|
E |
? |
5 |
* |
|
|
EXT |
2 |
2 |
|
|
E |
? |
5 |
* |
|
|
EXT |
3 |
3 |
|
|
E |
? |
5/- |
* |
|
|
EXT |
4 |
4 |
|
|
E |
? |
5 |
* |
|
|
6/- |
* |
|
||||||||
EXT |
5 |
5 |
|
|
E |
? |
6 |
|
||
EXT |
6 |
6 |
|
|
E |
? |
6 |
* |
|
|
EXT |
7 |
7 |
|
|
E |
? |
6 |
|
||
EXT |
8 |
8 |
|
|
E |
? |
6 |
|
||
EXT |
9 |
9 |
|
|
E |
? |
5 |
* |
|
|
EXT |
10 |
10 |
|
|
E |
? |
5 |
* |
|
|
EXT |
11 |
11 |
|
|
E |
? |
5/* |
|
||
EXT |
12 |
12 |
|
|
E |
? |
5/* |
|
||
EXT |
13 |
13 |
|
|
E |
? |
5/* |
|
||
6/* |
|
|||||||||
EXT |
― |
14 |
|
|
E |
? |
― |
|||
EXT |
― |
15 |
|
|
E |
? |
― |
|||
EXT |
16 |
16 |
|
|
E |
? |
6 |
|
||
EXT |
17 |
17 |
|
|
E |
? |
6 |
|
||
EXT |
18 |
18 |
|
|
E |
? |
6 |
|
||
EXT |
19 |
19 |
|
|
E |
? |
6 |
* |
|
|
EXT |
20 |
20 |
|
|
E |
? |
6 |
|
||
EXT |
21 |
21 |
|
|
E |
? |
6 |
|
||
EXT |
22 |
22 |
|
|
E |
? |
6 |
|
||
EXT |
23 |
23 |
|
|
E |
? |
6 |
|
||
EXT |
24 |
24 |
|
|
E |
? |
6 |
* |
|
|
EXT |
25 |
25 |
|
|
E |
? |
6 |
|
||
EXT |
26 |
26 |
|
|
E |
? |
6 |
|
||
EXT |
27 |
27 |
|
|
E |
? |
6 |
* |
|
|
EXT |
28 |
28 |
|
|
E |
? |
6 |
|
||
EXT |
29 |
29 |
|
|
E |
? |
6/* |
* |
|