Exercise · Volume 1 · 1.3.1-24
Exercise 1.3.1-24
not stated yet in Lean solution written
| Rating | 21 |
|---|---|
| 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
Transform rA and rX into rA and rX , 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 in rAX. Divide by (the byte size to the fourth power), which is the word :
DIV CONST
CONST CON 1(1:1)
The quotient is the first six bytes , which is in rA. The remainder is the last four bytes, in rX. The signs are all . This takes two words and . The constant is put by a word of the form .
Least time: three shifts of each:
SRC 4
SRA 1
SLC 5
After SRC 4 the ten bytes are rotated right by four places and the registers are and . SRA 1 shifts rA right by one place. Its last byte is lost and the first becomes , so rA is . SLC 5 rotates the ten bytes left by five places, which swaps the two registers. So rA is and rX is . The time is .
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 are the least for programs of this kind.
Check
A simulator run for and with random bytes confirms both programs. The second program takes , the first takes . 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 , because the constant is 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.