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.1-9

Exercise 2.1-9

not stated yet in Lean solution written

Rating23
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 2.1 of Volume 1

The task

Write a MIX program that prints one line for each card of the pile, from the top card down. The line has the title of the card, and it has parentheses around the title if the card is face down.

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 title is in the second word of the node, at address X+1X + 1. One printer line has 2424 words, and the line printer is unit 1818.

Hint 2

A face-down title takes seven characters, so it needs two words of the line buffer. Put the title in rA, shift the pair rAX one byte to the right, and set the two parentheses with partial-field stores.

Hint 3

Wait for the printer with JBUS before you change the buffer again, and clear the buffer once at the start.

Solution

Show the solution

Plan. Walk down the pile with X in rI1. For each card, build the line in a buffer BUF of 2424 words, print it, and go to NEXT(X).

The characters of MIX have these codes: the space is 00, the left parenthesis is 4242, and the right parenthesis is 4343. A face-up line holds the title in the first word and blanks elsewhere. A face-down line holds (, the five characters of the title, and ), in the first two words.

TAG     EQU  1:1
NEXT    EQU  4:5
PRINTER EQU  18
BUF     ORIG *+24         24-word line buffer
PRINT   ENT3 23           clear the buffer
1H      STZ  BUF,3
        DEC3 1
        J3NN 1B
        LD1  TOP          X <- TOP.
        J1Z  9F           empty pile: print nothing.
2H      JBUS *(PRINTER)   wait until the printer is ready.
        LDA  1,1          rA <- TITLE(X).
        LD2  0,1(TAG)     rI2 <- TAG(X).
        J2Z  3F           face up?
        ENTX 0            face down: shift rAX one byte right.
        SRAX 1            rA = 0 t1 t2 t3 t4, rX = t5 0 0 0 0.
        STA  BUF
        STX  BUF+1
        ENTA 42
        STA  BUF(1:1)     "(" in byte 1.
        ENTA 43
        STA  BUF+1(2:2)   ")" in byte 2 of the second word.
        JMP  4F
3H      STA  BUF          face up: title only.
        STZ  BUF+1
4H      OUT  BUF(PRINTER) print the line.
        LD1  0,1(NEXT)    X <- NEXT(X).
        J1NZ 2B
9H      HLT

The shift SRAX 1 moves the title one place to the right, so that byte 1 of rA is free for the left parenthesis and the fifth character t5 falls into the first byte of rX. The store STA BUF(1:1) uses the right-most byte of rA, which is 4242 after ENTA 42. The store STA BUF+1(2:2) does the same for the right parenthesis. In the face-up case, STZ BUF+1 removes the right parenthesis of a card printed before. The words BUF+2 to BUF+23 are never changed, and they stay blank.

Check

I put the three cards of (2) in the memory of a MIX simulator, with the first card face up and the last card face down, and ran the program. The output lines were the two titles without parentheses and then the title of the bottom card with parentheses. For example, the face-down 10 of clubs with the title 10 gave ( 10 ). An empty pile (TOP =0= 0) gave no line.

Remarks

The same buffer serves every line. The JBUS at the head of the loop keeps the program correct when the printer is slower than the loop. Exercise 2.1-8 has the same walk down the pile.

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.