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

Exercise 1.3.1-23

not stated yet in Lean solution written

Rating27
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 reverses the five bytes of the word in cell 0200, using no partial-field instructions, and another program that may use them. Each program must use as few words of memory 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 register rA holds five bytes. One of the shifts SRA brings a chosen byte to the right end.

Hint 2

A shift of rAX by one byte moves the last byte of rA to the front of rX.

Hint 3

Repeat five times and read the result from rX.

Solution

Show the solution

Plan: a loop that takes the bytes one by one from the right of rA and pushes each into the top of rX. After five passes rX holds the bytes in the reverse order.

        ORIG 3000
START   ENT1 4
LOOP    LDA  200
        SRA  0,1
        SRAX 1
        DEC1 1
        J1NN LOOP
        SLAX 5
        HLT

Let the word be + a b c d e+\ a\ b\ c\ d\ e. In the pass with rI1=k\text{rI1} = k, the instruction LDA 200 loads the word and SRA 0,1 shifts it kk places right, so that the byte ee, dd, cc, bb, aa (for k=0,1,2,3,4k = 0, 1, 2, 3, 4) is the last byte of rA. SRAX 1 moves rAX one byte right. The last byte of rA enters the front of rX and the old last byte of rX is lost. The order of passes is k=4,3,2,1,0k = 4, 3, 2, 1, 0, so the bytes enter in the order a,b,c,d,ea, b, c, d, e. After the jjth pass the front of rX holds the byte of the jjth pass and the bytes of the earlier passes follow it. After five passes rX is e d c b ae\ d\ c\ b\ a. The last instruction SLAX 5 moves rX into rA. The shifts do not change the signs of the registers. The last LDA 200 gives rA the sign of the word, and SLAX 5 keeps the sign of rA, so the result has the sign of the word. The program has 77 instructions and the HLT, 88 words, and it uses no partial field, and no temporary cell.

The same program is the answer for the second part, where partial fields are allowed. A program with partial fields, given in the check below, needs 1010 words and so is longer. The proof that no program with fewer than 88 words exists is left to the reader.

Check

A simulator run with the word ± 1 2 3 4 5\pm\ 1\ 2\ 3\ 4\ 5 in cell 200 gave rA=± 5 4 3 2 1\text{rA} = \pm\ 5\ 4\ 3\ 2\ 1 for b=64b = 64 and b=100b = 100 and for both signs, in 43u43u. The field program LDA 200(5:5), LDX 200(1:1), STA 200(1:1), STX 200(5:5), LDA 200(2:2), LDX 200(4:4), STA 200(4:4), STX 200(2:2), LDA 200, HLT also gave + 5 4 3 2 1+\ 5\ 4\ 3\ 2\ 1 and it destroys the word of cell 200 on the way.

Remarks

The loop needs no register other than rA, rX and rI1, and it destroys no cell. The next exercise cuts and joins words in the same way.

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.