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

Exercise 1.3.1-21

not stated yet in Lean solution written

Rating24
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

Decide if rJ can be zero, and write a program that sets rJ to a given NN without changing any memory cell.

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

What instructions change rJ? Which ones can put the value 00 in it?

Hint 2

To set rJ to NN a jump must be made from the cell N−1N - 1. So an instruction must stand there for a moment.

Solution

Show the solution

(a) The only operations that change rJ are the jumps (except JSJ). A jump from the cell cc sets rJ to c+1≥1c + 1 \ge 1. No operation loads rJ from memory or from a register. So rJ can be 00 only at the start, after the machine start (the "GO button" of Exercise 1.3.1-26), and never again.

(b) Place a JMP into the cell N−1N - 1 for a moment, jump there, and then restore the cell:

        ORIG 3000
START   LDX  -1,4
        LDA  JUMPW
        STA  -1,4
        JMP  -1,4
BACK    STX  -1,4
        HLT
JUMPW   JMP  BACK

The instruction LDX -1,4 saves the word of cell N−1N - 1 in rX. Then LDA JUMPW loads the word JMP BACK and STA -1,4 stores it into the cell N−1N - 1. The instruction JMP -1,4 goes to that cell. The word there is JMP BACK. It is a jump from the cell N−1N - 1, so rJ becomes NN. Then it goes to BACK, which restores the old word from rX. Memory is unchanged and the machine stops with rJ equal to NN. Because 0<N≤30000 < N \le 3000, the cell N−1N - 1 lies in 0…29990 \ldots 2999 and does not touch the program. The time is 10u10u without the HLT.

Check

A simulator run for N=1,2,…,3000N = 1, 2, \dots, 3000 and for b=64b = 64 and b=100b = 100 gave rJ =N= N every time. A comparison of all 4000 cells before and after the run showed no change except in rA and rX.

Remarks

The same trick sets rJ to any value from 11 to 39993999 if the program is put in other cells.

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.