Volume C1 · Basic concepts · TAOCP Section 1.4.1
Subroutines
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.1 of the book. The nodes and the exercises of the section.
Contents
Subroutines
Overview
A subroutine is a piece of code that a program reaches from several places and leaves by a return to the place of the call. Section 1.4.1 explains how MIX does this with the jump register rJ and with one instruction that the subroutine changes in its own text. The section then asks two questions about every subroutine: what does it save, and what does it cost? It answers the first with a count of memory words and the second with a count of cycles. It also states how to describe a subroutine so that the caller needs nothing but the description, and it gives the ways to pass arguments: in registers, in fixed cells, and in the words after the call. The last topics are the entrance and exit variants, the limit on nested calls, and a four-step method for the design of a long program.
The section uses the registers, the jump instructions and the instruction STJ of Section 1.3.1, and the language of Section 1.3.2. Its running example is the subroutine for the largest of words that Section 1.3.2 also uses. This chapter writes its own listing of that subroutine, so that every count in it was checked by a run.
The ideas
The return address is in rJ, and STJ puts it in the code. A jump instruction that is taken puts the address of the next instruction in rJ, with the exception of the jump JSJ. A subroutine begins with STJ, which copies rJ into the address part of its last instruction. The last instruction is a jump to itself, and after the copy it is a jump to the place after the call. This is the whole mechanism. It needs that the subroutine has its own cell for the return address, and it explains why a subroutine must not run twice at the same time.
Space and time are a simple trade. A piece of code of words that is used in places takes words. As a subroutine it takes words, two more words for the linkage, and one call word in each place. The saving is words. Each call costs more cycles. When the call needs words, the saving is . These counts are for an ideal case, and the section says so: a subroutine must handle the general case, while the inline copy could use facts about its place.
A subroutine extends the machine. After a subroutine is written, one instruction does a whole task. So the subroutine needs a definition as exact as the definition of an instruction in Section 1.3.1: what it needs at the entry, what it gives at the exit, and what else it changes. A caller then reasons from the definition and not from the code. A register that is not named in the definition keeps its value.
Arguments have three homes. An argument may be in a register, in a fixed cell, or in the words that follow the call. The third way lets a subroutine take many arguments, and the subroutine reads them through the return address.
Entrances and exits are parameters too. A subroutine may have several first instructions. The extra entrances put a fixed value in a register and then join the main code. A subroutine may return to one of several places. The place is then an argument, and it may be a small number that the subroutine adds to the return address.
A subroutine is not recursive. The cell that holds the return address holds only one address. If a subroutine is entered again before it leaves, the first address is lost. Chapter 8 of the book treats the general case. Section 1.4.1 only states the limit.
Write the long program twice, in two directions. The method of the section is to sketch from the top down and find the subroutines, then to write the real code from the bottom up, then to look at all the calls of each subroutine, and last to run the program by hand or on a machine. The method allows a full restart after the third step, and it asks the programmer not to repeat the first two steps for ever.
Definitions, results and proofs
Definition (subroutine, calling sequence, entrance, exit, TAOCP Section 1.4.1). A subroutine is a block of instructions that a program may enter from several places and that returns control to the place of the entry. The first instruction that runs when the block is entered is an entrance. The instruction that leaves the block is an exit. The code of the caller that transfers control to the entrance, with the code that prepares the arguments, is the calling sequence. A value that changes from one call to the next is a parameter of the subroutine. The value that a caller supplies in one call is an argument.
Node: 1.4.1-def-subroutine · Lean: not yet stated
Explanation. The definition has no more content than its words, but the mechanism behind it is exact and it is worth a theorem. In MIX the transfer to a subroutine is a jump that is taken. The jump leaves in rJ the address of the instruction after the jump. The return is a jump whose address was set to that value. The next theorem says that the linkage with STJ does this.
Theorem (the linkage of a subroutine). Let a subroutine begin with the instruction STJ EXIT, where EXIT is the address of a cell that holds a jump with index , and let no other instruction of the subroutine change the address part of the cell EXIT. Let a jump that is not JSJ and that is taken, at the address , enter the subroutine. Then the instruction at EXIT jumps to , when it runs.
Node: 1.4.1-thm-linkage · Lean: not yet stated
Proof. The jump at is taken, so it sets rJ to . The entrance is the instruction STJ EXIT. Its field is . It stores the sign and the two low bytes of rJ in the sign and the address bytes of the cell EXIT, and it does not touch the index byte, the field byte and the operation byte of that cell. After this, the cell is a jump with the address and the index . By assumption nothing else changes the cell until the exit instruction runs, so the exit instruction jumps to . The register rJ holds a positive number that is less than the size of the memory, so the stored address is a valid address.
The proof also shows the cost of the linkage: the call, the instruction STJ and the exit jump, which take , and u.
Example. A caller has JMP ABS at the address , where ABS is the subroutine of the specification example below. At the entrance rJ is . The first instruction of ABS is STJ EXIT, so the cell EXIT then holds a jump to . A run in a simulator of MIX showed this: after the return, the next instruction that ran was the one at .
Theorem (the saving of space, TAOCP eq. 1.4.1(2)). Let a piece of code of words occur in places of a program, and let a call take one word. Let the subroutine have one STJ word and one exit word besides the code. Then the program with the subroutine has words for this code, the program without it has words, and the saving is words.
Node: 1.4.1-(2) · Lean: not yet stated
Explanation. The count is exact for the model of the statement. The saving is positive if and only if . It is never positive for or , because one factor is zero and the saving is .
Example. The table has the saving for small and . Each entry is . The entries were computed by a program.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
| 2 | 0 | 1 | 2 | 3 | 4 | |||
| 3 | 1 | 3 | 5 | 7 | 9 | 11 | ||
| 4 | 0 | 3 | 6 | 9 | 12 | 15 | 18 | |
| 5 | 1 | 5 | 9 | 13 | 17 | 21 | 25 |
For the saving is positive only for . For it is positive already for . A concrete case is and . The three words LDA A, ADD B and STA C in six places take words. With a subroutine they take call words, words of code, one STJ and one exit, which is words, and .
Proof. Without a subroutine each of the places has the words, so the total is . With a subroutine each of the places has a single call word, which gives words. The subroutine has the words of code, one word STJ and one word for the exit, which gives . The total is . The difference is . Now , so .
Theorem (the cost of time, TAOCP Section 1.4.1). If the subroutine is called times in a run, then the program with the subroutine takes more cycles than the program without it, in the model of the previous theorem.
Node: 1.4.1-thm-time · Lean: not yet stated
Proof. In each call, three instructions run that the inline code does not have: the call JMP, which takes u, the instruction STJ, which takes u, and the exit jump, which takes u. The body of the subroutine is the same code as the inline code and takes the same time. So each call adds u, and calls add .
Example. The code LDA A, ADD B, STA C takes u inline. As a subroutine, one use takes u. The difference is u. A loop that makes calls pays u for the saving of words in the example above.
Theorem (a calling sequence of words, TAOCP eq. 1.4.1(3)). Let every call take words, with the jump among them, and let the code have words. Then the subroutine saves words. For this is the value of eq. 1.4.1(2).
Node: 1.4.1-(3) · Lean: not yet stated
Explanation. The calling sequence is a cost that the programmer pays times. The subroutine is useful only when its body is longer than its calling sequence. The cost in time also grows: each call runs the words of its calling sequence, so the cost of one call is the time of those words plus u for the instruction STJ and the exit, in place of the u of the one-word call.
Example. Let , , , for instance a call ENT1 n and JMP. Then the saving is words. Directly: the program without the subroutine has words, the one with it has words, and the difference is .
Proof. The count without a subroutine is . With it, the count is for the calls, for the code and for the linkage. The difference is . Now , so the difference is . For the value is .
Definition (the specification of a subroutine, TAOCP eq. 1.4.1(4)). The specification of a subroutine has four parts: the calling sequence, the entry conditions on the registers and the cells, the exit conditions that tell what the subroutine computes, and the list of every register and cell that it changes. A subroutine meets its specification when, for every state that satisfies the entry conditions, the run from the entrance to the return reaches the place after the calling sequence with the exit conditions true, and with each register and cell outside the list unchanged.
Node: 1.4.1-(4) · Lean: not yet stated
Explanation. The last clause is a frame rule. The caller may assume that anything that is not named is unchanged. The book leaves out rJ and the comparison indicator from its lists by custom, since every subroutine changes rJ, and since most subroutines change the indicator. This chapter names them each time, because an exact statement needs them. The specification is the interface of an extension of the machine, and it is as binding as the definition of an instruction.
Example. Take the subroutine ABS that replaces rA by its absolute value. Its text is: STJ EXIT, then JANN EXIT, then STA TMP, ENTA 0, SUB TMP, and the exit at the end. The specification is as follows.
- Calling sequence: JMP ABS.
- Entry: rA holds any word.
- Exit: rA holds the absolute value of the old rA. The magnitude does not change, so there is no overflow.
- Changed: rA, rJ, the cell TMP, and nothing else. The comparison indicator, rX, rI1 to rI6 and the overflow toggle are unchanged.
- Time: u if rA is not negative and u if it is, without the u of the call.
A run for rA , with rX , rI2 and the indicator set to greater, gave rA , the same rX, rI2 and indicator, the value in TMP, and u. The run for rA gave u, and the cell TMP was not changed. The cell TMP is in the list of changed cells even though the run for left it alone, because the run for changes it. A caller must not keep a value there.
Check. The path for a negative rA runs STJ (), JANN (, not taken), STA (), ENTA (), SUB () and the exit (), and . The path for a nonnegative rA runs STJ, JANN with the jump taken, and the exit, and .
Theorem (the saving of a register, TAOCP Section 1.4.1). Let a subroutine change rI3 and let the caller need rI3 back. Then two more instructions make the subroutine keep rI3: the instruction ST3 3F(0:2) at the entry, after the STJ, and the instruction 3H ENT3 * just before the exit. The extra cost is words and u per call.
Node: 1.4.1-thm-save-register · Lean: not yet stated
Proof. The instruction ST3 3F(0:2) stores the sign and the two bytes of rI3 in the sign and address bytes of the cell 3H. That cell is the instruction ENT3 with the address in the text. After the store, the cell is ENT3 with the old value of rI3 as address, and the instruction at 3H loads in rI3 again. The instruction ST3 takes u and ENT3 takes u, so the cost is u. The two instructions are two words.
Example. The subroutine MAXN of the next part, with these two lines added, was run for and the data . It left rI3 at the exit, and it took u more than the version without the two lines, u in place of u. In the version without them, rI3 is at the exit.
Algorithms and programs
This part has the subroutines of Section 1.4.1. The listings are written for this handbook, and the times were checked by running them in a simulator of MIX. The data lie in the words , with X , and the subroutines start at the address .
Program (the largest of n words, TAOCP eq. 1.4.1(1)). The subroutine MAXN has the calling sequence ENT3 n and JMP MAXN, with . At the exit rA holds the largest of , rI2 holds the smallest index of a word with that value, rI1 and rI3 . It changes no other register, and no cell. A second entrance MAX100 does ENT3 100 and falls into MAXN.
Node: 1.4.1-(1) · Lean: not yet stated
Idea. The subroutine scans the words from the first to the last. Register rA holds the largest word seen so far, rI2 holds its index, rI1 is the index of the word that was read last, and rI3 counts the words not yet read. A word replaces the largest one only if it is greater, so the first of equal words wins.
The program. The table has the listing with the times and the frequencies. The number is the number of indices with for which is greater than each of .
| Location | Line | Time | Frequency |
|---|---|---|---|
| 3000 | MAX100 ENT3 100 | 1 | 0 or 1 |
| 3001 | MAXN STJ EXIT | 2 | 1 |
| 3002 | LDA X+1 | 2 | 1 |
| 3003 | ENT2 1 | 1 | 1 |
| 3004 | ENT1 1 | 1 | 1 |
| 3005 | DEC3 1 | 1 | 1 |
| 3006 | 1H J3Z EXIT | 1 | |
| 3007 | INC1 1 | 1 | |
| 3008 | DEC3 1 | 1 | |
| 3009 | CMPA X,1 | 2 | |
| 3010 | JGE 1B | 1 | |
| 3011 | LDA X,1 | 2 | |
| 3012 | ENT2 0,1 | 1 | |
| 3013 | JMP 1B | 1 | |
| 3014 | EXIT JMP * | 1 | 1 |
A run. Let and , called from the address by ENT3 3 at and JMP MAXN at . Then rJ is at the entrance. The table shows the state after each instruction of the subroutine. The time is counted from the entrance. The column ci has , or for less, equal or greater.
| Step | Location | Instruction | Time | Total | rA | rI1 | rI2 | rI3 | ci | rJ |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3001 | STJ EXIT | 2 | 2 | 0 | 0 | 0 | 3 | 0 | 102 |
| 2 | 3002 | LDA X+1 | 2 | 4 | 3 | 0 | 0 | 3 | 0 | 102 |
| 3 | 3003 | ENT2 1 | 1 | 5 | 3 | 0 | 1 | 3 | 0 | 102 |
| 4 | 3004 | ENT1 1 | 1 | 6 | 3 | 1 | 1 | 3 | 0 | 102 |
| 5 | 3005 | DEC3 1 | 1 | 7 | 3 | 1 | 1 | 2 | 0 | 102 |
| 6 | 3006 | J3Z EXIT | 1 | 8 | 3 | 1 | 1 | 2 | 0 | 102 |
| 7 | 3007 | INC1 1 | 1 | 9 | 3 | 2 | 1 | 2 | 0 | 102 |
| 8 | 3008 | DEC3 1 | 1 | 10 | 3 | 2 | 1 | 1 | 0 | 102 |
| 9 | 3009 | CMPA X,1 | 2 | 12 | 3 | 2 | 1 | 1 | 102 | |
| 10 | 3010 | JGE 1B | 1 | 13 | 3 | 2 | 1 | 1 | 102 | |
| 11 | 3011 | LDA X,1 | 2 | 15 | 8 | 2 | 1 | 1 | 102 | |
| 12 | 3012 | ENT2 0,1 | 1 | 16 | 8 | 2 | 2 | 1 | 102 | |
| 13 | 3013 | JMP 1B | 1 | 17 | 8 | 2 | 2 | 1 | 3014 | |
| 14 | 3006 | J3Z EXIT | 1 | 18 | 8 | 2 | 2 | 1 | 3014 | |
| 15 | 3007 | INC1 1 | 1 | 19 | 8 | 3 | 2 | 1 | 3014 | |
| 16 | 3008 | DEC3 1 | 1 | 20 | 8 | 3 | 2 | 0 | 3014 | |
| 17 | 3009 | CMPA X,1 | 2 | 22 | 8 | 3 | 2 | 0 | 1 | 3014 |
| 18 | 3010 | JGE 1B | 1 | 23 | 8 | 3 | 2 | 0 | 1 | 3011 |
| 19 | 3006 | J3Z EXIT | 1 | 24 | 8 | 3 | 2 | 0 | 1 | 3007 |
| 20 | 3014 | EXIT JMP * | 1 | 25 | 8 | 3 | 2 | 0 | 1 | 3015 |
The result is rA and rI2 , the place of in . At step 20 the cell EXIT holds a jump to , since step 1 stored rJ there, so control goes to . The time is u. Here and the formula below gives . The JGE at step 10 does not jump, since is the case rA less than the word, and this is why the update lines run.
Correctness. The claim is: when control arrives at the line 1H, with rI3 equal to , then rI1 , rA is the largest of , and rI2 is the smallest index of a word of that value in that range. At the first arrival, . The lines at to have set rA , rI2 , rI1 and rI3 , and the claim holds. Suppose that it holds for some . If , the line J3Z jumps to the exit, and the claim says that rA is the largest of all words and rI2 is its smallest index, which is the specification. If , then INC1 gives rI1 , DEC3 gives rI3 , and CMPA compares rA with . If rA the line JGE goes to 1H, and the largest value and its smallest index do not change, since the new word is not larger. If rA , the lines at and put the new word and its index in rA and rI2, and the new word is strictly larger than all that came before, so its index is the smallest one with that value. In both cases the claim holds for . The value of rI3 goes down by one at each pass, so after passes the value is and the subroutine leaves. The registers rX, rI4, rI5 and rI6 and the memory are never named in the code, so they are unchanged.
Time. The line 1H runs once for each value , which is times. The lines from INC1 to JGE run for , which is times. The three lines of the update run times, by the definition of . The sum is where the first group is for the lines at to , and the last term is for the exit jump. The call JMP MAXN adds u, and the entrance MAX100 adds the u of ENT3. The time is least when is the largest (), which gives , and it is greatest when the words increase (), which gives .
Program (the second form, TAOCP eq. 1.4.1(5)). The entrances MAX100 and MAXN above are the second form of the subroutine. The first instruction ENT3 100 does not change rJ, and the next instruction is the STJ of MAXN. So a call of MAX100 and a call of MAXN with rI3 do the same work, and the subroutine is one body with two entrances.
Node: 1.4.1-(5) · Lean: not yet stated
Explanation. The listing uses the fact that ENT3 is not a jump, so it leaves rJ as the call set it. The order of the first lines matters: the extra entrance sets the register and then reaches the instruction STJ, so the return address is saved after the call and not before it.
Example. A program with the call JMP MAX100 at and random sets of words in was run with the listing of the previous program. Each run returned to with the largest word in rA and its smallest index in rI2. The call ENT3 4 with JMP MAXN, run on random sets of words, did the same work for .
Proof. The instruction ENT3 100 sets rI3 to and does not change rJ, rA or the indicator. Then the body is the one of MAXN with , and the correctness of MAXN gives the correctness of MAX100. The time is the time of MAXN for , plus u.
Program (a third entrance, TAOCP eq. 1.4.1(6)). A third entrance MAX50 can be placed before the other two. It has the instructions ENT3 50 and JSJ MAXN. The instruction JSJ jumps to MAXN and leaves rJ unchanged.
Node: 1.4.1-(6) · Lean: not yet stated
Explanation. The entrance MAX50 is not next to MAXN, so it cannot fall into it. It needs a jump. A plain JMP would put the address of the next word of MAX50 in rJ, and STJ would then store this wrong address. The instruction JSJ jumps and leaves rJ as the call set it. Exercise 1.4.1-2 asks for a way that does not use JSJ.
Example. A program with the three entrances was run. The calls JMP MAX100 and JMP MAX50, and the call ENT3 4 with JMP MAXN, were each run on random data sets of the right size. Each run returned with the largest value in rA and its smallest index in rI2.
Proof. The call JMP MAX50 makes rJ the address after the call. The instruction ENT3 does not change it, and the instruction JSJ does not change it, so at MAXN the STJ stores the address of the caller. The rest is the proof of the program.
Program (the arguments after the call, TAOCP eqs. 1.4.1(7) and (8)). The calling sequence JMP MAXA followed by a word CON n gives the argument in the word after the call. The subroutine MAXA reads that word through the return address and returns to the word after it.
Node: 1.4.1-(7) · Lean: not yet stated
Idea. The first instruction STJ *+1 stores rJ in the address part of the next instruction. The next instruction is ENT1 *, which now loads rI1 with the return address, that is, with the address of the word CON n. The instruction LD3 0,1 reads the argument. The subroutine then adds to rI1, so rI1 holds the address after the argument, and ST1 EXIT(0:2) puts this address in the exit instruction. The body that follows is the body of MAXN.
Example. In a run the call was at and the word CON 5 at . After STJ the instruction ENT1 held , so rI1 became . The load LD3 0,1 gave rI3 . After INC1 1, the register rI1 was , and the exit jump went to , past the argument word. The subroutine was run on random data sets of five words, and each run halted at , the place of the halt instruction after the argument, with the correct maximum.
Proof. At the entrance rJ is the address of the word after the call, which holds the argument. The instruction STJ *+1 changes the address part of the instruction at the next address, so that this instruction is ENT1 . It sets rI1 to , and the load reads the argument, which is the word at when index 1 is added to the address . Then the increase gives , and the store ST1 EXIT(0:2) makes the exit a jump to . After this the subroutine runs as MAXN with rI3 . So the return goes to , as the calling sequence requires.
Explanation. The way is useful when a subroutine has many arguments, and it is the way that a compiler can generate easily. The multiple entrance of the previous program does not mix with it well: an extra entrance would have to put the argument word in place, which is a more complex job. This is the reason why the book calls the earlier form more attractive when the arguments are few.
Program (two exits, TAOCP eq. 1.4.1(9)). Let MAXN be changed so that it returns to the word after the call when the largest value is not positive, and to the word two after the call when it is positive. The calling sequence has the call and then two places. The exit is JMP *,3, and rI3 is or at that moment.
Node: 1.4.1-(9) · Lean: not yet stated
Idea. The subroutine MAXN of the first program ends with rI3 . A test of rA after the scan sets rI3 to when rA is positive. Then the exit instruction, which has the index , adds rI3 to the address that STJ stored.
Example. The listing with the three entrances and the two exits was run on random data sets for each entrance. The data had values from to . In every run the halt was at the first place after the call when the largest value was not positive, and at the second place when it was positive.
Proof. The index part of the cell EXIT is . The instruction STJ changes only the sign and the address bytes, so the index stays. At the exit the effective address is the stored address plus rI3. If the largest value is not positive, the test leaves rI3 and the jump is to . Otherwise the instruction ENT3 1 sets rI3 to , and the jump is to .
Explanation. The return place is an argument of a subroutine. For several places, the caller gives them in the order that the subroutine will take, and the subroutine chooses an offset. This is the same method as the arguments after the call.
Theorem (the limit on nested calls, TAOCP eq. 1.4.1(10)). Subroutines with the linkage of this section may call each other to any depth, as long as no subroutine is called when it is already in a call that has not returned. If a subroutine is entered again before it returns, the return address of the first entry is lost, and the subroutine does not return to its first caller.
Node: 1.4.1-(10) · Lean: not yet stated
Proof. Each subroutine has one cell EXIT that holds the return address. Suppose that no subroutine is entered twice at the same time. Then the calls that are open at any moment form a chain , where was called by . The proof is by induction on the depth of the chain. When is entered, the cell EXIT gets the address of its caller. The cell is changed only by the entrance of , and by assumption is not entered again until it returns. So when reaches its exit the cell has the address of the caller, and the return is correct. For the second part, suppose that is entered while it is open. The second STJ writes the new return address in the cell EXIT and the old address is no longer stored anywhere, so the first call cannot return to its caller.
Example. Let A call B, and let B call A again once. The listing in the run had A with STJ EXITA and JMP B, and B with STJ EXITB, a test of a flag that makes B call A once and clear the flag. The main program called A from the address . The cell EXITA held a jump to after the first entrance and a jump to after the second, so the address was lost. In the run of steps, the program did not halt: the control passed between the addresses and for ever, and it never came back to the main program. A recursive scheme needs a stack for the return addresses. Chapter 8 of the book treats it, and Chapter 2 gives the stack.
The exercises
The table has the exercises of Section 1.4.1. The book marks no exercise of this section as recommended in the text that C1 reads. The kind is the class of spec 06.3 that C1 proposes.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 1.4.1-1 | 10 | informal | not yet stated | X1, 1.4.1-1 | |
| 1.4.1-2 | 10 | K6 | not yet stated | X1, 1.4.1-2 | |
| 1.4.1-3 | 15 | M | K3 | not yet stated | X1, 1.4.1-3 |
| 1.4.1-4 | 21 | K6 | not yet stated | X1, 1.4.1-4 | |
| 1.4.1-5 | 21 | K6 | not yet stated | X1, 1.4.1-5 | |
| 1.4.1-6 | 26 | K6 | not yet stated | X1, 1.4.1-6 | |
| 1.4.1-7 | 20 | informal | not yet stated | X1, 1.4.1-7 |
The exercises have three groups. Exercises 1 and 3 ask for the exact specification of a subroutine of the section, and the specification of ABS and the exit conditions of MAXN above are models. Exercises 2, 4, 5 and 6 are programs, and each is solved by the method of the section: write the specification first, then the body, and check the time by the frequencies. Exercise 5 changes the linkage itself, so it needs the theorem of the linkage and its proof. Exercise 7 asks for the reason that self-modifying code is now avoided, and the notes below give the main points.
Lean
The Lean code of tamnd/taocp-lean at main has the model of the machine in TAOCP/MIX/, and no file for Section 1.4.1. The linkage is in the definition TAOCP.MIX.State.jump, which has a flag saveJ. A jump that is taken with the flag true writes the program counter in the jump register, and the jump with the flag false is JSJ. The instruction STJ is in the execution of stores, TAOCP.MIX.State.execStore. The theorems of this chapter about the linkage and about the counts of space and time are not stated. A statement of the linkage theorem would be a triple of the program logic of TAOCP/MIX/Logic/Hoare.lean, for the code of a subroutine with a call, and the time would be a number in the triple.
The specification of a subroutine is a triple of the same logic, with the time. The frame clause of the definition would rest on the frame lemmas of the module TAOCP/MIX/Logic/Frame.lean, which tell what one instruction leaves unchanged. The Node lines say that no Lean statement exists for the items of this section, and they do not name a declaration.
Notes and history
The linkage of this section stores the return address in the text of the subroutine. It is the oldest way to link a closed subroutine, and the first book on the preparation of programs for a stored-program computer, by Wilkes, Wheeler and Gill (1951), is built on a library of such subroutines for the EDSAC. The text of the section on the writing of a long program was written by its author in 1964, and a note in the book says that he later added the practice of structured documentation, which he calls literate programming.
Later machines do not keep return addresses in the code. A program that changes itself is hard to read, because the text on the page is not the program that runs. It is hard to prove correct. It cannot be shared by several users at once, since each user needs a private copy of the code, and it cannot be kept in a read-only memory. The book says that the machine MMIX keeps this information in registers and in a special array, and exercise 7 asks for these reasons.
For the method of top-down design, the section cites the book of Dijkstra, Structured Programming (1972), and the paper of Wirth in Communications of the ACM 14 (1971), pages 221 to 227. The section turns the method into a two-direction process: a sketch from the top, then the code from the bottom, then a look at all the calls of each subroutine, then a test. The author allows a full restart after the third step, and he warns that the restart must not go on for ever.
Section 1.4.1 shows how a block of code that many places of a program need is written once and called. It defines the calling sequence, the entrance and the exit, counts the space that a subroutine saves and the time that it costs, shows how to give a subroutine several entrances, several exits and its arguments in the code after the call, and gives the rule that a subroutine cannot call itself with this linkage. It ends with a method for the writing of a long program. This chapter proves each count, tests each linkage on a run of MIX, and traces a subroutine for the largest of words on a small input.
The source of this page. Something is wrong? Report it.