Exercise · Volume 1 · 1.3.1-22
Exercise 1.3.1-22
not stated yet in Lean solution written
| Rating | 28 |
|---|---|
| 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 two programs that compute for the integer in cell 2000, one with the least number of words and one with the least running 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
The condition that fits in one word limits strongly. See how large can be for and for .
Hint 2
For the short program use a loop that multiplies by twelve times.
Hint 3
For the fast program think about a table.
Solution
Show the solution
Plan: short program: a loop with a counter; fast program: a table of the th powers, indexed by .
Because , the condition gives for every . For it gives , because .
Short program (seven words):
ORIG 3000
START ENT4 12
LDA 2000
LOOP MUL 2000
SLAX 5
DEC4 1
J4P LOOP
HLT
The register rA starts as . Each pass computes , and SLAX 5 moves the low five bytes back into rA. The product fits in a word, because it is a power of below . After twelve passes rA is . The time is without the HLT. Each of the seven words has its own task: the HLT, the load of , the multiply, the move of the product back to rA, and the three instructions of the loop counter (set, decrease, test). A program with fewer words would need a different method.
Fast program (a table, three instructions and the table of eleven words):
ORIG 3000
START LD1 2000
LDA TABLE,1
HLT
TABLE EQU 3500
ORIG 3495
CON -1220703125
CON -67108864
CON -1594323
CON -8192
CON -1
CON 0
CON 1
CON 8192
CON 1594323
CON 67108864
CON 1220703125
Here the table has the eleven words for , put at the cells so that the cell holds . For the entries for are not needed. The value fits in two bytes, so LD1 2000 is legal. The time is without the HLT. No program can be faster: it must read the cell 2000 ( at least) and then get a new word in rA by an instruction that reads memory ( at least), because an instruction of cannot make an arbitrary word.
Check
Both programs ran for all from to with and for with . Each gave rA exactly, and the times were and . The numbers: 5**13 = 1220703125, 6**13 = 13060694016, 100**5 = 10**10, 64**5 = 1073741824.
Remarks
Section 4.6.3 shows that five multiplications are needed for a generic by addition chains. The loop uses twelve and the table uses none. An addition chain for has five steps and no fewer (Section 4.6.3), so a program with fewer than five multiplications has to use the byte structure of the words.
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.