taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.4.3.1-6

Exercise 1.4.3.1-6

not stated yet in Lean solution written

Rating28
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.4.3.1 of Volume 1. Companion chapter: A MIX simulator

The task

Write the operator routines JBUS, IOC, IN, OUT and JRED for the simulator, for the units 16 (card reader) and 18 (printer) only. The operations read-card and skip-to-new-page take T=10000T = 10000 units, and print-line takes T=7500T = 7500 units.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Keep for each unit the time at which it becomes ready. Then busy means that this time is later than the simulated clock.

Hint 2

When an instruction needs a busy device, the machine waits. In the simulator this means that the clock is advanced to the ready time.

Hint 3

The instruction JBUS * is a special case: if the test is on the location of the instruction itself, one does not loop, but moves the clock forward.

Hint 4

Use the real card reader and printer for the real transmission. The simulated memory is real, so the address MM can be used as it is.

Hint 5

An address in an instruction has two bytes, so a constant like 10000 must be in a memory cell.

Solution

Show the solution

Model. A unit uu is busy from the start of an IN, OUT or IOC for TT units. The routines keep the time when the unit is free in the cells FREE and FREE+1, for the units 16 and 18. An operation that is started at a time SS makes the unit free at S+TS + T, and if the unit is still busy, the instruction waits and SS is the time at which the unit is free again. The simulator takes the clock at the start of the instruction. The one unit of time that the instruction takes is added by the control routine, as for the other operators.

Common routines. UNIT sets rI1 to 0 for unit 16 and to 1 for unit 18, and jumps to UNITERROR for any other unit. WAIT advances the clock to the free time of the unit. START puts the time CLOCK+T\text{CLOCK} + T into the free cell, with TT in rA.

UNIT    STJ  9F
        ENT1 0
        DEC3 16
        J3Z  9F             Unit 16: rI1 = 0.
        DEC3 2
        J3NZ UNITERROR      Not unit 18 either.
        ENT1 1
9H      JMP  *
WAIT    STJ  9F
        LDA  CLOCK
        CMPA FREE,1
        JGE  9F             Unit is free already.
        LDA  FREE,1
        STA  CLOCK          Wait until it is free.
9H      JMP  *
START   STJ  9F
        ADD  CLOCK
        STA  FREE,1         The unit is free again at CLOCK + T.
9H      JMP  *
FREE    CON  0
        CON  0
T10000  CON  10000
T7500   CON  7500

The operators:

JBUS    JMP  UNIT
        LDA  CLOCK
        CMPA FREE,1
        JGE  CYCLE          Not busy: no jump.
        ENTA 0,6
        DECA 0,5
        DECA 1              rA = 0 if M is the address of this instruction.
        JANZ JMP            An ordinary jump.
        LDA  FREE,1         JBUS * : let the time pass until the unit is free.
        STA  CLOCK
        ST6  JREG           The jump sets rJ, so do it here.
        JMP  CYCLE
JRED    JMP  UNIT
        LDA  CLOCK
        CMPA FREE,1
        JL   CYCLE          Busy: no jump.
        JMP  JMP            Jump with the routine of the text.
IOC     JMP  UNIT
        J1Z  UNITERROR      Only the printer has IOC.
        J5NZ ADDRERROR      M must be zero.
        JMP  WAIT
        IOC  0(18)
        LDA  T10000
        JMP  START
        JMP  CYCLE
IN      JMP  UNIT
        J1NZ UNITERROR      Only the card reader reads.
        JMP  WAIT
        JMP  MEMORY         Check the first address.
        ENT2 15,5
        CMP2 BEGINC
        JGE  MEMERROR       The last of 16 words must be in memory.
        IN   0,5(16)
        JBUS *(16)
        LDA  T10000
        JMP  START
        JMP  CYCLE
OUT     JMP  UNIT
        J1Z  UNITERROR      Only the printer writes.
        JMP  WAIT
        JMP  MEMORY
        ENT2 23,5
        CMP2 BEGINC
        JGE  MEMERROR       The last of 24 words must be in memory.
        OUT  0,5(18)
        JBUS *(18)
        LDA  T7500
        JMP  START
        JMP  CYCLE

The label JMP in JBUS and JRED is the entry of the routine of the text that makes the jump: it stores the location counter in the simulated rJ, and then goes to JSJ, which checks the address. The special case JBUS * needs no loop: the time to wait is known, so the clock is moved at once. Without this, the simulated loop would run for 10000 simulated cycles. The real JBUS *(16) after the real IN waits only for the real device, and it does not change the simulated time.

Check

Hand trace. IN 1000(16) at time 0 makes unit 16 free at 10000. The next instruction JBUS *(16) is a special case. It moves the clock to 10000, sets the simulated rJ, and takes 1 unit. So the time is 10001. A MIX simulator ran the simulator of the text with these routines, and the same simulated programs ran directly on the host. In eight programs with IN, OUT, IOC, JBUS and JRED, which wait for one another, the lines that were printed, the memory, the simulated rJ and the final clock were the same: for example 1000110001 for the program above, 75017501 for two OUT instructions in a row, and 2750527505 for a longer program.

Remarks

This model starts the count of TT at the time when the operation starts. The ready time has no other use in the simulator. The routine does not load a simulated memory area for the card reader if the card file is empty; this must be added, if the case can happen.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.