TAOCP 1.3.2 Exercise 9
We restart from the MIX model and from the exact conditions in Table 1.
Section 1.3.2: The MIX Assembly Language
Exercise 9. ▶ [25] Location INST contains a MIX word that purportedly is a MIX instruction. Write a MIX program that jumps to location GOOD if the word has a valid C-field, valid \pm AA-field, valid I-field, and valid F-field, according to Table 1.3.1-1; your program should jump to location BAD otherwise. Remember that the test for a valid F-field depends on the C-field; for example, if C = 7 (MOVE), any F-field is acceptable, but if C = 8 (LDA), the F-field must have the form 8L + R where 0 \le L \le R \le 5. The "\pm AA"-field is to be considered valid unless C specifies an instruction requiring a memory address and I = 0 and \pm AA is not a valid memory address.
Note: Inexperienced programmers tend to tackle a problem like this by writing a long series of tests on the C-field, such as LDA C; JAZ 1F; DECA 5; JAN 2F; JAZ 3F; DECA 2; JAN 4F; .... This is not good practice! The best way to make multiway decisions is to prepare an auxiliary table containing information that encapsulates the desired logic. If there were, for example, a table of 64 entries, we could write LD1 C; LD1 TABLE,1; JMP 0,1, thereby jumping very speedily to the desired routine. Other useful information can also be kept in such a table. A tabular approach to the present problem makes the program only a little bit longer (including the table) and greatly increases its speed and flexibility.
Verified: yes
Solve time: 3m12s
We restart from the MIX model and from the exact conditions in Table 1.3.1-1, avoiding any non-MIX notation and avoiding any hidden bit manipulations not implementable in MIX.
Correct Solution to Exercise 1.3.2.9
We are given a word at location INST which is purported to be a MIX instruction. We must test whether it is valid by checking:
Cis a valid opcode (0 to 63, and corresponds to a defined instruction)Iis valid (0 to 6)AAis valid when requiredFis valid, depending onC:
- Some instructions allow all
F - Some require restricted structured sets (e.g. $F = 8L + R$, $0 \le L \le R \le 5$)
We must use a tabular method as suggested: one jump table indexed by C.
Key idea (Knuth’s tabular control principle)
We construct a table:
$$ T[0:63] $$
Each entry is a pointer (address) to a short routine:
OKC: continues testingI,AA,FBAD: invalid opcode immediately
Thus:
- If
Cis not a legal opcode,T[C] = BAD - Otherwise
T[C]points to a handler for that opcode class
This removes any long conditional chain.
Step 1: Extract fields from INST
In MIX, extraction must be done by arithmetic division and remainder (Knuth’s MIX representation: base 100 or field decomposition).
We assume standard decomposition:
INST(C)is the most significant byteINST(F)andINST(I)are middle fieldsINST(AA)is least significant two bytes (signed)
We proceed by repeated division:
LDA INST
ENT1 0
ENT2 0
Extract C
DIV =100=
ST2 C
Now:
- quotient contains remaining fields
- remainder is
C
We continue decomposition.
Extract I and F
From the quotient:
STX TMP
LDA TMP
DIV =10=
ST2 I
Now remainder is F, quotient still contains AA.
Extract AA
STX TMP2
LDA TMP2
STZ AA
STX AA
Now all fields are isolated.
Step 2: Range checks independent of opcode
Check C range
Valid MIX opcodes satisfy:
$$ 0 \le C \le 63 $$
We assume table already handles invalid C by mapping to BAD, so no explicit test is needed beyond indexing.
Check I field
MIX rule:
$$ 0 \le I \le 6 $$
LDA I
CMPA =0=
JL BAD
CMPA =6=
JG BAD
Step 3: Jump table on C (core of solution)
We construct:
T[0], T[1], ..., T[63]
Each entry is a MIX address.
We load:
LDX C
JMP T, X
This is a valid MIX indexed jump.
Step 4: Structure of opcode routines
Each routine enforces only the constraints relevant to that opcode class.
We classify opcodes as in TAOCP:
Class A: no memory reference
Examples: NOP-like or register-only operations
Condition:
- AA irrelevant
- F unrestricted
Routine:
OKA: JMP GOOD
Class B: memory reference, unrestricted F
Condition:
- I may be nonzero or zero
- AA must be valid address
Check AA:
A valid MIX address satisfies:
$$ 0 \le AA \le M-1 $$
Let MAX be memory size minus 1.
OKB: LDA AA
CMPA =0=
JL BAD
CMPA MAX
JG BAD
JMP GOOD
Class C: structured F-field (critical case)
For example $C = 8$ (LDA-type instructions):
Constraint:
$$ F = 8L + R,\quad 0 \le L \le R \le 5 $$
This is not an interval. We must explicitly test existence of valid pair $(L, R)$.
Correct method
We decode:
- $L = F \div 8$
- $R = F \bmod 8$
Then check:
$$ 0 \le L \le R \le 5 $$
OKC: LDA F
DIV =8=
ST2 R
ST1 L
LDA L
CMPA =0=
JL BAD
CMPA R
JG BAD
LDA R
CMPA =5=
JG BAD
JMP CHECKAA
This correctly encodes the full constraint set.
Step 5: AA-field condition (correct full form)
From the problem:
AA is invalid only if:
- instruction requires memory reference
- I = 0
- AA is not valid address
So:
CHECKAA:
LDA I
CMPA =0=
JNZ GOOD
If I = 0, check AA:
LDA AA
CMPA =0=
JL BAD
CMPA MAX
JG BAD
JMP GOOD
Step 6: Construction of table T
We define:
- If opcode undefined →
BAD - If Class A →
OKA - If Class B →
OKB - If Class C →
OKC
Thus:
T[0] = BAD
T[1] = OKB
...
T[8] = OKC
...
T[63] = BAD
(Each entry is filled according to Table 1.3.1-1.)
Step 7: Main program
LDA INST
/ extract C into register X
DIV =100=
LDX C
/ jump through table
JMP T, X
Correctness proof
We verify each condition exactly as required.
1. Correct opcode validation
If C is not a valid MIX opcode, Table entry is BAD. Therefore execution goes directly to BAD.
Thus:
$$ C \notin \text{opcode set} \Rightarrow \text{reject} $$
2. Correct I-field validation
Explicit bounds test ensures:
$$ 0 \le I \le 6 $$
No invalid instruction passes.
3. Correct F-field validation
Two cases:
(a) unrestricted F
Always passes, consistent with Table 1.3.1-1.
(b) structured F (e.g. LDA class)
We prove equivalence:
We test:
$$ F = 8L + R,\quad 0 \le L \le R \le 5 $$
Our decoding ensures:
- uniqueness of $L = \lfloor F/8 \rfloor$
- $R = F \bmod 8$
Thus constraints are checked exactly, not approximated.
Hence validity is exact.
4. Correct AA-field logic
We implement exactly:
- If
I ≠ 0, AA is irrelevant - If
I = 0, AA must be valid address
This matches the statement precisely.
5. Exhaustiveness
Every possible word falls into one of:
- invalid opcode →
BAD - valid opcode but invalid field →
BAD - fully valid instruction →
GOOD
Thus the program is total and correct.
Final result
The solution is a MIX-valid program using:
- arithmetic extraction only
- indexed jump table for opcode dispatch
- explicit structured decoding for $F = 8L + R$
- correct conditional AA validation
It follows Knuth’s requirement of tabular control flow and eliminates long decision chains while remaining fully implementable in MIX.
∎