Volume C1 · Basic concepts · TAOCP Section 1.4
Some fundamental programming techniques
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 1.4 of the book.
Some fundamental programming techniques
Overview
Sections 1.3.1 and 1.3.2 give a machine and a language for it. A reader who knows them can write a short program, trace it and count its time. Section 1.4 asks the next question: how does a person write a program of a thousand lines, and how does the program use the parts of the machine that are not the processor, such as the tapes and the disks? The section gives four answers, in four subsections. The subroutine is a piece of code that many places of a program share. The coroutine is a pair of pieces of code that pass control to each other and that each keep their place. The interpretive routine is a program that reads data as if the data were the instructions of another machine. The buffer lets a program compute while a device moves data. A last subsection, Section 1.4.5, is a history with a list of sources.
The section has no numbered item of its own, and so this chapter has no Node line. It uses the machine of Section 1.3.1, in particular the jump register rJ, the instruction STJ and the input and output instructions, and the language of Section 1.3.2.
The ideas
One idea, four times: separate a job into parts, and pay for the join. In each technique, a part of the work is moved out of the main stream of the program, and the cost of the move is the price of the join. A subroutine saves space and costs a few cycles at each call. A coroutine lets two routines each be written as a simple loop, and it costs a jump and the saving of a place at each exchange. An interpretive routine saves much more space still, since a long action may be a single word of data, and it costs a factor of time for each simulated step. A buffer costs memory for the extra blocks, and it can save all the time that a program would wait for a device. The reader should ask the same two questions of each subsection: what does the technique save, and what does it cost.
The order of the subsections. Section 1.4.1 comes first, because a coroutine is a subroutine whose entry is not fixed, and an interpreter uses subroutines in its table of actions. The treatment of linkage in Section 1.4.1 rests on the fact that the instruction STJ stores the return address in the exit instruction of the subroutine, and this one fact is the basis of Sections 1.4.1 to 1.4.3. Section 1.4.2 shows that the one-way call of Section 1.4.1 is a special case of a more symmetric rule. Section 1.4.3 and its two parts are about programs that treat other programs as data. Section 1.4.3.1 writes a simulator of MIX in MIX, a long program that shows all the instructions in use. Section 1.4.4 is the most machine dependent of the four, since it needs the devices of MIX and their times, and it may be read apart from the others.
What to do with the programs. The programs of Section 1.4 are long. A reader gets little from a program that is only read. The chapters of C1 for this section give a small input for each program, the state of the registers after each step, and the counts of the instructions that run. A reader should repeat at least one such run by hand for the simulator and for the coroutine program, since the control flow of both is not seen from the listing alone.
Two warnings from the section that apply to all the techniques. First, the linkage of Section 1.4.1 changes an instruction of the program in memory. This is a feature of MIX, and the book itself says that a later machine, MMIX, does not do it. A reader must know the reason: a program that modifies itself is hard to prove correct and hard to share between two users. Second, none of the four techniques is free of the rule of Section 1.4.1 on recursion: the return address of a routine is stored in one place, so a routine must not be re-entered before it has returned.
How the section is used later. The later chapters call subroutines without comment, and they expect the reader to know the linkage and its limit on recursion. The buffers of Section 1.4.4 are the first model of the overlap of work and transfer that returns in the study of sorting with tapes and disks, in Volume 3.
Notes and history
The four techniques are older than the book, and the book collects them as practice that a programmer of the time had to know. Section 1.4.5 of the book names the sources, and the chapter of C1 for that section gives a short account of them. The text of Section 1.4.1 on the writing of a long program was set down by the author in 1964, and a note in the book says that its later view adds structured documentation to the advice given there.
Section 1.4 turns from the language MIXAL to the habits of a programmer who writes long programs in it. It has four techniques: the subroutine, the coroutine, the interpretive routine and the buffered transfer of data. Each technique is a way to split a job into parts that call or serve each other, and each has a price in time and in space. This chapter gives the plan of the section and a way to read it. The next chapters give the techniques one by one.
The source of this page. Something is wrong? Report it.