Exercise · Volume 1 · 1.3.1-16
Exercise 1.3.1-16
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| 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 MIX program that clears the cells 0000 to 0099, 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
The instruction MOVE copies a word from to the cell that rI1 names, and then rI1 grows. If the source is the cell just before the target, the copy spreads the first word.
Hint 2
The field of MOVE is the number of words. It must fit in a byte of any size from 64, so it cannot be more than 63.
Hint 3
For the fastest version count the time that each word needs by itself.
Solution
Show the solution
Short program. Put a zero in cell 0, set rI1 to 1, and copy forward twice:
STZ 0
ENT1 1
MOVE 0(49)
MOVE 0(50)
The first MOVE copies cells into cells one word after the other, so every target word receives a zero, because the source word was cleared a moment before. Now rI1 is . The second MOVE copies the cells into . A single MOVE 0(99) would be shorter, but the field does not fit in a byte when . The program has words, and do not suffice: the two zero-making and index-setting instructions are needed, and one MOVE moves at most words. The time is
Fast program. A word cannot be stored in less than , by STZ or by the MOVE of one word. So cells need at least and no program is faster than that. The sequence STZ 0, STZ 1, ..., STZ 99 takes exactly . It has words. It is the fastest program. The MOVE form needs three more .
Check
The two programs ran in a simulator with and . All memory cells to were set to a non-zero word first. Both leave the cells to at and cell unchanged. The times are and (without the HLT).
Remarks
Exercise 1.3.1-17 asks the same for a number of cells that is not fixed. Exercise 1.3.1-20 clears all of memory.
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.