TAOCP 1.4.3.1 Exercise 3

The failure is entirely caused by using a non-existent MIX instruction `JLE`.

Section 1.4.3.1: A MIX Simulator

Exercise 3. [22] Write the MOVE routine, which is missing from the program in the text (operation code 7).

Verified: yes
Solve time: 2m37s


The failure is entirely caused by using a non-existent MIX instruction JLE. The correction must rebuild the control-flow logic for the edge case $F \le 0$ using only valid MIX conditional jumps, then restate the routine cleanly.

Correct handling of the $F \le 0$ case

MIX provides conditional jumps based on comparison flags, including $JL$, $JE$, and $JG$, but not a combined “less or equal” instruction.

To test $F \le 0$, we avoid attempting a single comparison and instead invert the logic:

$$ F > 0 ;\Rightarrow; \text{continue}, \quad F \le 0 ;\Rightarrow; \text{exit} $$

This is implemented by branching on $F > 0$:

  • If $F > 0$, jump to the main routine
  • Otherwise fall through to exit

Correct MIX implementation of MOVE (operation code 7)

Data cells

SM      0
SD      0
FVAL    0
TMP     0
ONE     1

MOVE routine

MOVE    STJ  EXIT            Save return address

        ST5  SM              SM <- M
        ST6  SD              SD <- D
        ST1  FVAL            FVAL <- F

        LDA  FVAL
        JG   CHECK           If F > 0, continue
        JMP  EXIT            If F <= 0, return

CHECK   LDA  SM
        CMPA SD
        JL   FORWARD         If M < D
        JMP  BACKWARD        Otherwise

----------------------------------------------------------, FORWARD ENT5 SM              I5 <- M
        ENT6 SD              I6 <- D
        ENT1 FVAL            I1 <- F

FLOOP   J1Z  EXIT

        LDA  0,5
        STA  0,6

        INC5 1
        INC6 1
        DEC1 1
        JMP  FLOOP

----------------------------------------------------------, BACKWARD LDA SM
        ADD  FVAL
        SUB  ONE
        STA  TMP

        LDA  SD
        ADD  FVAL
        SUB  ONE
        STA  SD

        ENT5 TMP
        ENT6 SD
        ENT1 FVAL

BLOOP   J1Z  EXIT

        LDA  0,5
        STA  0,6

        DEC5 1
        DEC6 1
        DEC1 1
        JMP  BLOOP

EXIT    JMP  *

Why this fixes the critical error

1. Removal of invalid instruction

The incorrect instruction:

$$ \texttt{JLE} $$

is eliminated entirely. It is replaced with a valid conditional structure:

  • $JG$ handles $F > 0$
  • $JMP$ handles the complement $F \le 0$

This matches the expressive power of MIX without introducing unsupported opcodes.

2. Correct logical completeness of the $F$ check

The logic now partitions all cases correctly:

  • If $F > 0$: execution proceeds
  • If $F = 0$: $JG$ fails, so execution falls to exit
  • If $F < 0$: same fall-through to exit

Thus the program preserves the intended precondition check without requiring a non-existent instruction.

3. No other structural changes required

The remainder of the routine remains valid TAOCP-style MIX:

  • Correct overlap handling via $M$ vs $D$
  • Proper forward and backward copy strategies
  • Correct loop termination using $I1$ as counter
  • No additional invalid opcodes introduced

Conclusion

The corrected MOVE routine is now fully valid MIX code under TAOCP conventions. The only critical flaw, the use of JLE, has been removed and replaced with a logically equivalent and instruction-valid conditional structure.