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

Exercise 1.3.1-24

not stated yet in Lean solution written

Rating21
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

Transform rA =+ 0 a b c d= +\,0\,a\,b\,c\,d and rX =+ e f g h i= +\,e\,f\,g\,h\,i into rA =+ a b c d e= +\,a\,b\,c\,d\,e and rX =+ 0 f g h i= +\,0\,f\,g\,h\,i, first with the least memory and then in the least time.

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, look at the ten bytes as one number and cut it at the right place by one division.

Hint 2

For the fast program, look for three shifts that rotate and clear.

Solution

Show the solution

Least memory: use the ten-byte number N=0 a b c d e f g h iN = 0\,a\,b\,c\,d\,e\,f\,g\,h\,i in rAX. Divide by b4b^4 (the byte size to the fourth power), which is the word + 1 0 0 0 0+\,1\,0\,0\,0\,0:

        DIV  CONST
CONST   CON  1(1:1)

The quotient is the first six bytes 0 a b c d e0\,a\,b\,c\,d\,e, which is a b c d ea\,b\,c\,d\,e in rA. The remainder is the last four bytes, 0 f g h i0\,f\,g\,h\,i in rX. The signs are all ++. This takes two words and 12u12u. The constant is put by a word of the form + 1 0 0 0 0+\,1\,0\,0\,0\,0.

Least time: three shifts of 2u2u each:

        SRC  4
        SRA  1
        SLC  5

After SRC 4 the ten bytes 0 a b c d ∣ e f g h i0\,a\,b\,c\,d\,|\,e\,f\,g\,h\,i are rotated right by four places and the registers are f g h i 0f\,g\,h\,i\,0 and a b c d ea\,b\,c\,d\,e. SRA 1 shifts rA right by one place. Its last byte 00 is lost and the first becomes 00, so rA is 0 f g h i0\,f\,g\,h\,i. SLC 5 rotates the ten bytes left by five places, which swaps the two registers. So rA is a b c d ea\,b\,c\,d\,e and rX is 0 f g h i0\,f\,g\,h\,i. The time is 6u6u.

A breadth-first search over sequences of the six shifts (all counts) and ENTA 0, ENTX 0 finds no sequence of one or two instructions, so three instructions and 6u6u are the least for programs of this kind.

Check

A simulator run for b=64b = 64 and b=100b = 100 with random bytes confirms both programs. The second program takes 6u6u, the first takes 12u12u. The search for shorter programs gave nothing of length one or two.

Remarks

The first answer relies on the division to cut a number at a byte boundary. It works for any bb, because the constant is b4b^4 in the machine's own base.

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.