Exercise · Volume 1 · 1.3.1-17
Exercise 1.3.1-17
not stated yet in Lean solution written
| Rating | 26 |
|---|---|
| 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 programs that clear the cells 0000 through for the number in rI2, with , first as short as possible and then as fast 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
For the short program, use a loop that counts rI2 down to and clears one cell for each value.
Hint 2
For the fast one, use MOVE 0(63) in a loop. The last block has fewer than words. The count of the last MOVE is part of the instruction, so the program must store that count into the instruction.
Hint 3
A zero in cell must be there before the first copy.
Solution
Show the solution
Plan: the short version is a loop of one store, the fast version moves zeros in blocks of words, with the last count written into the program by itself.
Short program, starting at location 3000:
LOOP STZ 0,2
DEC2 1
J2NN LOOP
HLT
Each pass clears the cell , , ..., in turn. The loop ends when rI2 becomes . The time is without the HLT, and the program has words and the HLT.
Fast program:
STZ 0
ENT1 1
JMP TEST
LOOP MOVE 0(63)
TEST DEC2 63
J2P LOOP
INC2 63
ST2 LAST(4:4)
LAST MOVE 0
HLT
After STZ 0 and ENT1 1, the cells are cleared in blocks of by MOVE 0(63), and rI1 grows by each time. The counter rI2 holds the number of cells still to be cleared, from the cell on: it starts at . The test DEC2 63; J2P LOOP repeats the MOVE while more than cells remain. When the loop ends, INC2 63 gives with if and if . The instruction ST2 LAST(4:4) writes into the field byte of the last MOVE, and this MOVE copies the last words. The value fits in a byte. Let for , and for . The number of full blocks is , and
The time is , because each of the full passes takes , and the tail takes units, after the first three instructions (). A lower bound for any program is , because each cell needs a store of at least . So the fast program is within units of the best possible time.
Check
A simulator run with and for cleared exactly the cells to every time and left cell and all later cells unchanged. The times for were: : ; : ; : ; : , and the short program took for . These agree with : for it is .
Remarks
The count byte of the last MOVE is changed by the program. Such a change of an instruction by the program itself was common in early machine code. Exercise 1.3.1-16 is the case of a fixed .
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.