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-11

Exercise 2.2.4-11

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 COPY, called by JMP COPY with rI1 =P= P, that leaves in rI2 the address of a new polynomial equal to polynomial(P)\mathrm{polynomial}(P) 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 ABC<0\mathrm{ABC} < 0. This is also the test that ends the loop.

Solution

Show the solution

A node takes two words: the coefficient in the first and ABC\mathrm{ABC} with LINK\mathrm{LINK} 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 22p+2822p + 28 units for a polynomial with pp terms, counting the instruction JMP COPY of the caller. Each pass through the loop takes 2222 units, and the rest is 2828.

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 p+1p + 1 nodes. For p=4,5,6p = 4, 5, 6 the times were 116116, 138138, 160160, which agree with 22p+2822p + 28.

Remarks

The copy keeps the order of the terms. It does not need to compare any ABC\mathrm{ABC} 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.