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
The state and the step
Statement
Definition P7.2.1 (machine). A machine is a set of states, a subset of halted states, and a function , the step, with for each . 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 , and for , . The step of is , so the condition holds.
Example P7.2.3. A machine with a program counter has , a state , and for and otherwise. It adds to and stops at .
Definition P7.2.4 (run). The run from is the sequence , . Write for the state after steps. The run halts if for some , and its length is the least such .
Lean: TAOCP.CompMethod.run, with run_zero, run_succ, and TAOCP.CompMethod.Terminates for halting.
Example P7.2.5. The countdown machine from has the states . It halts with length .
Example P7.2.6. The second machine from has the states , , , . It halts with length and the value .
Theorem P7.2.7 (two runs in a row). For all , .
Lean: TAOCP.CompMethod.run_add (proved in the project).
Proof. Induction on . For both sides are . Suppose the claim for . Then , and by the hypothesis this is . This is , since both sides apply times to .
Theorem P7.2.8 (halted states stay). If then for all . 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 . By the previous theorem . Since and fixes each element of , by induction on .
Definition P7.2.9 (the instruction cycle). The state of a stored program machine has a register part, a memory , and a program counter . The step is the composition of three parts. Fetch reads the word . 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 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 meaning "store the accumulator at the cell ". Fetch with reads . Decode gives the operation and the address . Execute puts the accumulator into and sets .
Example P7.2.11. A jump word at the cell sets to and no more. A word with the operation , 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 has one value for each and . If then for each , by the theorem on two runs in a row.
Examples
Run of the second machine. The table gives .
| state | in | |
|---|---|---|
| 0 | no | |
| 1 | no | |
| 2 | no | |
| 3 | yes | |
| 4 | yes |
The state at equals the state at , as the theorem on halted states says.
Two runs in a row. With and , .
A run that does not halt. Take , and . No state is halted, so no run halts. A program that loops for ever is a machine of this type.
Exercises
- [10] The machine , has . Does the run from visit every state? What is the run from ?
- [10] Show that on is the step of a machine only when is empty.
- [M15] Prove that if two states have then the runs from and agree after a shift.
- [M18] Prove that the length of a halting run is unique, and that it is if and only if .
- [M20] Show by an example that the condition " fixes " is needed for the theorem on halted states.
- [15] A machine has 100 cells and a program counter with . What happens when the step of the instruction at sets ? Give two reasonable rules.
- [M22] Let and be two steps on with the same . Prove that is also a step on with this .
- [M20] A finite machine with states does not halt from . Prove that its run from repeats a state within the first states, and that from then on it cycles.
- [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.
- [M25] Let be a run in a finite machine and let be the least index and the least period of the cycle. Show that , where .
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 , a program counter and a memory of sixteen cells . Each of and the cells holds an integer from to . A word is an instruction: its operation is and its address is . At the start and . The eight operations are:
| op | name | effect | time |
|---|---|---|---|
| 0 | HLT | the machine halts | 1 |
| 1 | LDA | 2 | |
| 2 | STA | 2 | |
| 3 | ADD | 2 | |
| 4 | SUB | 2 | |
| 5 | JMP | 1 | |
| 6 | JZ | if then | 1 |
| 7 | JN | if then | 1 |
Each instruction adds the time of its row to the clock. Every instruction that does not jump sets , and so does a jump that is not taken. The machine faults and halts if the operation is or , if for an operation that uses an address, if , or if a result in is outside .
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 is STA , since . The word is JZ . The word is HLT.
Example P7.2.15. With , , the word (SUB ) gives . The word (ADD ) gives . With and the word makes a fault, since .
Definition P7.2.16 (the program ). The program T multiplies by , with , by additions. Its cells are:
| cell | word | instruction | cell | contents |
|---|---|---|---|---|
| 0 | 113 | LDA 13 | 12 | |
| 1 | 608 | JZ 8 | 13 | |
| 2 | 415 | SUB 15 | 14 | (the product ) |
| 3 | 213 | STA 13 | 15 | |
| 4 | 114 | LDA 14 | ||
| 5 | 312 | ADD 12 | ||
| 6 | 214 | STA 14 | ||
| 7 | 500 | JMP 0 | ||
| 8 | 0 | HLT |
The cells hold .
Theorem P7.2.17 (correctness and time of T). Let and be integers with and and . The program T halts with and uses units of time.
Proof. Name the initial value of cell . The invariant at the cell is: with and , and the cells and still hold and .
At the start and . The invariant holds. Take a pass through the cells to with the invariant true and . The LDA puts in , and JZ does not jump since . SUB gives and STA stores it, so . LDA loads , ADD adds to give , and STA puts it in . The jump goes to the cell with the invariant true for . All values in are , , and . The last is the partial product of and a number up to , which has the absolute value at most when , so there is no fault. The other values are at most in the absolute value. A pass with costs units. If , LDA loads , the JZ jumps to the cell and the HLT halts. This end costs units. At the end .
The loop passes through the cell with , which is times. The passes with are in number. The variant is : it falls by one in each pass and cannot go below , so the loop ends after passes. The time is .
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 and . The cells hold . The table gives the state after each instruction: the instruction executed, the value of , the next and the clock.
| step | instruction | after | next | 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 and the clock is . At the end of each pass, rows 8 and 16, the invariant gives and , then and .
Other inputs. For and the run takes units and gives . For and the run takes units and gives . For and it takes units and gives . All three agree with .
A fault. With and , the first ADD gives and the second gives . The machine faults in the second pass, and the product is not computed. The hypothesis of the theorem excludes this input.
Exercises
- [10] Write the words of the instructions LDA 5, ADD 12, JN 3 and HLT.
- [10] What does the word do when ? When ?
- [15] Run T with and , and give the final and the time.
- [20] Write a program for S8 that sets to the larger of and . Give its time for , and for , .
- [20] Write a program that computes into , with one JN. The cell holds .
- [M20] Show that in T the cell and the cell never change.
- [M22] Run T with and . Show that the run ends in a fault, and find the pass and the instruction where the fault occurs.
- [M20] Let and with . Find the pass of T in which the fault occurs, and say what holds then.
- [20] Give a program for S8 that counts down to and stores the number of passes in . The cell holds . What is its time for ?
- [M23] Show that no program for S8 can compute , since . What is the largest power of that fits?
- [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 gives to each instruction a number of time units . The time of a halting run that executes the instructions in this order is . The instruction HLT is counted.
Lean: TAOCP.MIX.Instr.time gives the cost of a MIX instruction in the unit , 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 and . The two instructions LDA and HLT in a row cost .
Example P7.2.20. In MIX, Instr.time_load says that each load (the operation codes to ) takes , Instr.time_jump says that each jump (codes to ) takes , and Instr.time_move says that MOVE with words takes units.
Definition P7.2.21 (frequency). Let a halting run have its instructions at the addresses . The frequency of the address is the number of with .
Theorem P7.2.22 (time as a sum over the addresses). The time of the run is , where is the cost of the instruction at the address and the sum is over all addresses of the program. The number of steps is .
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 of states at the address ); TAOCP.CompMethod.visits counts the visits to a set of states.
Proof. Group the terms by the address . The terms with the address are in number and each is when the program is not changed during the run. So their sum is . Each term is in exactly one group, so the sum of the groups is the time. The same grouping of the number gives .
Theorem P7.2.23 (the flow law). Take a halting run that starts at the address and stops at the HLT at the address . For an address , let be the number of times that control enters from another instruction, and the number of times that control leaves for another instruction. Then and . So .
Proof. Each execution of is either the first instruction of the run or it follows an execution of another instruction. In the first case and there is one such execution. In the second it is counted in . Each execution of is either the last instruction of the run, or it is followed by another instruction, which is counted in . The last instruction of a halting run is a HLT, and so it is the one at .
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 to for code and to for data, so it uses of the cells of S8.
Example P7.2.26. A program that uses cells to for code and all the other cells for data has the space , the whole memory.
Examples
The frequencies of T. For an input , the frequencies are , and . The costs are for the cells to . The time is The number of steps is . For this is , as many rows as the trace of the last section.
The flow law on T. At the cell : control is put in at the start, once, and enters from the cell times, so . It leaves for the cell every time, . At the cell : , and the exits are to the cell and to the cell . At the cell : and , so with .
The flow graph for has these edges with their counts: three times, twice, once, , , , , twice each and 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 for is .
Another cost function. Let the cost be for LDA, STA, ADD, SUB, for the jumps and for HLT. One pass of T then costs and the final test costs . The time is . The frequencies do not change, only the costs.
Exercises
- [10] Use the frequencies to find the time of the maximum program of the answer to exercise 2.2-4 for , .
- [M15] Let each cost be between and . Prove that a halting run of steps has the time with .
- [15] Give for each address of T with , and the time by the sum.
- [M20] Prove the flow law for the cell of T with by counting the transitions in the run.
- [20] The cost is for each load or store, for each jump and for HLT, for ADD and SUB. Find the time of T.
- [M20] The input is uniform on . Find the mean time of T.
- [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.
- [10] How many cells of data can a program have in S8 when its code takes cells?
- [20] Draw the flow graph of T with the counts for .
- [M23] A program of instructions has a run of steps. Prove that some instruction is executed at least times.
- [M25] Prove that a halting run of S8 has at most 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 .
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 , and chapter 4 gives the costs and 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.