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-3

Exercise 1.4.3.1-3

not stated yet in Lean solution written

Rating22
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 routine MOVE for operation code 7 of the simulator.

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

The instruction MOVE M(F) copies FF words from the locations M,M+1,…M, M+1, \ldots to the locations rI1,rI1+1,…\text{rI1}, \text{rI1}+1, \ldots, one word at a time in this order. At the end rI1 has grown by FF. The time is 1+2F1 + 2F.

Hint 2

Because the simulated memory is part of the real memory, a word can be copied with one load and one store. The sign does not need special care.

Hint 3

Check every address that is used. The destination is given by the simulated rI1, kept as a magnitude and a sign.

Hint 4

The table of the text gives 1 unit of time for MOVE. The 2F2F units remain to be added.

Solution

Show the solution

If F=0F = 0, nothing is done, and rI1 stays. Otherwise load the magnitude of the simulated rI1 in the real rI1. If its sign is minus and the magnitude is not zero, the address is not valid. Then do FF times: check the source (with MEMORY), check the destination, copy the word, add 22 to the simulated clock, and add 11 to both pointers. At the end store the new value of rI1, with sign plus.

MOVE    J3Z  CYCLE          F = 0: nothing to do.
        LD1  I1REG          Magnitude of the simulated rI1.
        LDA  SIGN1
        JAP  1F
        J1NZ MEMERROR       Negative and not zero.
1H      JMP  MEMORY         Check the source address in rI5.
        CMP1 BEGINC         BEGINC holds the value of BEGIN.
        JGE  MEMERROR       Destination outside the memory.
        LDA  0,5
        STA  0,1            Copy one word, with its sign.
        LDA  CLOCK
        INCA 2
        STA  CLOCK          Two more units of time.
        INC1 1
        INC5 1
        DEC3 1
        J3P  1B
        ST1  I1REG
        ENTA 1
        STA  SIGN1          The new rI1 has the sign plus.
        JMP  CYCLE

When the source and the destination overlap, the words are copied in increasing order, and so the copy has the same effect as the hardware: a source word that has been overwritten earlier is copied again with its new value. The simulated value of rI1 can be at most BEGIN at the end, because each destination was checked.

The sign of the new rI1: if the old rI1 was −0-0, the sum −0+F-0 + F is positive. So storing the sign plus is right for every case with F≥1F \ge 1.

Check

Three hundred random cases ran on a MIX simulator, with F∈{0,1,2,3,5,17,40}F \in \{0, 1, 2, 3, 5, 17, 40\}, index fields 00, 11 and 22, random signed words and different destinations, some of them overlapping the source. The memory and the simulated rI1 were the same as after the real MOVE. The simulated clock after MOVE 1000(F) was 1,3,11,351, 3, 11, 35 for F=0,1,5,17F = 0, 1, 5, 17 (the formula 1+2F1 + 2F).

Remarks

The check of the source in MEMORY is repeated for every word. This is slower than a check of the first and the last word, but it gives the exact place of the error and it stops the move at the same word as the hardware would try to read. The time of the simulation does not matter for the simulated clock.

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.