taocp

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

Volume P7 · Machines, programs and their correctness

A model machine and its instruction cycle

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 first
Contents
  1. The state and the step
  2. A small machine
  3. Time and space

The state and the step

Statement

Definition P7.2.1 (machine). A machine is a set QQ of states, a subset Ω⊆Q\Omega \subseteq Q of halted states, and a function f:Q→Qf : Q \to Q, the step, with f(q)=qf(q) = q for each q∈Ωq \in \Omega. The step of a halted state is the state itself.

Lean: TAOCP.CompMethod, a structure with the fields Q, I, Ω, f and fixes : ∀ q ∈ Ω, f q = q. It has the set I of input states that the definition above leaves out.

Example P7.2.2. A countdown machine has Q=ℕQ = \mathbb{N}, Ω={0}\Omega = \{0\} and f(n)=n−1f(n) = n - 1 for n>0n > 0, f(0)=0f(0) = 0. The step of 00 is 00, so the condition holds.

Example P7.2.3. A machine with a program counter has Q=ℕ×ℤQ = \mathbb{N} \times \mathbb{Z}, a state (p,v)(p, v), Ω={(p,v):p≥3}\Omega = \{(p, v) : p \ge 3\} and f(p,v)=(p+1,v+p)f(p, v) = (p + 1, v + p) for p<3p < 3 and f(p,v)=(p,v)f(p, v) = (p, v) otherwise. It adds 0,1,20, 1, 2 to vv and stops at p=3p = 3.

Definition P7.2.4 (run). The run from qq is the sequence q0=qq_0 = q, qk+1=f(qk)q_{k+1} = f(q_k). Write fk(q)=qkf^k(q) = q_k for the state after kk steps. The run halts if fk(q)∈Ωf^k(q) \in \Omega for some kk, and its length is the least such kk.

Lean: TAOCP.CompMethod.run, with run_zero, run_succ, and TAOCP.CompMethod.Terminates for halting.

Example P7.2.5. The countdown machine from 55 has the states 5,4,3,2,1,05, 4, 3, 2, 1, 0. It halts with length 55.

Example P7.2.6. The second machine from (0,10)(0, 10) has the states (0,10)(0, 10), (1,10)(1, 10), (2,11)(2, 11), (3,13)(3, 13). It halts with length 33 and the value 1313.

Theorem P7.2.7 (two runs in a row). For all j,k≥0j, k \ge 0, fj+k(q)=fk(fj(q))f^{j+k}(q) = f^k(f^j(q)).

Lean: TAOCP.CompMethod.run_add (proved in the project).

Proof. Induction on jj. For j=0j = 0 both sides are fk(q)f^k(q). Suppose the claim for jj. Then fj+1+k(q)=fj+k+1(q)=f(fj+k(q))f^{j+1+k}(q) = f^{j+k+1}(q) = f(f^{j+k}(q)), and by the hypothesis this is f(fk(fj(q)))=fk+1(fj(q))f(f^k(f^j(q))) = f^{k+1}(f^j(q)). This is fk(f(fj(q)))=fk(fj+1(q))f^k(f(f^j(q))) = f^k(f^{j+1}(q)), since both sides apply ff k+1k+1 times to fj(q)f^j(q). □\square

Theorem P7.2.8 (halted states stay). If fj(q)∈Ωf^j(q) \in \Omega then fk(q)=fj(q)f^k(q) = f^j(q) for all k≥jk \ge j. So the state at the end of a halting run does not depend on how many extra steps are made.

Lean: TAOCP.CompMethod.run_of_mem_Ω and TAOCP.CompMethod.run_eq_of_le.

Proof. Let k=j+mk = j + m. By the previous theorem fk(q)=fm(fj(q))f^k(q) = f^m(f^j(q)). Since fj(q)∈Ωf^j(q) \in \Omega and ff fixes each element of Ω\Omega, fm(fj(q))=fj(q)f^m(f^j(q)) = f^j(q) by induction on mm. □\square

Definition P7.2.9 (the instruction cycle). The state of a stored program machine has a register part, a memory MM, and a program counter pp. The step is the composition of three parts. Fetch reads the word M[p]M[p]. Decode splits the word into an operation and its operands. Execute changes the registers, the memory and the program counter by the rule of the operation, with the program counter set to p+1p + 1 unless the operation jumps. A fault, for example an address outside the memory, leaves the machine in a halted state.

Lean: TAOCP.MIX.State.fetch, TAOCP.MIX.Word.decode, TAOCP.MIX.State.exec and TAOCP.MIX.State.step, which fetch the word at pc, add its time to the clock and run its instruction; a fault gives the status fault.

Example P7.2.10. Take a machine with the word M[3]=213M[3] = 213 meaning "store the accumulator at the cell 1313". Fetch with p=3p = 3 reads 213213. Decode gives the operation 22 and the address 1313. Execute puts the accumulator into M[13]M[13] and sets p=4p = 4.

Example P7.2.11. A jump word 508508 at the cell 77 sets pp to 88 and no more. A word with the operation 99, which is no instruction, makes a fault, and the state is halted at once.

Theorem P7.2.12 (determinism). From one state the machine has one run. Two runs that start at the same state agree at each step, and two runs that meet at one state agree from then on.

Proof. The step is a function, so fk(q)f^{k}(q) has one value for each qq and kk. If fj(q)=fj′(q′)f^{j}(q) = f^{j'}(q') then fj+m(q)=fm(fj(q))=fm(fj′(q′))=fj′+m(q′)f^{j + m}(q) = f^m(f^{j}(q)) = f^m(f^{j'}(q')) = f^{j' + m}(q') for each mm, by the theorem on two runs in a row. □\square

Examples

Run of the second machine. The table gives fk(0,10)f^k(0, 10).

kk state in Ω\Omega
0 (0,10)(0, 10) no
1 (1,10)(1, 10) no
2 (2,11)(2, 11) no
3 (3,13)(3, 13) yes
4 (3,13)(3, 13) yes

The state at k=4k = 4 equals the state at k=3k = 3, as the theorem on halted states says.

Two runs in a row. With j=2j = 2 and k=1k = 1, f3(0,10)=f1(f2(0,10))=f1(2,11)=(3,13)f^{3}(0, 10) = f^1(f^2(0, 10)) = f^1(2, 11) = (3, 13).

A run that does not halt. Take Q=ℕQ = \mathbb{N}, Ω=∅\Omega = \emptyset and f(n)=n+1f(n) = n + 1. No state is halted, so no run halts. A program that loops for ever is a machine of this type.

Exercises

  1. [10] The machine Q={0,…,9}Q = \{0, \dots, 9\}, f(n)=(n+3) mod 10f(n) = (n + 3) \bmod 10 has Ω=∅\Omega = \emptyset. Does the run from 00 visit every state? What is the run from 55?
  2. [10] Show that f(n)=n+1f(n) = n + 1 on ℕ\mathbb{N} is the step of a machine only when Ω\Omega is empty.
  3. [M15] Prove that if two states q,q′q, q' have fj(q)=fj′(q′)f^j(q) = f^{j'}(q') then the runs from qq and q′q' agree after a shift.
  4. [M18] Prove that the length of a halting run is unique, and that it is 00 if and only if q∈Ωq \in \Omega.
  5. [M20] Show by an example that the condition "ff fixes Ω\Omega" is needed for the theorem on halted states.
  6. [15] A machine has 100 cells and a program counter pp with 0≤p<1000 \le p < 100. What happens when the step of the instruction at p=99p = 99 sets p←100p \leftarrow 100? Give two reasonable rules.
  7. [M22] Let ff and gg be two steps on QQ with the same Ω\Omega. Prove that g∘fg \circ f is also a step on QQ with this Ω\Omega.
  8. [M20] A finite machine with ∣Q∣=n|Q| = n states does not halt from qq. Prove that its run from qq repeats a state within the first n+1n + 1 states, and that from then on it cycles.
  9. [20] Write the instruction cycle of the machine S8 of the next section in three lines, one for fetch, one for decode and one for execute.
  10. [M25] Let q0,q1,…q_0, q_1, \dots be a run in a finite machine and let μ\mu be the least index and λ\lambda the least period of the cycle. Show that μ+λ≤n\mu + \lambda \le n, where n=∣Q∣n = |Q|.

Lean

The machine of TAOCP 1.1 is TAOCP.CompMethod in TAOCP/Algorithm/CompMethod.lean. The run is CompMethod.run, and the halting is CompMethod.Terminates. The laws of this section are CompMethod.run_add, CompMethod.run_succ, CompMethod.run_of_mem_Ω and CompMethod.run_eq_of_le. The MIX machine refines this notion: its state is TAOCP.MIX.State, its step is TAOCP.MIX.State.step, its run is TAOCP.MIX.State.run, and TAOCP.MIX.State.run_add is the law of two runs in a row for it. The halted state is a state whose status is not running, and TAOCP.MIX.State.step_of_ne_running says that the step then does not change the state.

Notes and history

The idea of a machine as a state and a step function goes back to Turing in 1936, who described the step by a table of rules. TAOCP Section 1.1 defines an algorithm as a computational method with a step and a set of final states, and the handbook uses the same form for the machines. The names fetch, decode and execute are the names of the classical stored program computer of the 1940s.

A small machine

Statement

The machine S8 is small enough to run by hand. It is the model for the proofs of this volume until MIX takes over in chapter 3.

Definition P7.2.13 (the machine S8). The state of S8 is an accumulator AA, a program counter pp and a memory of sixteen cells M[0],…,M[15]M[0], \dots, M[15]. Each of AA and the cells holds an integer from −999-999 to 999999. A word w≥0w \ge 0 is an instruction: its operation is ⌊w/100⌋\lfloor w / 100 \rfloor and its address is a=w mod 100a = w \bmod 100. At the start A=0A = 0 and p=0p = 0. The eight operations are:

op name effect time
0 HLT the machine halts 1
1 LDA aa A←M[a]A \leftarrow M[a] 2
2 STA aa M[a]←AM[a] \leftarrow A 2
3 ADD aa A←A+M[a]A \leftarrow A + M[a] 2
4 SUB aa A←A−M[a]A \leftarrow A - M[a] 2
5 JMP aa p←ap \leftarrow a 1
6 JZ aa if A=0A = 0 then p←ap \leftarrow a 1
7 JN aa if A<0A < 0 then p←ap \leftarrow a 1

Each instruction adds the time of its row to the clock. Every instruction that does not jump sets p←p+1p \leftarrow p + 1, and so does a jump that is not taken. The machine faults and halts if the operation is 88 or 99, if a≥16a \ge 16 for an operation that uses an address, if p≥16p \ge 16, or if a result in AA is outside [−999,999][-999, 999].

Lean: the project has no model of S8. The MIX model of chapter 3 is TAOCP.MIX.State, with the same form: registers, memory, pc, time and status.

Example P7.2.14. The word 213213 is STA 1313, since 213=2⋅100+13213 = 2 \cdot 100 + 13. The word 608608 is JZ 88. The word 00 is HLT.

Example P7.2.15. With A=5A = 5, M[3]=4M[3] = 4, the word 403403 (SUB 33) gives A=1A = 1. The word 303303 (ADD 33) gives A=9A = 9. With A=600A = 600 and M[3]=500M[3] = 500 the word 303303 makes a fault, since 1100>9991100 > 999.

Definition P7.2.16 (the program T\mathrm{T}). The program T multiplies xx by yy, with y≥0y \ge 0, by yy additions. Its cells are:

cell word instruction cell contents
0 113 LDA 13 12 xx
1 608 JZ 8 13 yy
2 415 SUB 15 14 00 (the product PP)
3 213 STA 13 15 11
4 114 LDA 14
5 312 ADD 12
6 214 STA 14
7 500 JMP 0
8 0 HLT

The cells 9,10,119, 10, 11 hold 00.

Theorem P7.2.17 (correctness and time of T). Let xx and yy be integers with y≥0y \ge 0 and ∣xy∣≤999|xy| \le 999 and ∣x∣≤999|x| \le 999. The program T halts with M[14]=xyM[14] = xy and uses 14y+414y + 4 units of time.

Proof. Name y0=yy_0 = y the initial value of cell 1313. The invariant at the cell 00 is: M[13]=rM[13] = r with 0≤r≤y00 \le r \le y_0 and M[14]=x(y0−r)M[14] = x (y_0 - r), and the cells 1212 and 1515 still hold xx and 11.

At the start r=y0r = y_0 and M[14]=0=x⋅0M[14] = 0 = x \cdot 0. The invariant holds. Take a pass through the cells 00 to 77 with the invariant true and r>0r > 0. The LDA puts rr in AA, and JZ does not jump since r≠0r \ne 0. SUB gives A=r−1A = r - 1 and STA stores it, so M[13]=r−1M[13] = r - 1. LDA 1414 loads x(y0−r)x(y_0 - r), ADD 1212 adds xx to give x(y0−r+1)=x(y0−(r−1))x(y_0 - r + 1) = x(y_0 - (r - 1)), and STA puts it in M[14]M[14]. The jump goes to the cell 00 with the invariant true for r−1r - 1. All values in AA are rr, r−1r - 1, x(y0−r)x(y_0 - r) and x(y0−r+1)x(y_0 - r + 1). The last is the partial product of xx and a number up to y0y_0, which has the absolute value at most ∣xy0∣≤999|xy_0| \le 999 when x≠0x \ne 0, so there is no fault. The other values are at most y0≤999y_0 \le 999 in the absolute value. A pass with r>0r > 0 costs 2+1+2+2+2+2+2+1=142 + 1 + 2 + 2 + 2 + 2 + 2 + 1 = 14 units. If r=0r = 0, LDA loads 00, the JZ jumps to the cell 88 and the HLT halts. This end costs 2+1+1=42 + 1 + 1 = 4 units. At the end M[14]=x(y0−0)=xy0M[14] = x(y_0 - 0) = x y_0.

The loop passes through the cell 00 with r=y0,y0−1,…,1,0r = y_0, y_0 - 1, \dots, 1, 0, which is y0+1y_0 + 1 times. The passes with r>0r > 0 are y0y_0 in number. The variant is rr: it falls by one in each pass and cannot go below 00, so the loop ends after y0y_0 passes. The time is 14y0+414 y_0 + 4. □\square

Lean: not yet stated, because the project has no model of S8. The same proof for the MIX program of the maximum is TAOCP.MIX.MaxExample.maxSpec (chapter 12).

Examples

A trace. Run T with x=7x = 7 and y=2y = 2. The cells hold [113,608,415,213,114,312,214,500,0,0,0,0,7,2,0,1][113, 608, 415, 213, 114, 312, 214, 500, 0, 0, 0, 0, 7, 2, 0, 1]. The table gives the state after each instruction: the instruction executed, the value of AA, the next pp and the clock.

step pp instruction AA after next pp clock
1 0 LDA 13 2 1 2
2 1 JZ 8 2 2 3
3 2 SUB 15 1 3 5
4 3 STA 13 1 4 7
5 4 LDA 14 0 5 9
6 5 ADD 12 7 6 11
7 6 STA 14 7 7 13
8 7 JMP 0 7 0 14
9 0 LDA 13 1 1 16
10 1 JZ 8 1 2 17
11 2 SUB 15 0 3 19
12 3 STA 13 0 4 21
13 4 LDA 14 7 5 23
14 5 ADD 12 14 6 25
15 6 STA 14 14 7 27
16 7 JMP 0 14 0 28
17 0 LDA 13 0 1 30
18 1 JZ 8 0 8 31
19 8 HLT 0 9 32

At the end M[14]=14=7⋅2M[14] = 14 = 7 \cdot 2 and the clock is 32=14⋅2+432 = 14 \cdot 2 + 4. At the end of each pass, rows 8 and 16, the invariant gives M[13]=1M[13] = 1 and M[14]=7=7⋅(2−1)M[14] = 7 = 7 \cdot (2 - 1), then M[13]=0M[13] = 0 and M[14]=14M[14] = 14.

Other inputs. For x=7x = 7 and y=3y = 3 the run takes 4646 units and gives 2121. For x=5x = 5 and y=0y = 0 the run takes 44 units and gives 00. For x=9x = 9 and y=1y = 1 it takes 1818 units and gives 99. All three agree with 14y+414y + 4.

A fault. With x=600x = 600 and y=2y = 2, the first ADD gives 600600 and the second gives 1200>9991200 > 999. The machine faults in the second pass, and the product is not computed. The hypothesis ∣xy∣≤999|xy| \le 999 of the theorem excludes this input.

Exercises

  1. [10] Write the words of the instructions LDA 5, ADD 12, JN 3 and HLT.
  2. [10] What does the word 709709 do when A=−4A = -4? When A=0A = 0?
  3. [15] Run T with x=−3x = -3 and y=2y = 2, and give the final M[14]M[14] and the time.
  4. [20] Write a program for S8 that sets M[14]M[14] to the larger of M[12]M[12] and M[13]M[13]. Give its time for M[12]=4M[12] = 4, M[13]=9M[13] = 9 and for M[12]=9M[12] = 9, M[13]=4M[13] = 4.
  5. [20] Write a program that computes ∣M[12]∣|M[12]| into M[13]M[13], with one JN. The cell 1111 holds 00.
  6. [M20] Show that in T the cell 1515 and the cell 1212 never change.
  7. [M22] Run T with x=7x = 7 and y=−1y = -1. Show that the run ends in a fault, and find the pass and the instruction where the fault occurs.
  8. [M20] Let y>0y > 0 and ∣x∣≤999|x| \le 999 with ∣xy∣>999|xy| > 999. Find the pass of T in which the fault occurs, and say what M[14]M[14] holds then.
  9. [20] Give a program for S8 that counts M[12]≥0M[12] \ge 0 down to 00 and stores the number of passes in M[13]M[13]. The cell 1515 holds 11. What is its time for M[12]=5M[12] = 5?
  10. [M23] Show that no program for S8 can compute 2202^{20}, since 220>9992^{20} > 999. What is the largest power of 22 that fits?
  11. [25] Count the possible states of S8. Give a bound for the length of a run that does not repeat a state, and say why that bound is useless in practice.

Lean

The project has no Lean model of S8. The same kind of statement for the MIX machine is the triple TAOCP.MIX.Triple, which gives the exact time of a piece of code between an entry and an exit. Chapter 12 shows it on TAOCP.MIX.MaxExample.maxSpec, which has the shape of the theorem of this section: a precondition, a postcondition, and a time that is a formula in the data.

Notes and history

Teaching machines with a handful of instructions are common in textbooks of computer organisation, from the decimal machines of the 1950s to the register machines of the logic books. TAOCP uses MIX for the same purpose, with a word of five bytes and a larger set of instructions. S8 is the writer's own design. The decimal word of three digits and a sign is chosen so that each instruction and each number is easy to read on a trace.

Time and space

Statement

A program has a result and a cost. The cost of a run is its time and its space. This section defines both for a stored program machine and gives the rule that the later chapters use to find the time of a loop.

Definition P7.2.18 (cost of an instruction). A cost function τ\tau gives to each instruction a number of time units τ≥1\tau \ge 1. The time of a halting run that executes the instructions I1,…,ImI_1, \dots, I_m in this order is τ(I1)+⋯+τ(Im)\tau(I_1) + \cdots + \tau(I_m). The instruction HLT is counted.

Lean: TAOCP.MIX.Instr.time gives the cost of a MIX instruction in the unit uu, from its operation code c and its modification f. The clock of a state is the field time of TAOCP.MIX.State. For a general machine, TAOCP.CompMethod.cost is the sum of a cost function over the states of a run, with TAOCP.CompMethod.cost_add for two runs in a row.

Example P7.2.19. In S8 the costs are τ(LDA)=τ(STA)=τ(ADD)=τ(SUB)=2\tau(\mathrm{LDA}) = \tau(\mathrm{STA}) = \tau(\mathrm{ADD}) = \tau(\mathrm{SUB}) = 2 and τ(JMP)=τ(JZ)=τ(JN)=τ(HLT)=1\tau(\mathrm{JMP}) = \tau(\mathrm{JZ}) = \tau(\mathrm{JN}) = \tau(\mathrm{HLT}) = 1. The two instructions LDA 1313 and HLT in a row cost 33.

Example P7.2.20. In MIX, Instr.time_load says that each load (the operation codes 88 to 2323) takes 2u2u, Instr.time_jump says that each jump (codes 3434 to 5555) takes 1u1u, and Instr.time_move says that MOVE with FF words takes 1+2F1 + 2F units.

Definition P7.2.21 (frequency). Let a halting run have its instructions at the addresses p1,…,pmp_1, \dots, p_m. The frequency NaN_a of the address aa is the number of ii with pi=ap_i = a.

Theorem P7.2.22 (time as a sum over the addresses). The time of the run is ∑aNaτa\sum_a N_a \tau_a, where τa\tau_a is the cost of the instruction at the address aa and the sum is over all addresses of the program. The number of steps is ∑aNa\sum_a N_a.

Lean: TAOCP.CompMethod.cost_eq_sum_visits and TAOCP.CompMethod.cost_steps_eq_sum_count (proved in the project, for a cost that is constant on the sets LaL_a of states at the address aa); TAOCP.CompMethod.visits counts the visits to a set of states.

Proof. Group the mm terms τ(Ii)\tau(I_i) by the address pip_i. The terms with the address aa are NaN_a in number and each is τa\tau_a when the program is not changed during the run. So their sum is NaτaN_a \tau_a. Each term is in exactly one group, so the sum of the groups is the time. The same grouping of the number m=∑i1m = \sum_i 1 gives ∑aNa\sum_a N_a. □\square

Theorem P7.2.23 (the flow law). Take a halting run that starts at the address ss and stops at the HLT at the address hh. For an address aa, let in(a)in(a) be the number of times that control enters aa from another instruction, and out(a)out(a) the number of times that control leaves aa for another instruction. Then Na=in(a)+[a=s]N_a = in(a) + [a = s] and Na=out(a)+[a=h]N_a = out(a) + [a = h]. So in(a)+[a=s]=out(a)+[a=h]in(a) + [a = s] = out(a) + [a = h].

Proof. Each execution of aa is either the first instruction of the run or it follows an execution of another instruction. In the first case a=sa = s and there is one such execution. In the second it is counted in in(a)in(a). Each execution of aa is either the last instruction of the run, or it is followed by another instruction, which is counted in out(a)out(a). The last instruction of a halting run is a HLT, and so it is the one at hh. □\square

The flow law is the Kirchhoff law of the program: the flow into a node is the flow out of it, with one unit put in at the start and one taken out at the end. Chapter 10 uses it to find the frequencies of a program without a trace.

Definition P7.2.24 (space). The space of a program is the number of memory cells that it uses, in code and in data. A cell that the program reads or writes counts, and a cell that the program does not touch does not count.

Example P7.2.25. T uses the cells 00 to 88 for code and 1212 to 1515 for data, so it uses 1313 of the 1616 cells of S8.

Example P7.2.26. A program that uses cells 00 to 88 for code and all the other cells for data has the space 1616, the whole memory.

Examples

The frequencies of T. For an input y≥0y \ge 0, the frequencies are N0=N1=y+1N_0 = N_1 = y + 1, N2=⋯=N7=yN_2 = \dots = N_7 = y and N8=1N_8 = 1. The costs are 2,1,2,2,2,2,2,1,12, 1, 2, 2, 2, 2, 2, 1, 1 for the cells 00 to 88. The time is 2(y+1)+(y+1)+y(2+2+2+2+2+1)+1=14y+4.2(y+1) + (y+1) + y(2 + 2 + 2 + 2 + 2 + 1) + 1 = 14 y + 4. The number of steps is 2(y+1)+6y+1=8y+32(y + 1) + 6y + 1 = 8y + 3. For y=2y = 2 this is 1919, as many rows as the trace of the last section.

The flow law on T. At the cell 00: control is put in at the start, once, and enters from the cell 77 yy times, so N0=y+1N_0 = y + 1. It leaves 00 for the cell 11 every time, out(0)=y+1out(0) = y + 1. At the cell 11: in(1)=y+1in(1) = y + 1, and the exits are yy to the cell 22 and 11 to the cell 88. At the cell 88: in(8)=1in(8) = 1 and [8=h]=1[8 = h] = 1, so in(8)=out(8)+1in(8) = out(8) + 1 with out(8)=0out(8) = 0.

The flow graph for y=2y = 2 has these edges with their counts: 0→10 \to 1 three times, 1→21 \to 2 twice, 1→81 \to 8 once, 2→32 \to 3, 3→43 \to 4, 4→54 \to 5, 5→65 \to 6, 6→76 \to 7 twice each and 7→07 \to 0 twice. At each node the sum of the counts into it, plus one at the start node, equals the sum out of it, plus one at the end node.

Time for several inputs. The time 14y+414 y + 4 for y=0,1,…,9y = 0, 1, \dots, 9 is 4,18,32,46,60,74,88,102,116,1304, 18, 32, 46, 60, 74, 88, 102, 116, 130.

Another cost function. Let the cost be 33 for LDA, STA, ADD, SUB, 22 for the jumps and 11 for HLT. One pass of T then costs 3+2+3+3+3+3+3+2=223 + 2 + 3 + 3 + 3 + 3 + 3 + 2 = 22 and the final test costs 3+2+1=63 + 2 + 1 = 6. The time is 22y+622 y + 6. The frequencies do not change, only the costs.

Exercises

  1. [10] Use the frequencies to find the time of the maximum program of the answer to exercise 2.2-4 for M[12]=9M[12] = 9, M[13]=4M[13] = 4.
  2. [M15] Let each cost be between 11 and 22. Prove that a halting run of mm steps has the time TT with m≤T≤2mm \le T \le 2m.
  3. [15] Give NaN_a for each address of T with y=4y = 4, and the time by the sum.
  4. [M20] Prove the flow law for the cell 11 of T with y=4y = 4 by counting the transitions in the run.
  5. [20] The cost is 44 for each load or store, 11 for each jump and for HLT, 33 for ADD and SUB. Find the time of T.
  6. [M20] The input yy is uniform on 0,1,…,90, 1, \dots, 9. Find the mean time of T.
  7. [M22] A program has no loop: each instruction is executed at most once. Prove that the time is at most the sum of the costs of all its instructions.
  8. [10] How many cells of data can a program have in S8 when its code takes 99 cells?
  9. [20] Draw the flow graph of T with the counts for y=3y = 3.
  10. [M23] A program of nn instructions has a run of mm steps. Prove that some instruction is executed at least ⌈m/n⌉\lceil m / n \rceil times.
  11. [M25] Prove that a halting run of S8 has at most 16⋅19991716 \cdot 1999^{17} steps, and explain why a cycle must follow if the run is longer.

Lean

The cost of a run as a sum is TAOCP.CompMethod.cost, defined for a method M, a function τ on the states and a start state, with cost_zero, cost_add, cost_indicator, cost_of_mem_Ω, cost_eq_sum_visits and cost_steps_eq_sum_count. The time of a MIX instruction is TAOCP.MIX.Instr.time, with the theorems Instr.time_load, Instr.time_store, Instr.time_cmp, Instr.time_jump and Instr.time_move. The clock advances by this amount at each step, which is the line time := s.time + ins.time in TAOCP.MIX.State.step. The theorem TAOCP.MIX.Program.init_time says that a program starts with the clock at 00.

Notes and history

The count of the frequency of each instruction is the method of TAOCP Section 1.3.2 and of the analyses of Section 1.2.10, where the running time is a sum of frequencies times costs. Kirchhoff's law of the electric network is the law of the flow here, and TAOCP uses its name in the same sense. The units of time of MIX are the unit uu, and chapter 4 gives the costs υ\upsilon and μ\mu of MMIX.

A program means something only on a machine. This chapter defines a machine as a state and a step function, and it shows that every question about a run is a question about this function. The definition is the same for a MIX machine, an MMIX machine and a toy with eight instructions.

Section 1 gives the abstract definition: the state, the step, the halted states and the run. It proves the laws of the run, in particular that two runs in a row are one run. Section 2 builds a small machine, called S8, with one accumulator and sixteen cells, and it runs a program for multiplication by repeated addition, with a trace and a proof of its result. Section 3 adds the clock: the cost of an instruction, the time of a run as a sum, and the count of the executions of each instruction.

The chapter uses P1 (induction and functions). It is the base of chapters 3 and 4, where the model is MIX and MMIX, and of chapter 8, where the model is a language. The Lean parts name TAOCP.CompMethod of the project, the method of TAOCP Section 1.1, and the MIX declarations State.step and State.run that refine it.

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