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

Exercise 1.3.1-16

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 MIX program that clears the cells 0000 to 0099, first as short as possible and then as fast as possible.

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 copies a word from MM to the cell that rI1 names, and then rI1 grows. If the source is the cell just before the target, the copy spreads the first word.

Hint 2

The field of MOVE is the number of words. It must fit in a byte of any size from 64, so it cannot be more than 63.

Hint 3

For the fastest version count the time that each word needs by itself.

Solution

Show the solution

Short program. Put a zero in cell 0, set rI1 to 1, and copy forward twice:

        STZ  0
        ENT1 1
        MOVE 0(49)
        MOVE 0(50)

The first MOVE copies cells 0,1,…,480, 1, \dots, 48 into cells 1,2,…,491, 2, \dots, 49 one word after the other, so every target word receives a zero, because the source word was cleared a moment before. Now rI1 is 5050. The second MOVE copies the cells 0,…,490, \dots, 49 into 50,…,9950, \dots, 99. A single MOVE 0(99) would be shorter, but the field 9999 does not fit in a byte when b<100b < 100. The program has 44 words, and 33 do not suffice: the two zero-making and index-setting instructions are needed, and one MOVE moves at most 6363 words. The time is

2+1+(1+2⋅49)+(1+2⋅50)=203u.2 + 1 + (1 + 2 \cdot 49) + (1 + 2 \cdot 50) = 203u.

Fast program. A word cannot be stored in less than 2u2u, by STZ or by the MOVE of one word. So 100100 cells need at least 200u200u and no program is faster than that. The sequence STZ 0, STZ 1, ..., STZ 99 takes exactly 200u200u. It has 100100 words. It is the fastest program. The MOVE form needs three more uu.

Check

The two programs ran in a simulator with b=64b = 64 and b=100b = 100. All memory cells 00 to 9999 were set to a non-zero word first. Both leave the cells 00 to 9999 at +0+0 and cell 100100 unchanged. The times are 203u203u and 200u200u (without the HLT).

Remarks

Exercise 1.3.1-17 asks the same for a number of cells that is not fixed. Exercise 1.3.1-20 clears all of memory.

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.