taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.2.4-15

Exercise 2.2.4-15

not stated yet in Lean solution written

Rating24
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 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 =P= P, rI2 =Q= Q, rI4 =M= M, and it must set polynomial(Q)←polynomial(Q)+polynomial(M)×polynomial(P)\mathrm{polynomial}(Q) \leftarrow \mathrm{polynomial}(Q) + \mathrm{polynomial}(M) \times \mathrm{polynomial}(P) 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 MM run Program A, but make it add the term of PP times that term of MM. For this one changes the three switch instructions: SW1 loads the ABC\mathrm{ABC} field to compare, SW2 and SW3 load the coefficient.

Hint 2

The new SW1 must not add the ABC\mathrm{ABC} of MM to the sentinel of PP.

Hint 3

In the new version the ABC\mathrm{ABC} must be placed in the field (0:3)(0:3) 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 MM be at rI4.

The subroutine M1X is called in place of SW1. It loads the ABC\mathrm{ABC} field of the current term of PP. If that is not negative, it adds the ABC\mathrm{ABC} of the term of MM. Then it shifts the result two bytes left, so that it is in the field (0:3)(0:3). For the sentinel, which has a negative ABC\mathrm{ABC}, the addition is skipped, and the sentinel still compares as −1-1.

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 PP and QQ again. The register rI4 follows the links of MM in a full circle, so at the end it is the sentinel of MM, the original value. The subroutine assumes that every product of two coefficients fits in one word and that the sum of exponents of xx, yy, zz stays below the byte size, as the text does.

If MM is the zero polynomial the first test ends the loop, and the subroutine changes nothing in QQ.

Check

I ran 300 random triples (P,Q,M)(P, Q, M) in the simulator. In each case the polynomial QQ afterward was Q+MPQ + M P computed with python, the polynomials PP and MM 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.