Exercise · Volume 1 · 2.2.4-11
Exercise 2.2.4-11
not stated yet in Lean solution written
| Rating | 24 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 2.2.4 of Volume 1 |
The task
Write a MIX subroutine COPY, called by JMP COPY with rI1 , that leaves in rI2 the address of a new polynomial equal to and leaves rI1 unchanged.
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
Build the copy from its sentinel on. Keep a pointer to the sentinel of the copy, to close the circle at the end, and a pointer to its last node.
Hint 2
The sentinel has . This is also the test that ends the loop.
Solution
Show the solution
A node takes two words: the coefficient in the first and with in the second. The register use is: rI1 the original sentinel, rI2 the new sentinel, rI3 the last node of the copy, rI4 the node of the original that is being read, rI5 the new node.
COPY STJ CEXIT Save the return address.
LD2 AVAIL rI2 <- sentinel of the copy.
J2Z OVERFLOW
LDX 1,2(LINK)
STX AVAIL Pop the node off the AVAIL list.
LDA 0,1 Coefficient of the original sentinel.
STA 0,2
LDA 1,1(ABC)
STA 1,2(ABC) ABC of the sentinel, which is -1.
ENT3 0,2 Last node of the copy so far.
ENT4 0,1 Node read so far.
CLOOP LD4 1,4(LINK) Next node of the original.
LDA 1,4(ABC)
JAN CDONE The sentinel is reached.
LD5 AVAIL New node.
J5Z OVERFLOW
LDX 1,5(LINK)
STX AVAIL
STA 1,5(ABC) Copy the ABC field, rA still holds it.
LDA 0,4
STA 0,5 Copy the coefficient.
ST5 1,3(LINK) Link the previous node to the new one.
ENT3 0,5
JMP CLOOP
CDONE ST2 1,3(LINK) Close the circle at the new sentinel.
CEXIT JMP *
The register rI1 is not changed. On exit rI2 is the new sentinel.
The time is units for a polynomial with terms, counting the instruction JMP COPY of the caller. Each pass through the loop takes units, and the rest is .
Check
I ran the subroutine on 300 random polynomials in the simulator of MIX. The copy equalled the original, its nodes were new, rI1 was unchanged, and the AVAIL list lost exactly nodes. For the times were , , , which agree with .
Remarks
The copy keeps the order of the terms. It does not need to compare any field, in contrast with Program A.
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.