TAOCP 1.4.2 Exercise 3

The three `CMPA PERIOD` instructions in `OUT` serve to detect the end-of-input marker, namely the period character `.

Section 1.4.2: Coroutines

Exercise 3. [20] True or false: The three CMPA PERIOD instructions within OUT may all be omitted, and the program would still work. (Look carefully.)

Verified: yes
Solve time: 1m40s


Solution

The three CMPA PERIOD instructions in OUT serve to detect the end-of-input marker, namely the period character .. Control enters OUT1 with rA containing the next translated character from IN. The first STA OUTPUT+16,4(1:1) stores rA into the first position of the current output word, immediately followed by CMPA PERIOD. If rA = PERIOD, execution jumps to 9H, bypassing subsequent storage and continuing the termination sequence.

If this comparison is omitted, and the input character is PERIOD, the program continues to the next instruction, which is JMP IN, causing control to reenter the IN coroutine. IN will fetch the next input character, but the period is intended to signal termination, not to be treated as ordinary input. Consequently, the program will misinterpret the period, potentially overwriting it or requesting additional characters beyond the end of the input, violating the exit condition of OUT.

The second CMPA PERIOD follows STA OUTPUT+16,4(2:2). This ensures that if the period occurs in the second position of the output triple, the program terminates immediately, rather than attempting to store a nonexistent third character. Omitting it allows execution to fall through to the third storage instruction and the subsequent CMPA PERIOD. If the input period is the second character of the triple, this would result in the third storage instruction storing the following character in the next triple prematurely, violating the grouping invariant.

The third CMPA PERIOD follows STA OUTPUT+16,4(3:3). This detects the period in the third position of the output triple and initiates the termination sequence. If this comparison is omitted, a period in the third position will not trigger the jump to 9H. The next instruction increments rI4 and jumps back to 1B, causing the coroutine to continue as though the period had not occurred. This also violates the exit condition of OUT, which requires that the last character processed be the period, and no further input be requested.

Therefore, each of the three CMPA PERIOD instructions corresponds to one of the three positions in the output triple. Removing any of them compromises the correctness of the output grouping and the termination condition. The program relies on checking rA after each STA to determine whether to terminate immediately; omitting all three would allow execution to continue past the period, producing incorrect output and possibly reading beyond the input buffer.

This completes the proof.

$\boxed{\text{False}}$