Exercise · Volume 1 · 1.4.3.1-3
Exercise 1.4.3.1-3
not stated yet in Lean solution written
| Rating | 22 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 1.4.3.1 of Volume 1. Companion chapter: A MIX simulator |
The task
Write the routine MOVE for operation code 7 of the simulator.
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 M(F) copies words from the locations to the locations , one word at a time in this order. At the end rI1 has grown by . The time is .
Hint 2
Because the simulated memory is part of the real memory, a word can be copied with one load and one store. The sign does not need special care.
Hint 3
Check every address that is used. The destination is given by the simulated rI1, kept as a magnitude and a sign.
Hint 4
The table of the text gives 1 unit of time for MOVE. The units remain to be added.
Solution
Show the solution
If , nothing is done, and rI1 stays. Otherwise load the magnitude of the simulated rI1 in the real rI1. If its sign is minus and the magnitude is not zero, the address is not valid. Then do times: check the source (with MEMORY), check the destination, copy the word, add to the simulated clock, and add to both pointers. At the end store the new value of rI1, with sign plus.
MOVE J3Z CYCLE F = 0: nothing to do.
LD1 I1REG Magnitude of the simulated rI1.
LDA SIGN1
JAP 1F
J1NZ MEMERROR Negative and not zero.
1H JMP MEMORY Check the source address in rI5.
CMP1 BEGINC BEGINC holds the value of BEGIN.
JGE MEMERROR Destination outside the memory.
LDA 0,5
STA 0,1 Copy one word, with its sign.
LDA CLOCK
INCA 2
STA CLOCK Two more units of time.
INC1 1
INC5 1
DEC3 1
J3P 1B
ST1 I1REG
ENTA 1
STA SIGN1 The new rI1 has the sign plus.
JMP CYCLE
When the source and the destination overlap, the words are copied in increasing order, and so the copy has the same effect as the hardware: a source word that has been overwritten earlier is copied again with its new value. The simulated value of rI1 can be at most BEGIN at the end, because each destination was checked.
The sign of the new rI1: if the old rI1 was , the sum is positive. So storing the sign plus is right for every case with .
Check
Three hundred random cases ran on a MIX simulator, with , index fields , and , random signed words and different destinations, some of them overlapping the source. The memory and the simulated rI1 were the same as after the real MOVE. The simulated clock after MOVE 1000(F) was for (the formula ).
Remarks
The check of the source in MEMORY is repeated for every word. This is slower than a check of the first and the last word, but it gives the exact place of the error and it stops the move at the same word as the hardware would try to read. The time of the simulation does not matter for the simulated clock.
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.