TAOCP 1.3.1 Exercise 17

The proposal’s failure comes from two independent MIX semantics issues: (i) `MOVE` cannot broadcast from a single word, and (ii) index registers cannot be compared directly.

Section 1.3.1: Description of MIX

Exercise 17. [26] This is the same as the previous exercise, except that locations 0000 through N, inclusive, are to be set to zero, where N is the current contents of rI2. Your programs (a) and (b) should work for any value 0 \le N \le 2999; they should start in location 3000.

Verified: yes
Solve time: 1m55s


The proposal’s failure comes from two independent MIX semantics issues: (i) MOVE cannot broadcast from a single word, and (ii) index registers cannot be compared directly. Once both are repaired, the exercise reduces to a standard minimal loop construction.

1. Correct interpretation of the task

We must set all memory locations

$$ 0,1,\dots,N $$

to zero, where $0 \le N \le 2999$, and initially register $rI2 = N$. The program begins at location $3000$.

A key constraint is that MIX has no vectorized “fill” instruction. Any correct solution must therefore either:

  • explicitly write each location, or
  • use a valid block-transfer whose source already contains the correct data.

The second option fails because no block of $N+1$ zeros is initially available. Thus any correct solution ultimately reduces to a loop that performs $N+1$ stores.

2. Part (a): shortest program

Why the proposed MOVE approach is invalid

The earlier solution assumes that a single zero word at Z can be expanded using:

$$ \texttt{MOVE 0,2} $$

This is impossible in MIX.

MOVE M, I(F) copies M consecutive words from a source block. It does not replicate a single word into a longer region. Therefore, without an already-prepared block of length $N+1$, MOVE cannot generate the required data.

So any correct shortest solution cannot rely on MOVE for broadcasting.

Correct minimal strategy

The shortest valid construction is a tight loop that:

  • initializes an index register to $0$,
  • stores zero into each location,
  • compares against $N$,
  • stops after reaching $N$.

We must also fix the comparison issue: MIX cannot compare registers directly. Instead, we copy $rI2$ into memory using STX2 and compare against that memory location.

Program (start at 3000)

        ENT1  0            ; rI1 ← 0
        LDA   ZERO         ; rA ← 0
        STX2  NVAL         ; NVAL ← rI2

LOOP    STA   0,1          ; M[rI1] ← 0
        CMP1  NVAL         ; compare rI1 with N
        JEQ   DONE

        INC1  1            ; rI1 ← rI1 + 1
        JMP   LOOP

DONE    HLT

ZERO    CON   0
NVAL    CON   0

Correctness

Invariant after $k$ iterations:

  • $rI1 = k$
  • locations $0,1,\dots,k-1$ are zero

At iteration $k$, STA 0,1 writes location $k$. If $k = N$, then CMP1 NVAL succeeds immediately after storing $N$, and the program halts. Thus exactly the range $0..N$ is written.

Why this is shortest

Any correct program must contain at least:

  • one instruction to initialize the writing value (or guarantee zero in $rA$),
  • one loop control mechanism,
  • one store per memory cell.

This loop uses the minimal MIX mechanism for all three requirements without introducing any redundant structure such as MOVE, which cannot legally solve the problem.

3. Part (b): fastest program

Lower bound

Each of the $N+1$ memory locations must be explicitly written. MIX provides no bulk zero-fill instruction. Therefore any program requires at least:

$$ N+1 \text{ executions of } \texttt{STA} $$

No asymptotic improvement is possible.

Optimality of the same loop

The loop above already achieves:

  • exactly one STA per memory cell,
  • one increment per iteration,
  • one comparison per iteration,
  • no extra overhead per cell.

No instruction in MIX can eliminate any of these while preserving correctness, because:

  • removing INC1 prevents traversal,
  • removing comparison prevents termination,
  • replacing STA requires an even more expensive memory operation,
  • MOVE cannot apply since it needs a preinitialized block of correct length.

Thus the loop is optimal in execution time up to constant per-iteration cost.

Final program (same as part a)

The fastest solution is identical:

        ENT1  0
        LDA   ZERO
        STX2  NVAL

LOOP    STA   0,1
        CMP1  NVAL
        JEQ   DONE
        INC1  1
        JMP   LOOP

DONE    HLT

ZERO    CON   0
NVAL    CON   0

4. Summary of corrections

  • The MOVE-based solution was invalid because MIX MOVE does not broadcast a single word.
  • Register-to-register comparison was impossible; corrected using STX2 and CMP1 with memory.
  • Both parts reduce to the same minimal correct loop.
  • Optimality follows from a strict lower bound of $N+1$ required stores.