Volume P7 · Machines, programs and their correctness
MIX as a case study
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
Registers and words
Statement
A MIX machine has a small set of places that hold numbers. This section names them, gives the range of each, and proves the rule by which the sum of two words may leave the range. The state defined here is the state of chapter 2 for MIX.
Definition P7.3.1 (the state of MIX). A MIX machine with byte size has these parts.
| part | name | holds | range of the magnitude |
|---|---|---|---|
| accumulator | A | a word: a sign and five bytes | to |
| extension | X | a word | to |
| index registers | I1 to I6 | a half word: a sign and two bytes | to |
| jump register | J | a half word with the sign always plus | to |
| memory | cells to | a word each | to |
| overflow toggle | OV | on or off | |
| comparison indicator | CI | less, equal or greater | |
| counter | the address of the next instruction | to | |
| clock | the time used, in units | ||
| status | running, halted or fault |
The registers A, X, I1 to I6 and J are the registers. The registers I1 to I6 are the index registers. The value of a word or of a half word is its sign times its magnitude, as in P7.1.
Lean: TAOCP.MIX.State has the fields a, x, i (a function from Fin 6 to half words), j, ov, cmp (of type Ordering), mem, pc, time and status. The memory is TAOCP.MIX.Mem, a function from Fin memSize to Word b, with TAOCP.MIX.memSize = 4000. The status is TAOCP.MIX.Status, with the cases running, halted and fault, and the reason of a fault is a TAOCP.MIX.Fault. A word is TAOCP.MIX.Word b and a half word is TAOCP.MIX.Half b.
Example P7.3.2. With let A hold . The magnitude is , so the value of A is .
Example P7.3.3. The register I3 holds . Its value is . The register J holds after a jump that comes from the cell , since .
Definition P7.3.4 (the byte size). The byte size of MIX is a fixed number with . A byte is a number from to .
Theorem P7.3.5 (why ). A half word can hold every address of the memory if and only if . This holds for , and it fails for .
Proof. The addresses are , so they fit in the magnitude of a half word exactly when , that is . For we have , and grows with . For we have .
Lean: TAOCP.MIX.ByteSize is the class of the byte sizes with .
Example P7.3.6. For the last address has the bytes , since . For the same address is the bytes .
Theorem P7.3.7 (range of the registers). The magnitude of A and of X is less than , so the value is between and . The magnitude of an index register and of J is less than . For these bounds are and .
Proof. A word has five bytes, each less than , so by the bound of a row in P7.1 the magnitude is at most . The same holds for two bytes and . For : and .
Lean: TAOCP.MIX.Word.mag_lt, TAOCP.MIX.Half.mag_lt, TAOCP.MIX.Word.abs_value_lt and TAOCP.MIX.Half.natAbs_value_lt.
Example P7.3.8. The largest word has the value . Add to it, and the true sum is not a value of a word. The next theorem says what the machine then does.
Definition P7.3.9 (the sum with a flag). Let be a word with value and let be an integer. Put . The sum with a flag of and is the word with the magnitude and the flag that is true when . The sign of the result is for , for , and the sign of for .
Lean: TAOCP.MIX.Word.addInt returns the pair of the word and the flag. The sign rule is TAOCP.MIX.Sign.ofInt.
Theorem P7.3.10 (the overflow rule). The instructions ADD and SUB, and INCA and DECA, replace A by the sum with a flag of A and the integer , and they switch OV on when the flag is true. They never switch OV off. If the flag is false, the new value of A is .
Proof. The first claim is the definition of the instructions in the model. The last claim: when the magnitude is and the sign is the sign of , so the value is , except for , where both signs give the value .
Lean: TAOCP.MIX.Word.value_ofInt is the last claim: the word of an integer with has the value . The model sets ov := s.ov || o in TAOCP.MIX.State.execAdd and TAOCP.MIX.State.addReg, so a flag never turns OV off.
Example P7.3.11. Let A hold and add the word . The sum is , the magnitude modulo is , and the flag is true. A becomes and OV is on. The machine lost the high part: the new value is the true sum minus .
Example P7.3.12. Let A hold , with the value , and subtract the word with the value . The true result is , the magnitude modulo is and the flag is true. A becomes with OV on.
Theorem P7.3.13 (no overflow for opposite signs). If and are not both positive and not both negative, that is , then the sum of and does not overflow.
Proof. If then , so because both are values of the words (an integer that comes from a word of the memory has ). The case is the same.
Example P7.3.14. A holds and the cell holds . The sum is , no overflow. Since , the sign of the result is the sign of A: the result is . If A holds and the cell holds , the result is . Both have the value .
Definition P7.3.15 (the comparison indicator). The instructions CMPA, CMPX and CMP1 to CMP6 compare a register with a cell and set CI to less, equal or greater by the order of the values. No other instruction of the groups of this section changes CI.
Lean: TAOCP.MIX.State.execCmp sets cmp to compare of the two field values, an Ordering.
Example P7.3.16. If A holds and the cell holds , then CMPA gives less. If A holds and the cell holds , CMPA gives equal, since the comparison uses the values.
Definition P7.3.17 (the start state of the model). The book does not say what the registers hold before a program runs. The model of the project starts with all registers , OV off, CI equal, the clock , and the counter at the first instruction. A correct program must not use this choice.
Lean: TAOCP.MIX.Program.init, with TAOCP.MIX.Program.init_time and TAOCP.MIX.Program.init_pc. A TAOCP.MIX.Program is a memory image and a start address.
Examples
A state. Let and consider the machine with A , X , I1 , the other index registers , J , OV off, CI equal, and . Its values are for A, for X and for I1.
Two additions in a row. From the state above, add the word with the value twice. The first sum is , so A holds the magnitude and OV is on. The second sum is , so A holds and OV stays on. After the two additions the value of A is and OV is on, while the true sum is . The machine keeps the sum modulo and the toggle records that something was lost.
The sign of zero. Start with A . Run SUB with a cell of value . The result is and A has the sign minus: . A jump instruction that tests the sign of A, as JAN does, treats as zero, because the test uses the value: JAN does not jump, JAZ jumps. The next section lists the jumps.
Exercises
- [10] With give the value of the register A that holds .
- [10] With what are the largest value of A and the largest value of an index register?
- [15] With let A hold and run ADD with a cell of value . Give the value of A and the state of OV.
- [15] A holds and OV is on. Run ADD with a cell of value . Give A, with its sign, and OV.
- [M22] Show that if two integers and are values of words and overflows, then and have the same sign, and the machine result has the value when both are positive.
- [M25] How many different words are there for the byte size ? How many different values do they have? Give both numbers for .
- [M20] Show that a machine with the byte size could not address all the cells, and find the least with .
- [15] The instruction JMP in the cell is run. What does J hold, as a half word, with ?
Lean
The state and the registers are in TAOCP/MIX/Machine.lean and TAOCP/MIX/Word.lean: TAOCP.MIX.State, TAOCP.MIX.Status, TAOCP.MIX.Fault, TAOCP.MIX.Mem, TAOCP.MIX.Program, TAOCP.MIX.Word, TAOCP.MIX.Half. The range of a register is TAOCP.MIX.Word.mag_lt and TAOCP.MIX.Half.mag_lt. The sum with a flag is the definition TAOCP.MIX.Word.addInt, and TAOCP.MIX.Word.value_ofInt gives the exact value when there is no overflow. The theorems of this section on the sign rule and on the opposite signs are not stated in the project.
Notes and history
MIX is the machine of TAOCP. Its design copies the shape of the decimal and binary computers of the early 1960s, with a sign and magnitude word, one accumulator, an extension register and index registers. The byte size is left open on purpose, so that a program works on a binary machine and on a decimal machine. The model of the project fixes as a parameter with the bounds of this section. The start state in Program.init is a convention of the project, and the book leaves it open.
Instruction format and timing
Statement
An instruction of MIX is a word of the memory. This section cuts the word into its parts, computes the cell that the instruction uses, and gives the time of each group of instructions. The step function of MIX reads the word at the counter , decodes it by these rules and runs it.
Definition P7.3.18 (the instruction word). The word or is the instruction with the address where , the index specification , the modification and the operation code . In the field notation of P7.1, is the field , is , is and is .
Lean: TAOCP.MIX.Instr is the structure with sign, aa, i, f and c. The functions are TAOCP.MIX.Word.decode and TAOCP.MIX.Instr.encode, the signed address is TAOCP.MIX.Instr.addr, and TAOCP.MIX.Instr.mk' builds an instruction from a signed address.
Example P7.3.19. With the instruction that loads A from the cell with the field has , , and . Its word is .
Example P7.3.20. The instruction that stores X into the field of the cell has , the sign , , and . Its word is .
Theorem P7.3.21 (decode and encode). Every word is the encoding of its decoding. Every instruction whose parts fit in their bytes, that is , , and , is the decoding of its encoding.
Proof. The encoding writes as the two bytes and , and the other parts as bytes. The decoding forms again, and the identity of the division gives back . The bytes , and are copied. For a word, the decoding gives , and dividing by gives and the remainder , because . The sign is copied in both directions.
Lean: TAOCP.MIX.Word.encode_decode and TAOCP.MIX.Instr.decode_encode with the four hypotheses above.
Definition P7.3.22 (the effective address). Let . The effective address of the instruction is when , and when . The sum must have a magnitude below , and . The model stops with the fault index if one of the two fails. A memory instruction needs , and the model stops with the fault address if it fails.
Lean: TAOCP.MIX.State.effAddr, TAOCP.MIX.State.readCell and TAOCP.MIX.State.writeCell, with the faults TAOCP.MIX.Fault.index and TAOCP.MIX.Fault.address.
Example P7.3.23. The instruction has and . With rI3 the address is . With rI3 the address is . With the address is for any rI3.
Example P7.3.24. With rI2 the instruction LDA has . The cell does not exist, so the model stops with the fault address. In the same machine ENTA has and does no memory access, so it sets A to and does not fault.
Definition P7.3.25 (the register code). The operation codes to are the loads LDA, LD1 to LD6, LDX in this order. The codes to are the negative loads, to are the stores of the same registers, to are the jumps on a register, to are the address transfers INC, DEC, ENT and ENN, and to are the comparisons. In each of these groups the register is determined by : the value is A, the values to are I1 to I6 and the value is X.
Lean: TAOCP.MIX.Reg.ofOffset maps to the register, and TAOCP.MIX.State.exec dispatches on .
Example P7.3.26. The code has , a store, and , so it is ST2. The code has in the comparison group, so it is CMPX. The code is in the transfer group with , so it is one of INCA, DECA, ENTA and ENNA, chosen by .
Theorem P7.3.27 (load by field). Let the cell hold the word and let the field be . The instruction LD with this field puts into the register the word whose magnitude is the field magnitude of , right justified, and whose sign is the sign of if and otherwise. The instruction LDN puts the same magnitude with the opposite sign. For an index register the instruction needs the bytes to of the result to be zero.
Proof. The statement is the definition of load in P7.1 together with the rule of the negative load. The magnitude is by the theorem on fields, and a value below is a row of the bytes with zero bytes to the left. If these zero bytes include the bytes to of the register.
Lean: TAOCP.MIX.Word.load with TAOCP.MIX.Word.mag_load, TAOCP.MIX.Word.sign_load and TAOCP.MIX.Word.value_load. The model executes it in TAOCP.MIX.State.execLoad and stops with the fault register when an index register would get a non-zero byte to .
Example P7.3.28. The cell holds . LDA with puts in A: the bytes right justified, the sign plus because . The value is . LDA with gives with the value . LDAN with gives .
Example P7.3.29. LD1 with on the same cell puts in I1, the value . LD1 with would need the bytes of the register to be zero, and they are not, so the model stops with the fault register.
Theorem P7.3.30 (store by field). The instruction ST with the field puts the low bytes of the register into the field of the cell, and it puts the sign of the register in the sign of the cell only if . No other part of the cell changes. STZ stores a word of zeros in the same way. STJ is ST of the register J with the sign plus, and the default field of STJ is .
Proof. This is the theorem on store of P7.1 applied to the word of the register. For STJ, J holds two bytes, so the word of J has the bytes to equal to zero, and the field takes its low bytes into the bytes and of the cell, which is the address part of an instruction.
Lean: TAOCP.MIX.Word.store, with TAOCP.MIX.Word.mag_store, TAOCP.MIX.Word.sign_store, TAOCP.MIX.Word.store_high, TAOCP.MIX.Word.store_low and TAOCP.MIX.Word.load_store. The model has TAOCP.MIX.State.execStore.
Theorem P7.3.31 (multiplication). The instruction MUL with the cell value and the field value of the word takes the product of the magnitudes , writes its high five bytes into A and its low five bytes into X. Both signs are the product of the sign of A and the sign of , also for the product zero. No overflow is possible, because .
Proof. Write . Then with and , and since . A gets and X gets .
Lean: TAOCP.MIX.Word.mul returns the pair and TAOCP.MIX.State.execMul executes it.
Example P7.3.32. A holds and the cell holds . The product is . So A gets and X gets . If the cell holds , both A and X get the sign minus.
Theorem P7.3.33 (division). Let the pair A, X have the magnitude and let the divisor have the value . If the instruction DIV puts the quotient in A with the sign of the product of the sign of A and the sign of , and it puts the remainder in X with the sign of A. If the quotient does not fit in a word. The model then sets OV and leaves A and X unchanged.
Proof. We show that exactly when . If then , so the quotient is below . If then , so the quotient is at least . When the quotient fits, with is the division with remainder.
Lean: TAOCP.MIX.Word.div returns none when the magnitude of the divisor is at most the magnitude of A, and TAOCP.MIX.State.execDiv then sets ov.
Example P7.3.34. A holds , X holds and the divisor is . Then . A becomes (sign minus times plus) and X becomes (the sign of A). If the divisor is then , so OV goes on and A, X stay.
Definition P7.3.35 (address transfer). The instructions INC and DEC add to the register or subtract from it, as ADD and SUB do for A and X. The instructions ENT and ENN put or in the register. For an index register the result must have a magnitude below , or the model stops with the fault register. These instructions use as a number and do not read the memory.
Lean: TAOCP.MIX.State.execEnter, TAOCP.MIX.State.addReg and TAOCP.MIX.State.enterReg.
Example P7.3.36. ENT1 is allowed and ENT1 is a fault for , since . INC1 on a register that holds is also a fault. INCA on A sets OV and leaves .
Theorem P7.3.37 (the time of an instruction). The time of an instruction of operation code and modification is given by this table, in units .
| instruction | time | |
|---|---|---|
| NOP | ||
| ADD, SUB | ||
| MUL | ||
| DIV | ||
| NUM, CHAR () | ||
| HLT () | ||
| shifts | ||
| MOVE | ||
| to | loads, negative loads, stores, STJ, STZ | |
| to | JBUS, IOC, IN, OUT, JRED | plus the wait of the device |
| the jumps on OV and CI | ||
| to | jumps on a register, address transfers | |
| to | comparisons |
Proof. The table is the definition of the time of the machine. The definition of the model has the same cases in the same order.
Lean: TAOCP.MIX.Instr.time, with TAOCP.MIX.Instr.time_load (codes to ), TAOCP.MIX.Instr.time_store (codes to ), TAOCP.MIX.Instr.time_cmp (codes to ), TAOCP.MIX.Instr.time_jump (codes to ) and TAOCP.MIX.Instr.time_move. The model does not run the input and output instructions, so it does not model the wait, and the codes to have the time in the model.
Example P7.3.38. The three instructions LDA , MUL , STX take units. The two instructions LDA and ADD take . The pair ENTA and DIV take .
Theorem P7.3.39 (time of a run). The time of a run of steps is the sum of the times of the executed instructions, and the clock of the model adds the time of an instruction at its fetch. A step that faults has still added its time.
Proof. This is the theorem on the time of a run of chapter 2, with the cost of a step taken from the table. The model adds ins.time to the clock before it runs the instruction, so a faulting instruction has been charged.
Lean: TAOCP.MIX.State.step adds the time at the head, and TAOCP.MIX.State.run_add splits a run in two runs.
Examples
Decode a word. Take and the word . The address is , , , so the field is , and , which is STA. The instruction is STA . With rI3 it stores the magnitude of A into the cell and keeps the sign of that cell. Its time is .
A line of code. The block LD1 , LDA , MUL , STA with the cell holding and the cells and holding and puts in I1. LDA loads the cell (), MUL gives in X and in A, STA stores A in the cell . The time is . A reader who wants the product in the cell must store X, not A.
Exercises
- [10] Write the word of LD3 for .
- [10] Decode the word .
- [15] With rI2 and , what does the model do for LDA , for ENTA and for LDA ?
- [15] Give the time of LDA , MUL , STA , STX .
- [M20] Show that determines the pair for , and count the valid pairs and their largest code.
- [M20] After a DIV that does not overflow, show that the dividend equals and that .
- [15] A holds and the cell holds . Run MUL and give A and X.
- [20] A holds , X holds and the cell holds . Run DIV and give A and X.
- [M20] Show that LDA followed by STA , both with the default field, leaves the cell unchanged.
- [20] Which of ENT1 , ENT1 , INC1 with rI1 is a fault, and why?
Lean
The format is in TAOCP/MIX/Instr.lean: TAOCP.MIX.Instr, TAOCP.MIX.Word.decode, TAOCP.MIX.Instr.encode, TAOCP.MIX.Word.encode_decode, TAOCP.MIX.Instr.decode_encode. The times are in TAOCP/MIX/Timing.lean. The semantics of each group is in TAOCP/MIX/Semantics.lean: execLoad, execStore, execAdd, execMul, execDiv, execEnter, execCmp. The theorems on MUL and DIV of this section are not stated in the project. The definitions that they describe, TAOCP.MIX.Word.mul and TAOCP.MIX.Word.div, are in TAOCP/MIX/Ops.lean.
Notes and history
The format with an address, an index, a modification and a code is the format of many machines of the period, and the field specification is the feature of MIX that has no use on a binary machine. The table of times lists the units of the book. A later section of TAOCP uses it for all analyses. The model of the project leaves out the wait of the input and output.
Jumps, shifts, MOVE and conversion
Statement
The groups of the last section change registers and memory and leave the counter to run on by one. This section covers the instructions that change , the instructions that move the bytes of the registers, the block copy, and the two instructions that convert between a number and its decimal characters.
Definition P7.3.40 (jump). A jump with the target replaces by , when its condition holds. When the jump is taken, the register J gets the address of the instruction that would have run next, that is the old , with the sign plus. The instruction JSJ is the exception: it jumps and leaves J as it is. The conditions are these.
| instruction | the jump is taken when | ||
|---|---|---|---|
| JMP | always | ||
| JSJ | always, J unchanged | ||
| JOV | OV is on, and then OV goes off | ||
| JNOV | OV is off, and if OV is on it goes off | ||
| JL, JE, JG | CI is less, equal, greater | ||
| JGE, JNE, JLE | CI is not less, not equal, not greater | ||
| JAN, JAZ, JAP, JANN, JANZ, JANP | to | the value of A is , , , , , | |
| the same for I1 to I6 and X | to | to | the same test on that register |
The target must be a cell from to , or the model stops with the fault address.
Lean: TAOCP.MIX.State.jump (with the flag that says whether J is saved), TAOCP.MIX.State.jumpIf, TAOCP.MIX.State.execJump for and TAOCP.MIX.State.execRegJump for the registers.
Example P7.3.41. The instruction JMP in the cell sets and J , since . The instruction JSJ in the same cell sets and leaves J as it was.
Example P7.3.42. A holds and I1 holds . JAZ jumps and JAN does not, because the value of A is . JANN jumps, since . J1N jumps, since the value of I1 is , and J1NN does not.
Theorem P7.3.43 (the toggle after JOV and JNOV). After JOV or JNOV the toggle OV is off, whether or not the jump was taken.
Proof. Case JOV. If OV was on, the instruction switches it off and jumps. If OV was off, it stays off and no jump occurs. Case JNOV. If OV was on, the instruction switches it off and does not jump. If OV was off, it stays off and the jump occurs. In the four cases OV is off at the end.
Lean: the cases are in TAOCP.MIX.State.execJump. The project states no theorem on this.
Example P7.3.44. A program adds two words with ADD and then runs JOV Fix. In a run with overflow, OV is on, the jump goes to Fix and OV is off there. A second JOV later does not jump, since the toggle is off now.
Theorem P7.3.45 (complementary jumps). In each of the pairs (JL, JGE), (JE, JNE), (JG, JLE), and (JAN, JANN), (JAZ, JANZ), (JAP, JANP) for each register, exactly one of the two instructions jumps. So if the instruction JGE T is followed by JL T on the same CI, the second never jumps after a first that did not.
Proof. The comparison indicator holds exactly one of less, equal, greater. The first of a pair jumps for one case or two cases, and the second for the other cases. The same holds for the sign of a value: it is , or .
Example P7.3.46. CI is equal. JL does not jump and JGE jumps. JE jumps and JNE does not. JG does not jump and JLE jumps. Exactly one of each pair.
Definition P7.3.47 (shifts). The instructions with move the bytes of A, or of A and X as a row of ten bytes, by places. The signs do not move. must not be negative, or the model stops with the fault shift.
| instruction | the bytes move | |
|---|---|---|
| SLA | A left places, zeros enter on the right | |
| SRA | A right places, zeros enter on the left | |
| SLAX | the row A X left places | |
| SRAX | the row A X right places | |
| SLC | the row A X turns left places | |
| SRC | the row A X turns right places |
A byte that leaves the row is lost, except in the rotations.
Lean: TAOCP.MIX.Word.shiftLeftA, TAOCP.MIX.Word.shiftRightA, TAOCP.MIX.Word.shiftLeftAX, TAOCP.MIX.Word.shiftRightAX, TAOCP.MIX.Word.rotateLeftAX, TAOCP.MIX.Word.rotateRightAX and TAOCP.MIX.State.execShift.
Theorem P7.3.48 (shifts are arithmetic). Let be the magnitude of A and the magnitude of the row A X. SLA by gives the magnitude . SRA by gives . SLAX by gives . SRAX by gives . SLC by with gives .
Proof. In radix a left shift by places is the product with , and the places that pass the left end are the digits of the multiple of , or of , that the remainder removes. A right shift by places drops the low digits, which is the floor of the quotient by . For the turn, write as the row . The number is the row with zeros, and is the row as a number, which is below . The sum is the row .
Example P7.3.49. Take and the row A X . SLAX gives . SRAX gives . SLC gives . SLA on A alone gives and SRA on A alone gives .
Theorem P7.3.50 (rotations). SLC by is the identity. SRC by equals SLC by . If SLC by is followed by SRC by , the row is the same as before.
Proof. The first claim is the case of the formula, since and . The second claim is the definition of the turn to the right. For the third claim, SLC by moves the first bytes to the end, and SRC by moves the last bytes back to the front.
Example P7.3.51. The row under SRC gives , the same as SLC . The time of each shift is and does not depend on .
Definition P7.3.52 (MOVE). The instruction MOVE copies words, one after the other, from the cells to the cells , where is the value of I1 when the instruction starts. After the copy I1 holds . The byte is a number from to , not a field specification. The time is units.
Lean: TAOCP.MIX.State.moveWords copies the words one at a time, and TAOCP.MIX.State.execMove adds to I1. The time is TAOCP.MIX.Instr.time_move.
Theorem P7.3.53 (MOVE on blocks that do not overlap or that overlap on the safe side). Let . If or , then after MOVE the cell holds the old contents of the cell for .
Proof. If the blocks are disjoint, and no write changes a cell that a later step reads. If nothing changes. Let . At step the cell is read. It could have been written by a step with , that is . This step has not run yet, so the cell holds its old contents.
Theorem P7.3.54 (MOVE with an overlap, the smear). If , then after MOVE the cell holds the old contents of the cell for .
Proof. By strong induction on . For the cell has not been written, since the steps before write the cells , which start at . So step copies the old contents of , and . For the cell was written at step , and the step reads it after that. By the hypothesis that holds for , this cell holds the old contents of . The step copies it.
Example P7.3.55. The cells to hold . With I1 the instruction MOVE has , . Every new cell holds the old contents of the cell since . The cells become and I1 becomes . The time is . With I1 the same instruction has and gives the cells to as , a correct copy.
Definition P7.3.56 (NUM and CHAR). The instruction NUM (, ) reads the ten bytes of A X as ten decimal digits, each byte counting as its value modulo , and it puts the number of these digits in A, with the sign of A. If the number is at least , OV goes on and A holds the number modulo . The instruction CHAR () takes the magnitude of A as a number with ten decimal digits and puts the character code of each digit in a byte: the first five in A and the last five in X. The signs stay. Each takes .
Lean: TAOCP.MIX.Word.num, TAOCP.MIX.Word.numeric (the number that the bytes spell), TAOCP.MIX.Word.char and TAOCP.MIX.charOf (the character code of a digit).
Example P7.3.57. A holds . CHAR writes the ten digits , so A and X . NUM on these gives the digits back, since and , and so on, and the number in A.
Theorem P7.3.58 (NUM undoes CHAR). For each value of A, NUM run after CHAR gives A the same magnitude and the same sign, and OV is not set.
Proof. The magnitude of A is below , so CHAR writes its ten decimal digits with leading zeros. The code of is , and . NUM reads these digits and forms . Since , there is no overflow. The sign of A stays through both.
Definition P7.3.59 (input and output). The instructions IN, OUT, IOC, JBUS and JRED ( to ) work with the devices numbered from to : tapes, disks, a card reader, a card punch, a printer and others. Each device has a block of fixed size, for example words for a tape and words for the printer. The time of an instruction is , and the device works in parallel with the program. A later instruction on the same device waits until it is free. The model of the project does not run these instructions and stops with the fault io.
Lean: TAOCP.MIX.Fault.io, the reason of the stop in TAOCP.MIX.State.exec for to .
Example P7.3.60. A program that writes a line of the printer takes the time of its own instructions only, if it starts the next instruction at once. If a second OUT to the printer comes before the first is finished, it waits. The wait is not in the time table.
Examples
A count loop. The two instructions ENT1 and then a loop of DEC1 and J1P , where is the cell of the DEC1, run ENT1 once and the pair ten times. The time is units. The last J1P does not jump, since I1 is . The register J holds the address after the last J1P that jumped, which is the address of the cell after J1P.
Shifts as multiplication. A holds . SLA gives . SLA gives , so the value is lost with no sign of it in OV. Shifts do not set OV. A program that wants to know must test.
MOVE as a fill. The cell holds a word and the cells to are free. Put I1 and run MOVE . Here , so every new cell holds . The time is for a fill of cells, two units for each cell and one for the call.
Exercises
- [10] The instruction JMP in the cell jumps to . What do and J hold afterwards for ?
- [10] A holds . Which of JAN, JAZ, JAP, JANN, JANZ and JANP jump?
- [15] OV is on. The instruction JNOV is run twice in a row at the same cell. Describe the two runs.
- [15] A holds . Give A after SLA and after SRA , each from the original A.
- [15] The row A X is with the bytes to in this order. Give the row after SRC .
- [M20] Show that SLA by followed by SRA by gives the magnitude for .
- [M20] Show that SRC by equals SLC by for not a multiple of , and that both are the identity when is a multiple of .
- [15] The cells to hold the words and I1 . Run MOVE . Give the six cells, I1 and the time.
- [20] Write a MIX fragment that fills the cells to with the word in the cell , and give its time.
- [15] CHAR is run with A . Give A and X, and the time.
- [M20] Explain why NUM run after CHAR gives back the magnitude of A. Give an example of A and X for which CHAR run after NUM does not give back the bytes of A and X.
- [M22] A loop has the instruction pair DEC1 , J1P and the loop body takes units. Show that a run with I1 at the start of the loop takes units.
Lean
The jumps, shifts and MOVE are in TAOCP/MIX/Semantics.lean: State.jump, State.execJump, State.execRegJump, State.execShift, State.moveWords, State.execMove, State.execSpecial. The pure functions on words are in TAOCP/MIX/Ops.lean. The project checks a few of these by decide in TAOCP/MIX/Examples.lean, for example the shifts and MOVE on small data. It states no theorem on the overlap of MOVE, nor on NUM and CHAR as inverse functions. Input and output are not run.
Notes and history
The jump that saves the return address in a register, and the shifts and rotations on a row of bytes, were common on the machines of the time. MOVE is a block copy in one instruction, which is rare on the machines of today. The smear of an overlapping MOVE is not an error of the machine. It is the result of copying word by word, and a program can use it to fill a block, as the example shows.
Three programs
Statement
This section gives three small programs of the writer, each as a list of cells, with its registers, its trace and its time. The proofs use the method of chapter 2: an invariant at the head of the loop, a variant that falls, and a count of the executions of each cell. Each program was run on a simulator of the model, and the numbers of the traces are the numbers of that run. All numbers are for .
A listing has one row for each cell: the address, the instruction, and the time of the instruction. The code is at and the data are at and . The instruction names are the names of the table of the last two sections, and chapter 5 gives the assembly language that writes them as text.
Program P7.3.61 (S, the sum of a table). The cell holds a count with . The cells to hold the words . The program S puts the sum in A and in the cell .
| cell | instruction | time | note |
|---|---|---|---|
| ENTA | A | ||
| LD1 | I1 | ||
| J1Z | if go to the end | ||
| ADD | A A the word , where I1 | ||
| DEC1 | I1 I1 | ||
| J1P | if I1 go to | ||
| STA | the sum goes to the cell | ||
| HLT |
Registers: A holds the partial sum, I1 holds the index of the next word. X and I2 to I6 are not used. J is set by each jump that is taken, and the program does not read it. The run enters with I1 , and the first word read is the last word of the table, since the sum is added from the top down.
Theorem P7.3.62 (correctness and time of S). Suppose and . After S halts, A and the cell hold , I1 holds , OV is off if it was off, and the time is units.
Proof. The invariant at the cell is: I1 with , and A holds (the empty sum is ). The cell is first reached from , when , with and A , an empty sum. The invariant holds. Take a pass from with the invariant true. ADD adds , since the address is and the cell holds . A now holds . The overflow flag is false: the absolute value of this sum is at most , so by the rule of overflow in section 1 OV is not set and A holds the exact value. DEC1 makes I1 . If the jump goes to , with I1 and A , which is the invariant for . If control falls to with A . For the jump of goes to with A , the empty sum. In all cases STA puts A in and HLT stops. The variant is I1: it falls by one in each pass and the loop leaves when it reaches , so the loop ends after passes. The cells , and run times each, and the other cells run once. The time is . For the cells to do not run, and the time agrees.
Lean: the project has no theorem on this program. Its example program TAOCP.MIX.Examples.sumProgram adds three words in a similar loop with the time , and TAOCP.MIX.MaxExample.maxSpec is a proved statement of the same shape (chapter 12).
Example P7.3.63 (a trace of S). Take and the table . The table is read from the top: , then , then , then . The state after each step is:
| step | cell | instruction | A | I1 | clock |
|---|---|---|---|---|---|
| 1 | ENTA | ||||
| 2 | LD1 | ||||
| 3 | J1Z | ||||
| 4 | ADD | ||||
| 5 | DEC1 | ||||
| 6 | J1P | ||||
| 7 | ADD | ||||
| 8 | DEC1 | ||||
| 9 | J1P | ||||
| 10 | ADD | ||||
| 11 | DEC1 | ||||
| 12 | J1P | ||||
| 13 | ADD | ||||
| 14 | DEC1 | ||||
| 15 | J1P | ||||
| 16 | STA | ||||
| 17 | HLT |
The sum is and the clock is . At the cell the invariant reads: I1 , A ; I1 , A ; I1 , A ; I1 , A . In each case A is the sum for I1.
Example P7.3.64 (a table that overflows). The table has , and . The hypothesis fails. The program reads , then , and the second ADD gives the true sum . A becomes , OV goes on, and the time is as before. The theorem on the time does not need the hypothesis on the sum, but the theorem on the value does.
Program P7.3.65 (D, the count of decimal digits). The cell holds a word with any sign, and the cell holds . The program D puts in the cell the number of decimal digits of , with for .
| cell | instruction | time | note |
|---|---|---|---|
| ENTA | A | ||
| LDX | X | ||
| ENT2 | I2 , the count | ||
| DIV | A X , X X | ||
| INC2 | I2 I2 | ||
| SRAX | the quotient moves from A to X, A | ||
| JXP | if X go to | ||
| ST2 | the count goes to the cell | ||
| HLT |
Registers: the pair A X is the ten byte number that DIV divides. At the head of the loop A is and X holds the part of that is not yet divided. I2 is the count. The instruction DIV divides a number of ten bytes by . The number is with , so it is , and the sign of X is not used. The instruction SRAX moves five bytes to the right in the row A X, so that the five bytes of A pass to X, and the five bytes of X, which hold the remainder, leave the row.
Theorem P7.3.66 (correctness and time of D). Let be a word and let be the number of decimal digits of for and for . After D halts, the cell holds and the time is units.
Proof. Put . The invariant at the cell after passes is: A , I2 , and X has the magnitude with the sign plus, for , and X for . At the first arrival and the magnitude of X is . A pass does this. The dividend is . The divisor is , so there is no overflow. DIV gives A with the sign plus, and X the remainder. INC2 gives I2 . SRAX moves the quotient to X and puts in A, with the signs plus. The invariant holds for . JXP jumps when X , that is when , and then the loop goes on.
The loop ends after the first with , that is . For the least such is the number of digits of , because has digits exactly when . For the first pass gives X , so . The variant is the magnitude of X: it falls strictly in each pass, since for , and the loop ends at . At the end ST2 stores .
Frequencies: the cells to run times each and the other cells once. The time is .
Lean: the project has no theorem on this program. The model has TAOCP.MIX.Word.div, TAOCP.MIX.Word.shiftRightAX and TAOCP.MIX.Word.pair that the proof uses.
Example P7.3.67 (a trace of D). Take , so and the time is .
| step | cell | instruction | A | X | I2 | clock |
|---|---|---|---|---|---|---|
| 1 | ENTA | |||||
| 2 | LDX | |||||
| 3 | ENT2 | |||||
| 4 | DIV | |||||
| 5 | INC2 | |||||
| 6 | SRAX | |||||
| 7 | JXP | |||||
| 8 | DIV | |||||
| 9 | INC2 | |||||
| 10 | SRAX | |||||
| 11 | JXP | |||||
| 12 | DIV | |||||
| 13 | INC2 | |||||
| 14 | SRAX | |||||
| 15 | JXP | |||||
| 16 | ST2 | |||||
| 17 | HLT |
The cell holds , the number of digits of . The pass of the steps 4 to 7 leaves X , the steps 8 to 11 leave , and the steps 12 to 15 leave .
Example P7.3.68 (other inputs). For the loop runs once and , time . For the time is . For and the time is . For the time is . For the result and the time are the same as for . The largest word has digits and takes units.
Program P7.3.69 (C, a copy of a block). The cells to hold seven words. The program C copies them to the cells to by one MOVE.
| cell | instruction | time | note |
|---|---|---|---|
| ENT1 | I1 | ||
| MOVE | copy words to the cells from I1 | ||
| HLT |
Theorem P7.3.70 (correctness and time of C). After C halts the cells hold the old contents of the cells for , I1 and the time is . For a block of words the same program with MOVE takes units.
Proof. The blocks and are disjoint, so by the theorem of MOVE in the last section each new cell holds the old contents of its source, and the sources are not changed. I1 grows by to . The time is . For words it is .
Program P7.3.71 (L, the same copy by a loop). The program L copies words with a loop that runs from the top of the block to the bottom.
| cell | instruction | time | note |
|---|---|---|---|
| ENT1 | I1 | ||
| LDA | A the word at I1 | ||
| STA | store it at I1 | ||
| DEC1 | |||
| J1P | |||
| HLT |
Theorem P7.3.72 (time of L, and the comparison). The program L copies the cells to and takes units. The program C takes units. So , and C is faster for each , and it uses three cells of code against six.
Proof. At I1 the loop copies the cell to the cell . The values cover the cells of the block. The cells to run times, with the time each, and ENT1 and HLT run once. The time is .
Example P7.3.73 (the times for ). C takes units and L takes . The difference is . For , the largest block of one MOVE with , C takes and L takes .
Example P7.3.74 (a trace of C). The state after each step is:
| step | cell | instruction | I1 | clock |
|---|---|---|---|---|
| 1 | ENT1 | |||
| 2 | MOVE | |||
| 3 | HLT |
Examples
A program that loops on a count. The loop of S has the same shape as the loop of the sum program of the project, TAOCP.MIX.Examples.sumProgram: load a count, run a body, decrement, test with J1P. In the project the body adds one of three cells and takes per round, and the whole run takes . The formula at gives . The project program has ENT1 in place of LD1 and has no J1Z, so it is shorter. The reader should compare the two listings cell by cell.
The cost of overflow. In S an overflow costs no time. The flag is only a record. A program that must detect overflow can run JOV after the loop. That adds the time if no overflow occurred.
Exercises
- [10] Give the time of S for a table of words and for a table of words.
- [15] Run S by hand for and the table . Give the state after each step and the time.
- [M20] Show that if the table of S has words and , then OV is on at the end and A . What is A for the table ?
- [15] Run D for . How many passes, and what time?
- [20] Change the cell of D to . What does D compute now, and what is its time for ?
- [M22] Show that D never faults and never sets OV for any with and the cell equal to .
- [15] Give the times of C and of L for .
- [M25] Let the source block start at and the destination block at , and change the address of the STA of L to . Find all the values of for which L copies a block of words correctly, and compare with the values of for which MOVE is correct.
- [M22] Write a version of S that reads the table from to , with I1 running from to and the address . The number is known when the program is written. Give its time and say what goes wrong for .
- [25] In D replace SRAX by a store of A in the cell , a load of X from this cell and ENTA . Give the new time per pass and the new total.
Lean
The loop sum is TAOCP.MIX.Examples.sumProgram, with a decide check of its result and its time . The theorem that proves a program of this kind, with the invariant and the time as a formula, is TAOCP.MIX.MaxExample.maxSpec of chapter 12. The project has no theorem for the programs S, D, C and L. A reader who wants to run them in Lean can write the cells with TAOCP.MIX.Instr.mk' and TAOCP.MIX.Word.ofInt, put them in a TAOCP.MIX.Program and run it with TAOCP.MIX.Program.run.
Notes and history
The sum of a table, the count of the digits of a number and the block copy are standard first programs on every machine. The count of the digits by repeated division by costs most in the DIV, which takes out of the of a pass. A program that must be fast for numbers of ten digits would use a table of powers of ten instead. The comparison of MOVE with a loop shows a trade that the designer of the machine made: MOVE uses for each word and a loop uses , since the loop pays for the count and the test.
The model machine of chapter 2 had eight instructions and a decimal word. MIX is a real design of the same kind, with 64 operation codes, six index registers and a word of five bytes and a sign. TAOCP uses it for every program of Volumes 1 to 3. This chapter takes MIX apart as an example of a machine, and it asks of each part the question of chapter 2: what is the state, what is the step, and what does it cost.
Section 1 lists the registers, the word and the memory, and it proves the rules of range and overflow for the registers. Section 2 gives the format of an instruction, the effective address and the field specification, and it states the time of the load, store and arithmetic groups. Section 3 covers the jumps, the shifts, MOVE, the conversion instructions and the devices. Section 4 gives three programs of the writer, a sum of a table, a count of the digits of a number and a copy of a block, each with its registers, its trace and its time.
The chapter uses P1, P7.1 on words and fields, and P7.2 on the state and the step. All numbers use the byte size , the smallest that the model allows. A machine with another byte size in the allowed range changes the numbers of the range and of the overflow, and it leaves the rules unchanged. The Lean parts name the declarations of TAOCP/MIX in the project: Word, Half, Instr, State, Program, State.step, State.run, Instr.time and the field functions. The model does not run floating point and does not run the input and output instructions: a program that uses them stops with a fault in the model, and the chapter says so where it matters. The base for chapter 5 on assembly language and for chapter 12 on proofs of MIX programs is here.
The source of this page. Something is wrong? Report it.