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 · 1.4.2-7

Exercise 1.4.2-7

not stated yet in Lean solution written

Rating30
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.4.2 of Volume 1. Companion chapter: Coroutines

The task

Write a MIX program that reverses the translation of the text. It reads cards punched like (3), and it punches cards like (2), in as few characters as possible. The final period ends the input.

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

Process the input as a stream of characters. Count runs of equal characters, with a count of at most 10.

Hint 2

A single character that is not a digit is sent as itself. A single digit must have the count 00 in front of it.

Hint 3

A run of more than 10 equal characters must be cut. Cut off pieces of 10 first.

Hint 4

Build the output words from five characters each. Remember to fill the last word and to punch the last card.

Solution

Show the solution

The input cards contain words of three characters and two blanks, in the way the text program has made them. The program skips blanks, so it also works for other layouts.

Method. Keep the present character in CCELL and its run length in rI1. For each new nonblank character ch:

  • if ch is the period, send out the run in hand, then send the period, then fill and punch the last card, and stop;
  • if ch equals CCELL and rI1 <10< 10, add 11 to rI1;
  • otherwise send out the run in hand, and start a new run of length 11 with ch.

To send out a run of length k≥1k \ge 1 of the character cc: if k=1k = 1 and cc is not a digit, send cc itself. Otherwise send the digit k−1k - 1 (code 30+k−130 + k - 1), and then cc. Here cc is a digit if its code is in 3030 to 3939.

The cuts are optimal. A run of kk equal characters costs 22 characters, or 11 if k=1k = 1 and the character is not a digit. A piece that is longer than 11 costs 22, and a piece cannot be longer than 1010. So the minimum for a run of a letter, with k=10q+rk = 10q + r, is 2q2q if r=0r = 0, 2q+12q + 1 if r=1r = 1 and 2q+22q + 2 if r>1r > 1. The cutting of 10 first gives this. A run of a digit costs 2⌈k/10⌉2\lceil k/10 \rceil, which the same method gives. Runs of different characters are independent, so the sum is minimal. A zero before a letter is never made.

The program has four parts: GET gives the next nonblank character, PUT puts a character in the output, FLUSH sends out one run, and the main loop.

CARDS   EQU  16
PUNCH   EQU  17
INBUF   EQU  1000
OUTBUF  EQU  1016
        ORIG 2000
PWORD   CON  0           Output word that is being built.
PTMP    CON  0
CCELL   CON  0           Character of the present run.
NEWC    CON  0
TEN     CON  10
FIVE    CON  5
PER     CON  40          Code of the period.
* GET: rA <- next nonblank input character. Uses rX and rI6.
GET     STJ  GX
GAGAIN  JXNZ GCHAR       Characters left in rX?
GNEXTW  J6NZ GLOADW      Words left on the card?
        IN   INBUF(CARDS)
        JBUS *(CARDS)
        ENN6 16
GLOADW  LDX  INBUF+16,6
        INC6 1
        JMP  GAGAIN
GCHAR   ENTA 0
        SLAX 1           Next character into rA.
        JAZ  GAGAIN      A blank: skip it.
GX      JMP  *
* PUT: append the character in rA to the output. Uses rI3, rI4, rI5.
PUT     STJ  PX
        STA  PTMP
        LDA  PWORD
        SLA  1
        ADD  PTMP
        STA  PWORD
        INC3 1
        CMP3 FIVE
        JNE  PX          Word not full yet.
        LDA  PWORD
        STA  OUTBUF+16,4
        INC4 1
        ENT3 0
        STZ  PWORD
        J4NZ PX          Card not full yet.
        OUT  OUTBUF(PUNCH)
        JBUS *(PUNCH)
        ENT5 15
CLR     STZ  OUTBUF,5    Clear the output area.
        DEC5 1
        J5NN CLR
        ENT4 -16
PX      JMP  *
* FLUSH: send out the run in CCELL of length rI1. rI1 is destroyed.
FLUSH   STJ  FX
        J1Z  FX          No run yet.
        DEC1 1
        J1NZ FCNT        Length more than 1.
        LDA  CCELL
        DECA 30
        JAN  FPLAIN      A letter.
        CMPA TEN
        JL   FCNT        A digit: it needs the count 0.
FPLAIN  LDA  CCELL
        JMP  PUT
        JMP  FX
FCNT    ENTA 30,1        The digit k-1.
        JMP  PUT
        LDA  CCELL
        JMP  PUT
FX      JMP  *
START   ENT6 0
        ENTX 0
        ENT4 -16
        ENT3 0
        ENT1 0
MAIN    JMP  GET
        CMPA PER
        JE   FIN
        CMPA CCELL
        JNE  NEW
        CMP1 TEN
        JGE  NEW         Run is full: cut here.
        INC1 1
        JMP  MAIN
NEW     STA  NEWC
        JMP  FLUSH
        LDA  NEWC
        STA  CCELL
        ENT1 1
        JMP  MAIN
FIN     JMP  FLUSH
        LDA  PER
        JMP  PUT
FPAD    J3Z  FPUNCH      Fill the last word with blanks.
        ENTA 0
        JMP  PUT
        JMP  FPAD
FPUNCH  INC4 16
        J4Z  HALT        The last card was punched already.
        OUT  OUTBUF(PUNCH)
        JBUS *(PUNCH)
HALT    HLT
        END  START

How PUT works. The word PWORD is shifted one byte to the left, and the new character is added in the last byte. After five characters the first character is in the first byte, and the word is stored. The shift removes the old first byte, which is 00 at that time.

A run never holds a blank, because GET skips blanks. At the start CCELL is blank and rI1 is 00, so the first character always goes to NEW, and the call of FLUSH with rI1 =0= 0 does nothing.

Check

Hand trace on the input AABB. The state after A: CCELL =A= A and rI1 =1= 1. After the second A: rI1 =2= 2. The first B is different, so FLUSH sends the count digit 11 (code 31), then A. After the second B and the period, FLUSH sends 11 and B, then the period, and the word 1A1B. is full. The card is punched with the other words blank. The output is 1A1B. followed by blanks.

The program was also run on a MIX simulator with 60 random inputs, with runs of 1, 2, 3, 10, 11, 12 and 25 equal characters, with digits, letters and special characters, and with several cards. In each case the output, when read by the decoder of the text, gave the input again, and its length equalled the minimum found by an independent dynamic program. For the input of (3) the output was A2B4E3426FGZYW3220PQ89R., which is (2) without the zero before the Z.

Remarks

The program is a single routine with two subroutines and not a pair of coroutines. The run-length state is explicit here, because the coroutine view does not fit well: the reverse translation must look one character ahead to know where a run ends.

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.