taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

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
  1. Subroutines

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 nn 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 kk words that is used in mm places takes mkmk words. As a subroutine it takes kk words, two more words for the linkage, and one call word in each place. The saving is (m−1)(k−1)−3(m-1)(k-1)-3 words. Each call costs 44 more cycles. When the call needs cc words, the saving is (m−1)(k−c)−c−2(m-1)(k-c)-c-2. 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 00, 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 pp, enter the subroutine. Then the instruction at EXIT jumps to p+1p+1, when it runs.

Node: 1.4.1-thm-linkage · Lean: not yet stated

Proof. The jump at pp is taken, so it sets rJ to p+1p+1. The entrance is the instruction STJ EXIT. Its field is (0:2)(0{:}2). 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 p+1p+1 and the index 00. By assumption nothing else changes the cell until the exit instruction runs, so the exit instruction jumps to p+1p+1. The register rJ holds a positive number that is less than the size of the memory, so the stored address is a valid address. □\square

The proof also shows the cost of the linkage: the call, the instruction STJ and the exit jump, which take 11, 22 and 11 u.

Example. A caller has JMP ABS at the address 101101, where ABS is the subroutine of the specification example below. At the entrance rJ is 102102. The first instruction of ABS is STJ EXIT, so the cell EXIT then holds a jump to 102102. A run in a simulator of MIX showed this: after the return, the next instruction that ran was the one at 102102.

Theorem (the saving of space, TAOCP eq. 1.4.1(2)). Let a piece of code of kk words occur in mm 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 m+k+2m+k+2 words for this code, the program without it has mkmk words, and the saving is (m−1)(k−1)−3(m-1)(k-1)-3 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 (m−1)(k−1)>3(m-1)(k-1) > 3. It is never positive for k=1k = 1 or m=1m = 1, because one factor is zero and the saving is −3-3.

Example. The table has the saving for small mm and kk. Each entry is (m−1)(k−1)−3(m-1)(k-1)-3. The entries were computed by a program.

k\mk \backslash m 1 2 3 4 5 6 7 8
2 −3-3 −2-2 −1-1 0 1 2 3 4
3 −3-3 −1-1 1 3 5 7 9 11
4 −3-3 0 3 6 9 12 15 18
5 −3-3 1 5 9 13 17 21 25

For k=2k=2 the saving is positive only for m≥5m \ge 5. For k≥5k \ge 5 it is positive already for m=2m = 2. A concrete case is k=3k = 3 and m=6m = 6. The three words LDA A, ADD B and STA C in six places take 1818 words. With a subroutine they take 66 call words, 33 words of code, one STJ and one exit, which is 1111 words, and 18−11=7=5⋅2−318 - 11 = 7 = 5 \cdot 2 - 3.

Proof. Without a subroutine each of the mm places has the kk words, so the total is mkmk. With a subroutine each of the mm places has a single call word, which gives mm words. The subroutine has the kk words of code, one word STJ and one word for the exit, which gives k+2k + 2. The total is m+k+2m + k + 2. The difference is mk−m−k−2mk - m - k - 2. Now (m−1)(k−1)=mk−m−k+1(m-1)(k-1) = mk - m - k + 1, so mk−m−k−2=(m−1)(k−1)−3mk - m - k - 2 = (m-1)(k-1) - 3. □\square

Theorem (the cost of time, TAOCP Section 1.4.1). If the subroutine is called tt times in a run, then the program with the subroutine takes 4t4t 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 11 u, the instruction STJ, which takes 22 u, and the exit jump, which takes 11 u. The body of the subroutine is the same code as the inline code and takes the same time. So each call adds 1+2+1=41+2+1 = 4 u, and tt calls add 4t4t. □\square

Example. The code LDA A, ADD B, STA C takes 2+2+2=62+2+2 = 6 u inline. As a subroutine, one use takes 1+2+6+1=101 + 2 + 6 + 1 = 10 u. The difference is 44 u. A loop that makes 10001000 calls pays 40004000 u for the saving of 77 words in the example above.

Theorem (a calling sequence of cc words, TAOCP eq. 1.4.1(3)). Let every call take cc words, with the jump among them, and let the code have kk words. Then the subroutine saves mk−(mc+k+2)=(m−1)(k−c)−c−2mk - (mc + k + 2) = (m-1)(k-c) - c - 2 words. For c=1c = 1 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 mm 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 cc words of its calling sequence, so the cost of one call is the time of those words plus 2+12 + 1 u for the instruction STJ and the exit, in place of the 44 u of the one-word call.

Example. Let k=6k = 6, c=2c = 2, m=4m = 4, for instance a call ENT1 n and JMP. Then the saving is (3)(4)−4=8(3)(4) - 4 = 8 words. Directly: the program without the subroutine has 2424 words, the one with it has 4⋅2+6+2=164 \cdot 2 + 6 + 2 = 16 words, and the difference is 88.

Proof. The count without a subroutine is mkmk. With it, the count is mcmc for the calls, kk for the code and 22 for the linkage. The difference is mk−mc−k−2mk - mc - k - 2. Now (m−1)(k−c)=mk−mc−k+c(m-1)(k-c) = mk - mc - k + c, so the difference is (m−1)(k−c)−c−2(m-1)(k-c) - c - 2. For c=1c = 1 the value is (m−1)(k−1)−3(m-1)(k-1) - 3. □\square

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: 44 u if rA is not negative and 99 u if it is, without the 11 u of the call.

A run for rA =−7= -7, with rX =9= 9, rI2 =4= 4 and the indicator set to greater, gave rA =7= 7, the same rX, rI2 and indicator, the value −7-7 in TMP, and 99 u. The run for rA =5= 5 gave 44 u, and the cell TMP was not changed. The cell TMP is in the list of changed cells even though the run for 55 left it alone, because the run for −7-7 changes it. A caller must not keep a value there.

Check. The path for a negative rA runs STJ (22), JANN (11, not taken), STA (22), ENTA (11), SUB (22) and the exit (11), and 2+1+2+1+2+1=92+1+2+1+2+1 = 9. The path for a nonnegative rA runs STJ, JANN with the jump taken, and the exit, and 2+1+1=42 + 1 + 1 = 4.

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 22 words and 33 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 00 in the text. After the store, the cell is ENT3 with the old value vv of rI3 as address, and the instruction at 3H loads vv in rI3 again. The instruction ST3 takes 22 u and ENT3 takes 11 u, so the cost is 33 u. The two instructions are two words. □\square

Example. The subroutine MAXN of the next part, with these two lines added, was run for n=3n = 3 and the data 3,8,53, 8, 5. It left rI3 =3= 3 at the exit, and it took 33 u more than the version without the two lines, 2828 u in place of 2525 u. In the version without them, rI3 is 00 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 X+1,…,X+nX+1, \ldots, X+n, with X =1000= 1000, and the subroutines start at the address 30003000.

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 n≥1n \ge 1. At the exit rA holds the largest of X[1],…,X[n]X[1], \ldots, X[n], rI2 holds the smallest index of a word with that value, rI1 =n= n and rI3 =0= 0. 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 uu is the number of indices jj with 2≤j≤n2 \le j \le n for which X[j]X[j] is greater than each of X[1],…,X[j−1]X[1], \ldots, X[j-1].

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 nn
3007 INC1 1 1 n−1n-1
3008 DEC3 1 1 n−1n-1
3009 CMPA X,1 2 n−1n-1
3010 JGE 1B 1 n−1n-1
3011 LDA X,1 2 uu
3012 ENT2 0,1 1 uu
3013 JMP 1B 1 uu
3014 EXIT JMP * 1 1

A run. Let n=3n = 3 and X[1],X[2],X[3]=3,8,5X[1], X[2], X[3] = 3, 8, 5, called from the address 101101 by ENT3 3 at 100100 and JMP MAXN at 101101. Then rJ is 102102 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 −1-1, 00 or 11 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 −1-1 102
10 3010 JGE 1B 1 13 3 2 1 1 −1-1 102
11 3011 LDA X,1 2 15 8 2 1 1 −1-1 102
12 3012 ENT2 0,1 1 16 8 2 2 1 −1-1 102
13 3013 JMP 1B 1 17 8 2 2 1 −1-1 3014
14 3006 J3Z EXIT 1 18 8 2 2 1 −1-1 3014
15 3007 INC1 1 1 19 8 3 2 1 −1-1 3014
16 3008 DEC3 1 1 20 8 3 2 0 −1-1 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 =8= 8 and rI2 =2= 2, the place of 88 in 3,8,53, 8, 5. At step 20 the cell EXIT holds a jump to 102102, since step 1 stored rJ there, so control goes to 102102. The time is 2525 u. Here u=1u = 1 and the formula below gives 6⋅3+3+4⋅1=256 \cdot 3 + 3 + 4 \cdot 1 = 25. The JGE at step 10 does not jump, since 3<83 < 8 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 rr, then rI1 =n−r= n - r, rA is the largest of X[1],…,X[n−r]X[1], \ldots, X[n-r], and rI2 is the smallest index of a word of that value in that range. At the first arrival, r=n−1r = n - 1. The lines at 30023002 to 30053005 have set rA =X[1]= X[1], rI2 =1= 1, rI1 =1= 1 and rI3 =n−1= n - 1, and the claim holds. Suppose that it holds for some rr. If r=0r = 0, the line J3Z jumps to the exit, and the claim says that rA is the largest of all nn words and rI2 is its smallest index, which is the specification. If r>0r > 0, then INC1 gives rI1 =n−r+1= n - r + 1, DEC3 gives rI3 =r−1= r - 1, and CMPA compares rA with X[n−r+1]X[n-r+1]. If rA ≥X[n−r+1]\ge X[n-r+1] 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 <X[n−r+1]< X[n-r+1], the lines at 30113011 and 30123012 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 r−1r - 1. The value of rI3 goes down by one at each pass, so after n−1n - 1 passes the value is 00 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 r=n−1,…,0r = n-1, \ldots, 0, which is nn times. The lines from INC1 to JGE run for r=n−1,…,1r = n-1, \ldots, 1, which is n−1n - 1 times. The three lines of the update run uu times, by the definition of uu. The sum is (2+2+1+1+1)+n⋅1+(n−1)(1+1+2+1)+u(2+1+1)+1=6n+4u+3,(2 + 2 + 1 + 1 + 1) + n \cdot 1 + (n-1)(1 + 1 + 2 + 1) + u(2 + 1 + 1) + 1 = 6n + 4u + 3, where the first group is for the lines at 30013001 to 30053005, and the last term is for the exit jump. The call JMP MAXN adds 11 u, and the entrance MAX100 adds the 11 u of ENT3. The time is least when X[1]X[1] is the largest (u=0u=0), which gives 6n+36n+3, and it is greatest when the words increase (u=n−1u = n-1), which gives 10n−110n - 1.

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 =100= 100 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 101101 and 5050 random sets of 100100 words in X[1],…,X[100]X[1], \ldots, X[100] was run with the listing of the previous program. Each run returned to 102102 with the largest word in rA and its smallest index in rI2. The call ENT3 4 with JMP MAXN, run on random sets of 44 words, did the same work for n=4n=4.

Proof. The instruction ENT3 100 sets rI3 to 100100 and does not change rJ, rA or the indicator. Then the body is the one of MAXN with n=100n = 100, and the correctness of MAXN gives the correctness of MAX100. The time is the time of MAXN for n=100n = 100, plus 11 u. □\square

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 5050 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. □\square

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 11 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 100100 and the word CON 5 at 101101. After STJ the instruction ENT1 held 101101, so rI1 became 101101. The load LD3 0,1 gave rI3 =5= 5. After INC1 1, the register rI1 was 102102, and the exit jump went to 102102, past the argument word. The subroutine was run on 5050 random data sets of five words, and each run halted at 103103, the place of the halt instruction after the argument, with the correct maximum.

Proof. At the entrance rJ is the address p+1p+1 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 p+1p+1. It sets rI1 to p+1p+1, and the load reads the argument, which is the word at p+1p+1 when index 1 is added to the address 00. Then the increase gives p+2p+2, and the store ST1 EXIT(0:2) makes the exit a jump to p+2p + 2. After this the subroutine runs as MAXN with rI3 =n= n. So the return goes to p+2p+2, as the calling sequence requires. □\square

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 00 or 11 at that moment.

Node: 1.4.1-(9) · Lean: not yet stated

Idea. The subroutine MAXN of the first program ends with rI3 =0= 0. A test of rA after the scan sets rI3 to 11 when rA is positive. Then the exit instruction, which has the index 33, adds rI3 to the address that STJ stored.

Example. The listing with the three entrances and the two exits was run on 5050 random data sets for each entrance. The data had values from −9-9 to 99. 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 33. 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 p+1p+1 plus rI3. If the largest value is not positive, the test leaves rI3 =0= 0 and the jump is to p+1p+1. Otherwise the instruction ENT3 1 sets rI3 to 11, and the jump is to p+2p+2. □\square

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 SS has one cell EXITS_S 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 S1,S2,…,SjS_1, S_2, \ldots, S_j, where Si+1S_{i+1} was called by SiS_i. The proof is by induction on the depth of the chain. When SiS_i is entered, the cell EXITSi_{S_i} gets the address of its caller. The cell is changed only by the entrance of SiS_i, and by assumption SiS_i is not entered again until it returns. So when SiS_i reaches its exit the cell has the address of the caller, and the return is correct. For the second part, suppose that SS is entered while it is open. The second STJ writes the new return address in the cell EXITS_S and the old address is no longer stored anywhere, so the first call cannot return to its caller. □\square

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 100100. The cell EXITA held a jump to 101101 after the first entrance and a jump to 30093009 after the second, so the address 101101 was lost. In the run of 20002000 steps, the program did not halt: the control passed between the addresses 30023002 and 30093009 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 nn words on a small input.

The source of this page. Something is wrong? Report it.