Exercise · Volume 1 · 2.2.4-15
Exercise 2.2.4-15
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 MULT that does Algorithm M. It is called by JMP MULT with rI1 , rI2 , rI4 , and it must set without changing rI1, rI2, rI4.
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 each term of run Program A, but make it add the term of times that term of . For this one changes the three switch instructions: SW1 loads the field to compare, SW2 and SW3 load the coefficient.
Hint 2
The new SW1 must not add the of to the sentinel of .
Hint 3
In the new version the must be placed in the field of rA, since the compare in Program A uses the field of rA with the same specification as the memory.
Solution
Show the solution
The three switches of Program A are SW1 LDA 1,1, SW2 LDA 0,1 and SW3 LDA 0,1. For the multiplication they are replaced by jumps to two small subroutines. Let the current term of be at rI4.
The subroutine M1X is called in place of SW1. It loads the field of the current term of . If that is not negative, it adds the of the term of . Then it shifts the result two bytes left, so that it is in the field . For the sentinel, which has a negative , the addition is skipped, and the sentinel still compares as .
The subroutine M2X is called in place of SW2 and SW3. It multiplies the coefficients and puts the product in rA.
MULT STJ MEXIT Save the return address.
LDA JM1
STA SW1 SW1 becomes JMP M1X.
LDA JM2
STA SW2 SW2 and SW3 become JMP M2X.
STA SW3
MLOOP LD4 1,4(LINK) Next term of M.
LDA 1,4(ABC)
JAN MDONE The sentinel of M: all terms are done.
JMP ADD Program A adds term(M) * P to Q.
JMP MLOOP
MDONE LDA OLD1
STA SW1 Put back the switches.
LDA OLD2
STA SW2
STA SW3
MEXIT JMP *
M1X STJ M1R
LDA 1,1(ABC)
JAN M1S
ADD 1,4(ABC) Add ABC(M) if ABC(P) is not negative.
M1S SLA 2 Move into the field (0:3) of rA.
M1R JMP *
M2X STJ M2R
LDA 0,1
MUL 0,4
SLAX 5 rA <- the low part of the product.
M2R JMP *
JM1 JMP M1X
JM2 JMP M2X
OLD1 LDA 1,1 The usual switch instructions of Program A.
OLD2 LDA 0,1
The instruction JMP ADD is a call of Program A, which returns to the next instruction, and Program A leaves rI1 and rI2 at the sentinels of and again. The register rI4 follows the links of in a full circle, so at the end it is the sentinel of , the original value. The subroutine assumes that every product of two coefficients fits in one word and that the sum of exponents of , , stays below the byte size, as the text does.
If is the zero polynomial the first test ends the loop, and the subroutine changes nothing in .
Check
I ran 300 random triples in the simulator. In each case the polynomial afterward was computed with python, the polynomials and were unchanged, and the three registers had their original values.
Remarks
The switches are modified inside the subroutine, so the program is not reentrant, and it restores the switches before it returns, as the exercise requires.
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.