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.3.1-20

Exercise 1.3.1-20

not stated yet in Lean solution written

Rating20
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.3.1 of Volume 1. Companion chapter: Description of MIX

The task

Write a program that puts a HLT into all 4000 cells and then stops.

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

Use MOVE with the source cell fixed and a block size that divides 39993999.

Hint 2

The program itself is in the memory, so the last copy overwrites the program. Make the last copy place a HLT where the next instruction would be.

Solution

Show the solution

Plan: put HLT into cell 0, then spread it by MOVE in blocks of 4343 words. Because 3999=93⋅433999 = 93 \cdot 43, the block count is exact. The program is put at the end of memory, and the last block overwrites it.

        ORIG 3995
START   ENT1 0
        MOVE 3999
LOOP    MOVE 0(43)
        JMP  LOOP
        HLT

The word in cell 3999 is the HLT. ENT1 0 sets the target, and MOVE 3999 copies the HLT into cell 0 and makes rI1 equal to 11. Each MOVE 0(43) copies the cells 0,…,420, \dots, 42 into the cells that start at rI1. After the first copy, the cells 00 to 4343 hold HLT, and in general after kk copies the cells 00 to 43k43k hold HLT. The first MOVE 0(43) copies cell 0 to cell 1, then cell 1 (just written) to cell 2, and so on, so that the cells 11 to 4343 all get HLT. After 9393 copies, rI1 is 1+93⋅43=40001 + 93 \cdot 43 = 4000 and all cells 00 to 39993999 hold HLT. The last copy overwrites the cells 39573957 to 39993999, including the instruction JMP LOOP in cell 3998. The next instruction to be fetched is cell 3998, which is now HLT, and the machine stops. The time is 1+3+93⋅87+92=8187u1 + 3 + 93 \cdot 87 + 92 = 8187u, plus 1u1u for the final HLT.

Check

Simulator runs with b=64b = 64 and b=100b = 100, with all other cells set to a non-zero value at the start, ended with all 4000 cells equal to the HLT word (+0 0 0 2 5+0\,0\,0\,2\,5) and with time 8187u8187u and the HLT. The count in python: 3999 == 93 * 43. The other divisor of 39993999 that is at most 6363 is 3131 with 129129 copies, which takes 8260u8260u.

Remarks

The field of MOVE must be at most 6363, so only 3131 and 4343 are possible block sizes. The choice 4343 is faster.

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.