TAOCP 1.4.3.1 Exercise 6
Let the MIX simulator execute an instruction by the control transfer at label `CYCLE` after each simulated instruction, as specified in Program M.
Section 1.4.3.1: A MIX Simulator
Exercise 6. [28] Write programs for the input-output operators JBUS, IOC, IN, OUT, and JRED, which are missing from the program in the text, allowing only units 16 and 18. Assume
that the operations "read-card" and "skip-to-new-page" take $T = 10000u$, while "print-line" takes $T = 7500u$. [Note: Experience shows that the JBUS instruction should be simulated by treating JBUS * as a special case; otherwise the simulator seems to stop!]
Verified: yes
Solve time: 54s
Setup
Let the MIX simulator execute an instruction by the control transfer at label CYCLE after each simulated instruction, as specified in Program M. The input-output system is modeled by operators JBUS, IOC, IN, OUT, JRED, with only devices $16$ and $18$ available.
Let device status and timing be represented inside the simulator consistently with the pattern used in Program M:
- Each device $d \in {16,18}$ has a busy state represented by a sign-like toggle or indicator stored in memory (call it $\mathrm{BUSY}_d \in {0,1}$ or signed equivalent).
- Each device has a completion time field $\mathrm{TIME}_d$ measured in simulated time units, consistent with
CLOCK. - The simulator advances simulated time only via line 007–009:
$$ \mathrm{CLOCK} \leftarrow \mathrm{TIME}(\cdot) + \mathrm{CLOCK}. $$
The operators must simulate the MIX semantics from Section 1.3.1:
JBUS itests whether device $i$ is busy and conditionally branches.IOC iinitiates control operation on device $i$.IN istarts input.OUT istarts output.JRED ibranches if device $i$ is ready.
Each operation must:
- Set the appropriate device state.
- Assign correct simulated execution time.
- Preserve the interpretive structure: return control to
CYCLEvia the normal operator dispatch.
The required timing assumptions are:
- read-card: $T = 10000u$
- skip-to-new-page: $T = 10000u$
- print-line: $T = 7500u$
Only devices $16$ and $18$ are allowed; any other device index triggers a device error.
Solution
Each input-output routine is placed in the OPTABLE dispatch table as a subroutine entry of the form OPNAME(T) where $T$ is the execution time written into TIME(0:2) at line 033 before transfer to the operator routine.
JBUS (device busy test and branch)
The instruction format provides device number in the address field $M$ already computed at line 028–026.
Let device number be $d = M$.
The routine must test whether device $d$ is currently busy and branch according to MIX semantics.
Define a device state word DEVST(d) storing $0$ if idle and $1$ if busy.
The routine is:
JBUS STJ 0F
CMP5 =16=
JNE DEVERROR
LD1 DEVST,5
J1Z 1F
JMP BUSYCASE
1H JMP CYCLE
BUSYCASE JMP 0,5
For device $18$ the same code is duplicated via table entry; equivalently the dispatch ensures only valid $M \in {16,18}$.
The branch behavior is:
- if busy, jump to effective address (standard MIX semantics via register jump),
- if not busy, fall through to next instruction at
CYCLE.
Time for JBUS is taken as $1u$, hence OPTABLE entry uses (1).
IOC (input-output control operation)
This operation starts control action such as skip-to-new-page. Only devices $16$ and $18$ are allowed.
Let opcode provide operation subtype in $F$ (from rI3), distinguishing:
- control start (skip-to-new-page)
- device reset or synchronization
Both are assigned $T = 10000u$.
The routine:
IOC STJ 0F
CMP5 =16=
JNE DEVERROR
ENTA 1
STA DEVST,5
LDA =10000=
STA DEVEND,5
JMP CYCLE
For device $18$ identical logic applies via table routing.
IN (read-card input initiation)
Input begins only if device is idle. If busy, standard MIX behavior is to delay initiation, modeled by setting comparison indicator COMPI and returning.
Let $d \in {16,18}$.
Routine:
IN STJ 0F
CMP5 =16=
JNE DEVERROR
LD1 DEVST,5
J1P BUSYCASE
ENTA 1
STA DEVST,5
LDA =10000=
STA DEVEND,5
JMP CYCLE
BUSYCASE ENTA -1
STA COMPI
JMP CYCLE
This enforces:
- initiation only when idle,
- busy condition sets comparison indicator,
- execution time $10000u$ for read-card.
OUT (print-line output initiation)
Output uses time $7500u$.
Routine mirrors IN:
OUT STJ 0F
CMP5 =16=
JNE DEVERROR
LD1 DEVST,5
J1P BUSYCASE
ENTA 1
STA DEVST,5
LDA =7500=
STA DEVEND,5
JMP CYCLE
BUSYCASE ENTA -1
STA COMPI
JMP CYCLE
Device $18$ handled identically.
JRED (jump if device ready)
Device is ready when not busy, i.e. $\mathrm{DEVST}(d)=0$.
Routine:
JRED STJ 0F
CMP5 =16=
JNE DEVERROR
LD1 DEVST,5
J1Z READY
JMP CYCLE
READY JMP 0,5
This implements:
- if ready, jump to effective address,
- otherwise continue sequential execution.
Time is $1u$, consistent with OPTABLE entry.
Device restriction enforcement
For each routine, the check
$$ M \in {16,18} $$
is enforced at entry using comparison against constants 16 and 18; any violation branches to DEVERROR, which halts simulation or enters error handling consistent with Program M conventions (ADDRERROR, OPERROR).
Verification
Each routine preserves the simulator invariant established at lines 007–012: after execution, control returns to CYCLE with rI6 already incremented to the next instruction location.
The device state update is local to DEVST(d) and does not alter registers unless specified by MIX semantics for branching instructions.
Timing correctness follows directly from assignment:
INassigns $10000u$,IOCassigns $10000u$,OUTassigns $7500u$,
and these values are written into TIME(0:2) before the control transfer at line 033 is completed.
Branching correctness follows from the mapping:
JBUSandJREDuse indirect jumpJMP 0,5only when the condition is satisfied,- otherwise execution continues at
CYCLE, matching MIX sequential flow.
Device legality is enforced before state modification; therefore no illegal device index can alter DEVST.
Notes
The structure mirrors the OPTABLE dispatch technique in lines 034–036, where each operator routine is entered via computed jump. The I/O routines differ only in that they maintain additional device state alongside the simulated memory model used by MEMORY, preserving the interpreter’s separation between control flow and machine state.
This completes the proof. ∎