TAOCP 1.3.2 Exercise 5

Program `P` uses the output instruction OUT buffer(PRINTER) on lines 25 and 35, but nowhere uses

Section 1.3.2: The MIX Assembly Language

Exercise 5. [11] Why doesn't Program P need a JBUS instruction to determine when the line printer is ready?

Verified: yes
Solve time: 1m23s


Solution

Program P uses the output instruction

OUT buffer(PRINTER)

on lines 25 and 35, but nowhere uses

JBUS *(PRINTER)

to test whether the printer is available.

The reason is that the algorithm employs two buffers, BUF0 and BUF1, exactly as described in steps P9P11. When an OUT instruction is executed, the contents of the specified buffer are transferred to the output device, and the processor is then free to continue execution while the printer is still printing that line. The printer operates asynchronously.

After line 35 executes, control does not immediately attempt another output operation. Instead, lines 28–34 construct the next line image in the other buffer. During this time the printer is printing the previous buffer.

The program alternates between the two buffers. The constants stored at locations BUF0+24 and BUF1+24 cause line 36,

LD4 24,4

to switch rI4 from one buffer to the other. Hence a buffer is never modified immediately after it has been given to OUT; the program works on the alternate buffer while the printer is busy.

The amount of computation required by lines 28–34 is substantial. For each printed line, ten primes are fetched, converted to character form by CHAR, and stored into the buffer. This computation takes much longer than the time needed to issue the OUT instruction itself. Consequently, by the time the next OUT instruction is reached, the printer has completed printing the previous line and is ready to accept another buffer.

Therefore no JBUS instruction is needed. The double-buffering scheme ensures that printing and buffer preparation proceed concurrently, and the printer is always ready when the next OUT instruction occurs.

$$ \boxed{\text{Program P avoids } JBUS \text{ by alternating between two buffers, so printer activity overlaps buffer preparation.}} $$