Exercise · Volume 1 · 1.3.1-23
Exercise 1.3.1-23
not stated yet in Lean solution written
| Rating | 27 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 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 . In the pass with , the instruction LDA 200 loads the word and SRA 0,1 shifts it places right, so that the byte , , , , (for ) 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 , so the bytes enter in the order . After the th pass the front of rX holds the byte of the th pass and the bytes of the earlier passes follow it. After five passes rX is . 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 instructions and the HLT, 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 words and so is longer. The proof that no program with fewer than words exists is left to the reader.
Check
A simulator run with the word in cell 200 gave for and and for both signs, in . 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 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.