Exercise · Volume 1 · 1.4.1-6
Exercise 1.4.1-6
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.4.1 of Volume 1. Companion chapter: Subroutines |
The task
Write a subroutine MOVE so that the pair JMP MOVE and NOP A,I(F) acts like the instruction MOVE A,I(F), apart from the value of rJ and the time and space used.
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 subroutine can read the word NOP A,I(F) through the address in rJ. Think of what you need from it: the address , the index and the count .
Hint 2
To add the contents of index register to you can build an ENTA A,I instruction in memory and execute it. Be careful about which registers you have used when this instruction is executed.
Hint 3
The copy loop must go upwards, because MOVE works from low to high addresses. A counter that runs from up to together with shifted addresses does this.
Hint 4
Save and restore every register that the loop uses, and do not use any instruction that changes the comparison indicator or the overflow toggle.
Solution
Show the solution
Plan. Find the effective address of the source. Then copy words, from to the locations that start at the old rI1, one by one in increasing order, and add to rI1.
MOVE STJ EXIT
STA SAVA Save rA.
ST2 SAV2(0:2) Save rI2.
LD2 EXIT(0:2) rI2 <- location of the NOP.
LDA 0,2(0:3) rA <- sign, A, I of the NOP.
STA IND(0:3) Store them into the instruction at IND.
LD2 SAV2(0:2) Restore rI2, because I may be 2.
IND ENTA 0 Self-modified: rA <- A + rI[I] = M.
STA SRC
LD2 EXIT(0:2)
LD2N 0,2(4:4) rI2 <- -F.
J2Z DONE No work if F = 0.
LDA SRC
DECA 0,2 rA <- M + F.
STA LDI(0:2)
ENTA 0,1
DECA 0,2 rA <- rI1 + F.
STA STI(0:2)
DEC1 0,2 rI1 <- rI1 + F.
LDI LDA *,2 Address is M + F; with rI2 = -F, -F+1, ... it is M, M+1, ...
STI STA *,2 Address is the old rI1 + F.
INC2 1
J2N LDI Repeat while rI2 < 0.
DONE LDA SAVA
LD2 SAV2(0:2)
EXIT JMP * Goes to the NOP, which does nothing, and then on.
SAVA CON 0
SAV2 CON 0
SRC CON 0
Why it works. The instruction STJ EXIT makes the address field of EXIT equal to the location of the NOP, since rJ holds the address of the instruction after the JMP MOVE. So rI2 points to the NOP word, and its fields can be read. The field (0:3) of the NOP word holds the sign, the two bytes of and the byte . The store into IND(0:3) gives the template ENTA 0 the same address and index, and the template keeps its own F and C fields. When it runs, it computes . This is done after rI2 is restored, because may be 2. The count is the byte (4:4) of the word.
The copy. With rI2 running from to , the addresses run from to , and run from upwards. The words are copied in increasing order, one at a time, so overlapping source and target act as in the hardware instruction. The register rI1 was increased by before the loop, and the index was used before this change. At the end rA and rI2 are restored.
The final JMP * jumps to the NOP, which is harmless, so the program goes on after it. The value of rJ differs from the hardware case, as allowed. The overflow toggle and the comparison indicator are not touched, because the code uses no comparison and no arithmetic on rA that can overflow.
Check
A simulator test compared this code with the hardware instruction on random cases: index fields , counts , random addresses and registers, and an overlapping case (source , target , ). In every case the memory, rA, rX, rI1 to rI6, the comparison indicator and the overflow toggle agreed with those of the hardware MOVE.
Remarks
The idea of reading the word after the call is the same as that of (7) and (8) in the text. Self-modifying code is used here three times (the address of EXIT, the instruction IND and the two loop instructions). Exercise 1.4.1-7 discusses why one avoids it today.
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.