Exercise · Volume 1 · 1.4.2-7
Exercise 1.4.2-7
not stated yet in Lean solution written
| Rating | 30 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 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 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
chis the period, send out the run in hand, then send the period, then fill and punch the last card, and stop; - if
chequalsCCELLand rI1 , add to rI1; - otherwise send out the run in hand, and start a new run of length with
ch.
To send out a run of length of the character : if and is not a digit, send itself. Otherwise send the digit (code ), and then . Here is a digit if its code is in to .
The cuts are optimal. A run of equal characters costs characters, or if and the character is not a digit. A piece that is longer than costs , and a piece cannot be longer than . So the minimum for a run of a letter, with , is if , if and if . The cutting of 10 first gives this. A run of a digit costs , 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 at that time.
A run never holds a blank, because GET skips blanks. At the start CCELL is blank and rI1 is , so the first character always goes to NEW, and the call of FLUSH with rI1 does nothing.
Check
Hand trace on the input AABB. The state after A: CCELL and rI1 . After the second A: rI1 . The first B is different, so FLUSH sends the count digit (code 31), then A. After the second B and the period, FLUSH sends 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.