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

Exercise 1.3.1-17

not stated yet in Lean solution written

Rating26
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 programs that clear the cells 0000 through NN for the number NN in rI2, with 0≤N≤29990 \le N \le 2999, 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

For the short program, use a loop that counts rI2 down to 00 and clears one cell for each value.

Hint 2

For the fast one, use MOVE 0(63) in a loop. The last block has fewer than 6363 words. The count of the last MOVE is part of the instruction, so the program must store that count into the instruction.

Hint 3

A zero in cell 00 must be there before the first copy.

Solution

Show the solution

Plan: the short version is a loop of one store, the fast version moves zeros in blocks of 6363 words, with the last count written into the program by itself.

Short program, starting at location 3000:

LOOP    STZ  0,2
        DEC2 1
        J2NN LOOP
        HLT

Each pass clears the cell NN, N−1N-1, ..., 00 in turn. The loop ends when rI2 becomes −1-1. The time is 4(N+1)u4(N+1)u without the HLT, and the program has 33 words and the HLT.

Fast program:

        STZ  0
        ENT1 1
        JMP  TEST
LOOP    MOVE 0(63)
TEST    DEC2 63
        J2P  LOOP
        INC2 63
        ST2  LAST(4:4)
LAST    MOVE 0
        HLT

After STZ 0 and ENT1 1, the cells 1,2,…1, 2, \dots are cleared in blocks of 6363 by MOVE 0(63), and rI1 grows by 6363 each time. The counter rI2 holds the number of cells still to be cleared, from the cell 11 on: it starts at NN. The test DEC2 63; J2P LOOP repeats the MOVE while more than 6363 cells remain. When the loop ends, INC2 63 gives rr with 1≤r≤631 \le r \le 63 if N≥1N \ge 1 and r=0r = 0 if N=0N = 0. The instruction ST2 LAST(4:4) writes rr into the field byte of the last MOVE, and this MOVE copies the last rr words. The value r≤63r \le 63 fits in a byte. Let q=⌈N/63⌉−1q = \lceil N/63 \rceil - 1 for N≥1N \ge 1, and q=0q = 0 for N=0N = 0. The number of full blocks is qq, and

r=N−63q.r = N - 63q.

The time is 4+129q+6+2r=2N+3q+104 + 129q + 6 + 2r = 2N + 3q + 10, because each of the qq full passes takes 1+126+1+1=129u1 + 126 + 1 + 1 = 129u, and the tail takes 1+1+1+2+1+2r1 + 1 + 1 + 2 + 1 + 2r units, after the first three instructions (4u4u). A lower bound for any program is 2(N+1)u2(N+1)u, because each cell needs a store of at least 2u2u. So the fast program is within 3q+83q + 8 units of the best possible time.

Check

A simulator run with b=64b = 64 and b=100b = 100 for N=0,7,14,…,2996,2999,63,64,62,126,127N = 0, 7, 14, \dots, 2996, 2999, 63, 64, 62, 126, 127 cleared exactly the cells 00 to NN every time and left cell N+1N+1 and all later cells unchanged. The times for b=64b = 64 were: N=0N = 0: 10u10u; N=63N = 63: 136u136u; N=64N = 64: 141u141u; N=2999N = 2999: 6149u6149u, and the short program took 4(N+1)=12000u4(N+1) = 12000u for N=2999N = 2999. These agree with 2N+3q+102N + 3q + 10: for N=2999N = 2999 it is 5998+141+10=61495998 + 141 + 10 = 6149.

Remarks

The count byte of the last MOVE is changed by the program. Such a change of an instruction by the program itself was common in early machine code. Exercise 1.3.1-16 is the case of a fixed NN.

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.