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

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
  1. Registers and words
  2. Instruction format and timing
  3. Jumps, shifts, MOVE and conversion
  4. Three programs

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 qq of chapter 2 for MIX.

Definition P7.3.1 (the state of MIX). A MIX machine with byte size bb has these parts.

part name holds range of the magnitude
accumulator A a word: a sign and five bytes 00 to b5−1b^5 - 1
extension X a word 00 to b5−1b^5 - 1
index registers I1 to I6 a half word: a sign and two bytes 00 to b2−1b^2 - 1
jump register J a half word with the sign always plus 00 to b2−1b^2 - 1
memory cells 00 to 39993999 a word each 00 to b5−1b^5 - 1
overflow toggle OV on or off
comparison indicator CI less, equal or greater
counter pp the address of the next instruction 00 to 39993999
clock tt the time used, in units uu
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 b=64b = 64 let A hold − ∣ 5 ∣ 0 ∣ 12 ∣ 63 ∣ 7-\,|\,5\,|\,0\,|\,12\,|\,63\,|\,7. The magnitude is 5⋅644+0⋅643+12⋅642+63⋅64+7=839392715 \cdot 64^4 + 0 \cdot 64^3 + 12 \cdot 64^2 + 63 \cdot 64 + 7 = 83939271, so the value of A is −83939271-83939271.

Example P7.3.3. The register I3 holds + ∣ 1 ∣ 40+\,|\,1\,|\,40. Its value is 1⋅64+40=1041 \cdot 64 + 40 = 104. The register J holds + ∣ 47 ∣ 3+\,|\,47\,|\,3 after a jump that comes from the cell 30103010, since 47⋅64+3=301147 \cdot 64 + 3 = 3011.

Definition P7.3.4 (the byte size). The byte size bb of MIX is a fixed number with 64≤b≤10064 \le b \le 100. A byte is a number from 00 to b−1b - 1.

Theorem P7.3.5 (why b≥64b \ge 64). A half word can hold every address of the memory if and only if b2≥4000b^2 \ge 4000. This holds for b≥64b \ge 64, and it fails for b=63b = 63.

Proof. The addresses are 0,…,39990, \dots, 3999, so they fit in the magnitude of a half word exactly when 3999≤b2−13999 \le b^2 - 1, that is b2≥4000b^2 \ge 4000. For b=64b = 64 we have b2=4096≥4000b^2 = 4096 \ge 4000, and b2b^2 grows with bb. For b=63b = 63 we have 632=3969<400063^2 = 3969 < 4000. □\square

Lean: TAOCP.MIX.ByteSize is the class of the byte sizes bb with 64≤b≤10064 \le b \le 100.

Example P7.3.6. For b=64b = 64 the last address 39993999 has the bytes 62,3162, 31, since 62⋅64+31=399962 \cdot 64 + 31 = 3999. For b=100b = 100 the same address is the bytes 39,9939, 99.

Theorem P7.3.7 (range of the registers). The magnitude of A and of X is less than b5b^5, so the value is between −(b5−1)-(b^5 - 1) and b5−1b^5 - 1. The magnitude of an index register and of J is less than b2b^2. For b=64b = 64 these bounds are 10737418231073741823 and 40954095.

Proof. A word has five bytes, each less than bb, so by the bound of a row in P7.1 the magnitude is at most b5−1b^5 - 1. The same holds for two bytes and b2−1b^2 - 1. For b=64b = 64: 645=230=107374182464^5 = 2^{30} = 1073741824 and 642=409664^2 = 4096. □\square

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 10737418231073741823. Add 11 to it, and the true sum 10737418241073741824 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 ww be a word with value xx and let vv be an integer. Put r=x+vr = x + v. The sum with a flag of ww and vv is the word with the magnitude ∣r∣ mod b5|r| \bmod b^5 and the flag that is true when ∣r∣≥b5|r| \ge b^5. The sign of the result is ++ for r>0r > 0, −- for r<0r < 0, and the sign of ww for r=0r = 0.

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 vv, 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 x+vx + v.

Proof. The first claim is the definition of the instructions in the model. The last claim: when ∣r∣<b5|r| < b^5 the magnitude ∣r∣ mod b5|r| \bmod b^5 is ∣r∣|r| and the sign is the sign of rr, so the value is rr, except for r=0r = 0, where both signs give the value 00. □\square

Lean: TAOCP.MIX.Word.value_ofInt is the last claim: the word of an integer rr with ∣r∣<b5|r| < b^5 has the value rr. 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 + ∣ 63 ∣ 63 ∣ 63 ∣ 63 ∣ 63+\,|\,63\,|\,63\,|\,63\,|\,63\,|\,63 and add the word + ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 2+\,|\,0\,|\,0\,|\,0\,|\,0\,|\,2. The sum is 10737418251073741825, the magnitude modulo b5b^5 is 11, and the flag is true. A becomes + ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 1+\,|\,0\,|\,0\,|\,0\,|\,0\,|\,1 and OV is on. The machine lost the high part: the new value is the true sum minus b5b^5.

Example P7.3.12. Let A hold − ∣ 63 ∣ 63 ∣ 63 ∣ 63 ∣ 60-\,|\,63\,|\,63\,|\,63\,|\,63\,|\,60, with the value −(b5−4)=−1073741820-(b^5 - 4) = -1073741820, and subtract the word with the value +10+10. The true result is −1073741830-1073741830, the magnitude modulo b5b^5 is 66 and the flag is true. A becomes − ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 6-\,|\,0\,|\,0\,|\,0\,|\,0\,|\,6 with OV on.

Theorem P7.3.13 (no overflow for opposite signs). If xx and vv are not both positive and not both negative, that is xv≤0xv \le 0, then the sum of xx and vv does not overflow.

Proof. If x≥0≥vx \ge 0 \ge v then v≤x+v≤xv \le x + v \le x, so ∣x+v∣≤max⁡(∣x∣,∣v∣)<b5|x + v| \le \max(|x|, |v|) < b^5 because both are values of the words (an integer vv that comes from a word of the memory has ∣v∣<b5|v| < b^5). The case x≤0≤vx \le 0 \le v is the same. □\square

Example P7.3.14. A holds +1073741823+1073741823 and the cell holds −1073741823-1073741823. The sum is 00, no overflow. Since r=0r = 0, the sign of the result is the sign of A: the result is +0+0. If A holds −5-5 and the cell holds +5+5, the result is −0-0. Both have the value 00.

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 55 and the cell holds 99, then CMPA gives less. If A holds −0-0 and the cell holds +0+0, 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 +0+0, OV off, CI equal, the clock 00, 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 b=64b = 64 and consider the machine with A =+ ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 9= +\,|\,0\,|\,0\,|\,0\,|\,0\,|\,9, X =− ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5= -\,|\,1\,|\,2\,|\,3\,|\,4\,|\,5, I1 =+ ∣ 0 ∣ 10= +\,|\,0\,|\,10, the other index registers +0+0, J =+ ∣ 0 ∣ 0= +\,|\,0\,|\,0, OV off, CI equal, p=3000p = 3000 and t=0t = 0. Its values are 99 for A, −(644+2⋅643+3⋅642+4⋅64+5)=−17314053-(64^4 + 2 \cdot 64^3 + 3 \cdot 64^2 + 4 \cdot 64 + 5) = -17314053 for X and 1010 for I1.

Two additions in a row. From the state above, add the word with the value 10737418201073741820 twice. The first sum is 9+1073741820=1073741829≥b59 + 1073741820 = 1073741829 \ge b^5, so A holds the magnitude 55 and OV is on. The second sum is 5+1073741820=10737418255 + 1073741820 = 1073741825, so A holds 11 and OV stays on. After the two additions the value of A is 11 and OV is on, while the true sum 21474836492147483649 is 2b5+12 b^5 + 1. The machine keeps the sum modulo b5b^5 and the toggle records that something was lost.

The sign of zero. Start with A =−3= -3. Run SUB with a cell of value −3-3. The result is 00 and A has the sign minus: −0-0. A jump instruction that tests the sign of A, as JAN does, treats −0-0 as zero, because the test uses the value: JAN does not jump, JAZ jumps. The next section lists the jumps.

Exercises

  1. [10] With b=64b = 64 give the value of the register A that holds + ∣ 2 ∣ 0 ∣ 0 ∣ 0 ∣ 1+\,|\,2\,|\,0\,|\,0\,|\,0\,|\,1.
  2. [10] With b=64b = 64 what are the largest value of A and the largest value of an index register?
  3. [15] With b=64b = 64 let A hold +1073741823+1073741823 and run ADD with a cell of value +2+2. Give the value of A and the state of OV.
  4. [15] A holds −5-5 and OV is on. Run ADD with a cell of value +5+5. Give A, with its sign, and OV.
  5. [M22] Show that if two integers xx and vv are values of words and x+vx + v overflows, then xx and vv have the same sign, and the machine result has the value x+v−b5x + v - b^5 when both are positive.
  6. [M25] How many different words are there for the byte size bb? How many different values do they have? Give both numbers for b=64b = 64.
  7. [M20] Show that a machine with the byte size b=63b = 63 could not address all the cells, and find the least bb with b2≥4000b^2 \ge 4000.
  8. [15] The instruction JMP in the cell 30103010 is run. What does J hold, as a half word, with b=64b = 64?

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 bb 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 pp, decodes it by these rules and runs it.

Definition P7.3.18 (the instruction word). The word + ∣ a1 ∣ a2 ∣ I ∣ F ∣ C+\,|\,a_1\,|\,a_2\,|\,I\,|\,F\,|\,C or − ∣ a1 ∣ a2 ∣ I ∣ F ∣ C-\,|\,a_1\,|\,a_2\,|\,I\,|\,F\,|\,C is the instruction with the address ±AA\pm AA where AA=a1b+a2AA = a_1 b + a_2, the index specification II, the modification FF and the operation code CC. In the field notation of P7.1, ±AA\pm AA is the field (0:2)(0:2), II is (3:3)(3:3), FF is (4:4)(4:4) and CC is (5:5)(5:5).

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 b=64b = 64 the instruction that loads A from the cell 2000+rI22000 + \text{rI2} with the field (0:3)(0:3) has AA=2000=31⋅64+16AA = 2000 = 31 \cdot 64 + 16, I=2I = 2, F=8⋅0+3=3F = 8 \cdot 0 + 3 = 3 and C=8C = 8. Its word is + ∣ 31 ∣ 16 ∣ 2 ∣ 3 ∣ 8+\,|\,31\,|\,16\,|\,2\,|\,3\,|\,8.

Example P7.3.20. The instruction that stores X into the field (2:2)(2:2) of the cell −100+rI4-100 + \text{rI4} has AA=100=1⋅64+36AA = 100 = 1 \cdot 64 + 36, the sign −-, I=4I = 4, F=8⋅2+2=18F = 8 \cdot 2 + 2 = 18 and C=31C = 31. Its word is − ∣ 1 ∣ 36 ∣ 4 ∣ 18 ∣ 31-\,|\,1\,|\,36\,|\,4\,|\,18\,|\,31.

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 AA<b2AA < b^2, I<bI < b, F<bF < b and C<bC < b, is the decoding of its encoding.

Proof. The encoding writes AAAA as the two bytes ⌊AA/b⌋\lfloor AA / b \rfloor and AA mod bAA \bmod b, and the other parts as bytes. The decoding forms a1b+a2a_1 b + a_2 again, and the identity b⌊AA/b⌋+(AA mod b)=AAb \lfloor AA / b \rfloor + (AA \bmod b) = AA of the division gives back AAAA. The bytes II, FF and CC are copied. For a word, the decoding gives a1b+a2a_1 b + a_2, and dividing by bb gives a1a_1 and the remainder a2a_2, because a2<ba_2 < b. The sign is copied in both directions. □\square

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 i=Ii = I. The effective address of the instruction is M=±AA+rIiM = \pm AA + \text{rI}_i when 1≤i≤61 \le i \le 6, and M=±AAM = \pm AA when i=0i = 0. The sum must have a magnitude below b2b^2, and i≤6i \le 6. The model stops with the fault index if one of the two fails. A memory instruction needs 0≤M≤39990 \le M \le 3999, 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 + ∣ 15 ∣ 40 ∣ 3 ∣ 5 ∣ 8+\,|\,15\,|\,40\,|\,3\,|\,5\,|\,8 has AA=15⋅64+40=1000AA = 15 \cdot 64 + 40 = 1000 and I=3I = 3. With rI3 =+104= +104 the address is M=1104M = 1104. With rI3 =−104= -104 the address is M=896M = 896. With I=0I = 0 the address is 10001000 for any rI3.

Example P7.3.24. With rI2 =−30= -30 the instruction LDA 20,220,2 has M=−10M = -10. The cell −10-10 does not exist, so the model stops with the fault address. In the same machine ENTA 20,220,2 has M=−10M = -10 and does no memory access, so it sets A to −10-10 and does not fault.

Definition P7.3.25 (the register code). The operation codes 88 to 1515 are the loads LDA, LD1 to LD6, LDX in this order. The codes 1616 to 2323 are the negative loads, 2424 to 3131 are the stores of the same registers, 4040 to 4747 are the jumps on a register, 4848 to 5555 are the address transfers INC, DEC, ENT and ENN, and 5656 to 6363 are the comparisons. In each of these groups the register is determined by C mod 8C \bmod 8: the value 00 is A, the values 11 to 66 are I1 to I6 and the value 77 is X.

Lean: TAOCP.MIX.Reg.ofOffset maps C mod 8C \bmod 8 to the register, and TAOCP.MIX.State.exec dispatches on ⌊C/8⌋\lfloor C / 8 \rfloor.

Example P7.3.26. The code 2626 has ⌊26/8⌋=3\lfloor 26 / 8 \rfloor = 3, a store, and 26 mod 8=226 \bmod 8 = 2, so it is ST2. The code 6363 has 63 mod 8=763 \bmod 8 = 7 in the comparison group, so it is CMPX. The code 4848 is in the transfer group with 48 mod 8=048 \bmod 8 = 0, so it is one of INCA, DECA, ENTA and ENNA, chosen by F=0,1,2,3F = 0, 1, 2, 3.

Theorem P7.3.27 (load by field). Let the cell MM hold the word ww and let the field be (L:R)(L:R). The instruction LDrr with this field puts into the register rr the word whose magnitude is the field magnitude of ww, right justified, and whose sign is the sign of ww if L=0L = 0 and ++ otherwise. The instruction LDrrN puts the same magnitude with the opposite sign. For an index register the instruction needs the bytes 11 to 33 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 ⌊∣w∣/b5−R⌋ mod bℓ\lfloor |w| / b^{5-R} \rfloor \bmod b^{\ell} by the theorem on fields, and a value below bℓb^{\ell} is a row of the bytes bf…bRb_f \dots b_R with zero bytes to the left. If ℓ≤2\ell \le 2 these zero bytes include the bytes 11 to 33 of the register. □\square

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 11 to 33.

Example P7.3.28. The cell holds − ∣ 17 ∣ 0 ∣ 5 ∣ 63 ∣ 2-\,|\,17\,|\,0\,|\,5\,|\,63\,|\,2. LDA with (2:4)(2:4) puts + ∣ 0 ∣ 0 ∣ 0 ∣ 5 ∣ 63+\,|\,0\,|\,0\,|\,0\,|\,5\,|\,63 in A: the bytes 0,5,630, 5, 63 right justified, the sign plus because L=2L = 2. The value is 383383. LDA with (0:2)(0:2) gives − ∣ 0 ∣ 0 ∣ 0 ∣ 17 ∣ 0-\,|\,0\,|\,0\,|\,0\,|\,17\,|\,0 with the value −1088-1088. LDAN with (0:2)(0:2) gives + ∣ 0 ∣ 0 ∣ 0 ∣ 17 ∣ 0+\,|\,0\,|\,0\,|\,0\,|\,17\,|\,0.

Example P7.3.29. LD1 with (4:5)(4:5) on the same cell puts + ∣ 63 ∣ 2+\,|\,63\,|\,2 in I1, the value 63⋅64+2=403463 \cdot 64 + 2 = 4034. LD1 with (1:5)(1:5) would need the bytes 17,0,517, 0, 5 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 STrr with the field (L:R)(L:R) puts the ℓ\ell 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 L=0L = 0. 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 (0:2)(0:2).

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 11 to 33 equal to zero, and the field (0:2)(0:2) takes its low bytes b4,b5b_4, b_5 into the bytes 11 and 22 of the cell, which is the address part of an instruction. □\square

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 vv and the field value of the word ww takes the product of the magnitudes ∣a∣ ∣v∣|a|\,|v|, 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 vv, also for the product zero. No overflow is possible, because ∣a∣ ∣v∣≤(b5−1)2<b10|a|\,|v| \le (b^5 - 1)^2 < b^{10}.

Proof. Write P=∣a∣ ∣v∣P = |a|\,|v|. Then P=qb5+rP = qb^5 + r with q=⌊P/b5⌋q = \lfloor P / b^5 \rfloor and 0≤r<b50 \le r < b^5, and q<b5q < b^5 since P<b10P < b^{10}. A gets qq and X gets rr. □\square

Lean: TAOCP.MIX.Word.mul returns the pair and TAOCP.MIX.State.execMul executes it.

Example P7.3.32. A holds +1073741823+1073741823 and the cell holds +3+3. The product is 3221225469=2⋅645+10737418213221225469 = 2 \cdot 64^5 + 1073741821. So A gets +2+2 and X gets +1073741821+1073741821. If the cell holds −3-3, both A and X get the sign minus.

Theorem P7.3.33 (division). Let the pair A, X have the magnitude N=∣a∣ b5+∣x∣N = |a|\,b^5 + |x| and let the divisor have the value vv. If ∣v∣>∣a∣|v| > |a| the instruction DIV puts the quotient ⌊N/∣v∣⌋\lfloor N / |v| \rfloor in A with the sign of the product of the sign of A and the sign of vv, and it puts the remainder N mod ∣v∣N \bmod |v| in X with the sign of A. If ∣v∣≤∣a∣|v| \le |a| the quotient does not fit in a word. The model then sets OV and leaves A and X unchanged.

Proof. We show that ⌊N/∣v∣⌋<b5\lfloor N / |v| \rfloor < b^5 exactly when ∣a∣<∣v∣|a| < |v|. If ∣a∣≤∣v∣−1|a| \le |v| - 1 then N≤(∣v∣−1)b5+(b5−1)<∣v∣ b5N \le (|v| - 1) b^5 + (b^5 - 1) < |v|\,b^5, so the quotient is below b5b^5. If ∣a∣≥∣v∣|a| \ge |v| then N≥∣v∣ b5N \ge |v|\,b^5, so the quotient is at least b5b^5. When the quotient fits, N=q∣v∣+rN = q|v| + r with 0≤r<∣v∣0 \le r < |v| is the division with remainder. □\square

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 − ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 2-\,|\,0\,|\,0\,|\,0\,|\,0\,|\,2, X holds + ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 6+\,|\,0\,|\,0\,|\,0\,|\,0\,|\,6 and the divisor is +7+7. Then N=2⋅645+6=2147483654=306783379⋅7+1N = 2 \cdot 64^5 + 6 = 2147483654 = 306783379 \cdot 7 + 1. A becomes −306783379-306783379 (sign minus times plus) and X becomes −1-1 (the sign of A). If the divisor is +2+2 then ∣v∣≤∣a∣|v| \le |a|, so OV goes on and A, X stay.

Definition P7.3.35 (address transfer). The instructions INCrr and DECrr add MM to the register or subtract MM from it, as ADD and SUB do for A and X. The instructions ENTrr and ENNrr put MM or −M-M in the register. For an index register the result must have a magnitude below b2b^2, or the model stops with the fault register. These instructions use MM 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 40954095 is allowed and ENT1 40964096 is a fault for b=64b = 64, since 4096=b24096 = b^2. INC1 11 on a register that holds 40954095 is also a fault. INCA 11 on A =1073741823= 1073741823 sets OV and leaves +0+0.

Theorem P7.3.37 (the time of an instruction). The time of an instruction of operation code CC and modification FF is given by this table, in units uu.

CC instruction time
00 NOP 11
1,21, 2 ADD, SUB 22
33 MUL 1010
44 DIV 1212
55 NUM, CHAR (F=0,1F = 0, 1) 1010
55 HLT (F=2F = 2) 11
66 shifts 22
77 MOVE 1+2F1 + 2F
88 to 3333 loads, negative loads, stores, STJ, STZ 22
3434 to 3838 JBUS, IOC, IN, OUT, JRED 11 plus the wait of the device
3939 the jumps on OV and CI 11
4040 to 5555 jumps on a register, address transfers 11
5656 to 6363 comparisons 22

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

Lean: TAOCP.MIX.Instr.time, with TAOCP.MIX.Instr.time_load (codes 88 to 2323), TAOCP.MIX.Instr.time_store (codes 2424 to 3333), TAOCP.MIX.Instr.time_cmp (codes 5656 to 6363), TAOCP.MIX.Instr.time_jump (codes 3434 to 5555) 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 3434 to 3838 have the time 11 in the model.

Example P7.3.38. The three instructions LDA 10001000, MUL 10011001, STX 10021002 take 2+10+2=142 + 10 + 2 = 14 units. The two instructions LDA 10001000 and ADD 10011001 take 44. The pair ENTA 55 and DIV 10001000 take 1+12=131 + 12 = 13.

Theorem P7.3.39 (time of a run). The time of a run of mm 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. □\square

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 b=64b = 64 and the word + ∣ 2 ∣ 0 ∣ 3 ∣ 13 ∣ 24+\,|\,2\,|\,0\,|\,3\,|\,13\,|\,24. The address is 2⋅64+0=1282 \cdot 64 + 0 = 128, I=3I = 3, F=13=8⋅1+5F = 13 = 8 \cdot 1 + 5, so the field is (1:5)(1:5), and C=24C = 24, which is STA. The instruction is STA 128,3(1:5)128,3(1:5). With rI3 =100= 100 it stores the magnitude of A into the cell 228228 and keeps the sign of that cell. Its time is 2u2u.

A line of code. The block LD1 5050, LDA 1000,11000,1, MUL 1000,11000,1, STA 1001,11001,1 with the cell 5050 holding +3+3 and the cells 10031003 and 10041004 holding +6+6 and +7+7 puts 33 in I1. LDA loads the cell 10031003 (+6+6), MUL gives 6⋅6=366 \cdot 6 = 36 in X and 00 in A, STA stores A =0= 0 in the cell 10041004. The time is 2+2+10+2=162 + 2 + 10 + 2 = 16. A reader who wants the product in the cell must store X, not A.

Exercises

  1. [10] Write the word of LD3 500,1(1:2)500,1(1:2) for b=64b = 64.
  2. [10] Decode the word + ∣ 1 ∣ 2 ∣ 0 ∣ 5 ∣ 26+\,|\,1\,|\,2\,|\,0\,|\,5\,|\,26.
  3. [15] With rI2 =−30= -30 and b=64b = 64, what does the model do for LDA 20,220,2, for ENTA 20,220,2 and for LDA −20,2-20,2?
  4. [15] Give the time of LDA 10001000, MUL 10011001, STA 10021002, STX 10031003.
  5. [M20] Show that F=8L+RF = 8L + R determines the pair (L,R)(L, R) for 0≤L≤R≤50 \le L \le R \le 5, and count the valid pairs and their largest code.
  6. [M20] After a DIV that does not overflow, show that the dividend NN equals q∣v∣+rq|v| + r and that 0≤r<∣v∣0 \le r < |v|.
  7. [15] A holds +5⋅644+5 \cdot 64^4 and the cell holds +64+64. Run MUL and give A and X.
  8. [20] A holds +0+0, X holds +1000+1000 and the cell holds +64+64. Run DIV and give A and X.
  9. [M20] Show that LDA mm followed by STA mm, both with the default field, leaves the cell mm unchanged.
  10. [20] Which of ENT1 50005000, ENT1 40954095, INC1 11 with rI1 =4095= 4095 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 (L:R)(L:R) 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 pp to run on by one. This section covers the instructions that change pp, 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 MM replaces pp by MM, 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 p+1p + 1, with the sign plus. The instruction JSJ is the exception: it jumps and leaves J as it is. The conditions are these.

instruction CC FF the jump is taken when
JMP 3939 00 always
JSJ 3939 11 always, J unchanged
JOV 3939 22 OV is on, and then OV goes off
JNOV 3939 33 OV is off, and if OV is on it goes off
JL, JE, JG 3939 4,5,64, 5, 6 CI is less, equal, greater
JGE, JNE, JLE 3939 7,8,97, 8, 9 CI is not less, not equal, not greater
JAN, JAZ, JAP, JANN, JANZ, JANP 4040 00 to 55 the value of A is <0<0, =0=0, >0>0, ≥0\ge 0, ≠0\ne 0, ≤0\le 0
the same for I1 to I6 and X 4141 to 4747 00 to 55 the same test on that register

The target must be a cell from 00 to 39993999, 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 C=39C = 39 and TAOCP.MIX.State.execRegJump for the registers.

Example P7.3.41. The instruction JMP 35003500 in the cell 30203020 sets p=3500p = 3500 and J =+ ∣ 47 ∣ 13= +\,|\,47\,|\,13, since 3021=47⋅64+133021 = 47 \cdot 64 + 13. The instruction JSJ 35003500 in the same cell sets p=3500p = 3500 and leaves J as it was.

Example P7.3.42. A holds −0-0 and I1 holds −5-5. JAZ jumps and JAN does not, because the value of A is 00. JANN jumps, since 0≥00 \ge 0. J1N jumps, since the value of I1 is −5<0-5 < 0, 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. □\square

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 <0<0, =0=0 or >0>0. □\square

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 C=6C = 6 move the bytes of A, or of A and X as a row of ten bytes, by MM places. The signs do not move. MM must not be negative, or the model stops with the fault shift.

instruction FF the bytes move
SLA 00 A left MM places, zeros enter on the right
SRA 11 A right MM places, zeros enter on the left
SLAX 22 the row A X left MM places
SRAX 33 the row A X right MM places
SLC 44 the row A X turns left M mod 10M \bmod 10 places
SRC 55 the row A X turns right M mod 10M \bmod 10 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 mm be the magnitude of A and N=m b5+mXN = m\,b^5 + m_X the magnitude of the row A X. SLA by kk gives the magnitude m bk mod b5m\,b^k \bmod b^5. SRA by kk gives ⌊m/bk⌋\lfloor m / b^k \rfloor. SLAX by kk gives Nbk mod b10N b^k \bmod b^{10}. SRAX by kk gives ⌊N/bk⌋\lfloor N / b^k \rfloor. SLC by kk with r=k mod 10r = k \bmod 10 gives (Nbr mod b10)+⌊N/b10−r⌋(N b^r \bmod b^{10}) + \lfloor N / b^{10-r} \rfloor.

Proof. In radix bb a left shift by kk places is the product with bkb^k, and the places that pass the left end are the digits of the multiple of b5b^5, or of b10b^{10}, that the remainder removes. A right shift by kk places drops the kk low digits, which is the floor of the quotient by bkb^k. For the turn, write NN as the row n1n2…n10n_1 n_2 \dots n_{10}. The number Nbr mod b10N b^r \bmod b^{10} is the row nr+1…n100…0n_{r+1} \dots n_{10} 0 \dots 0 with rr zeros, and ⌊N/b10−r⌋\lfloor N / b^{10-r} \rfloor is the row n1…nrn_1 \dots n_r as a number, which is below brb^r. The sum is the row nr+1…n10n1…nrn_{r+1} \dots n_{10} n_1 \dots n_r. □\square

Example P7.3.49. Take b=64b = 64 and the row A X =(+ ∣ 3 ∣ 1 ∣ 4 ∣ 1 ∣ 5 ∣) (− ∣ 9 ∣ 2 ∣ 6 ∣ 5 ∣ 3 ∣)= (+\,|\,3\,|\,1\,|\,4\,|\,1\,|\,5\,|)\,(-\,|\,9\,|\,2\,|\,6\,|\,5\,|\,3\,|). SLAX 22 gives (+ ∣ 4 ∣ 1 ∣ 5 ∣ 9 ∣ 2 ∣) (− ∣ 6 ∣ 5 ∣ 3 ∣ 0 ∣ 0 ∣)(+\,|\,4\,|\,1\,|\,5\,|\,9\,|\,2\,|)\,(-\,|\,6\,|\,5\,|\,3\,|\,0\,|\,0\,|). SRAX 33 gives (+ ∣ 0 ∣ 0 ∣ 0 ∣ 3 ∣ 1 ∣) (− ∣ 4 ∣ 1 ∣ 5 ∣ 9 ∣ 2 ∣)(+\,|\,0\,|\,0\,|\,0\,|\,3\,|\,1\,|)\,(-\,|\,4\,|\,1\,|\,5\,|\,9\,|\,2\,|). SLC 44 gives (+ ∣ 5 ∣ 9 ∣ 2 ∣ 6 ∣ 5 ∣) (− ∣ 3 ∣ 3 ∣ 1 ∣ 4 ∣ 1 ∣)(+\,|\,5\,|\,9\,|\,2\,|\,6\,|\,5\,|)\,(-\,|\,3\,|\,3\,|\,1\,|\,4\,|\,1\,|). SLA 11 on A alone gives + ∣ 1 ∣ 4 ∣ 1 ∣ 5 ∣ 0+\,|\,1\,|\,4\,|\,1\,|\,5\,|\,0 and SRA 22 on A alone gives + ∣ 0 ∣ 0 ∣ 3 ∣ 1 ∣ 4+\,|\,0\,|\,0\,|\,3\,|\,1\,|\,4.

Theorem P7.3.50 (rotations). SLC by 1010 is the identity. SRC by kk equals SLC by 10−(k mod 10)10 - (k \bmod 10). If SLC by kk is followed by SRC by kk, the row is the same as before.

Proof. The first claim is the case r=0r = 0 of the formula, since Nb0 mod b10=NN b^0 \bmod b^{10} = N and ⌊N/b10⌋=0\lfloor N / b^{10} \rfloor = 0. The second claim is the definition of the turn to the right. For the third claim, SLC by kk moves the first rr bytes to the end, and SRC by kk moves the last rr bytes back to the front. □\square

Example P7.3.51. The row 3 1 4 1 5 9 2 6 5 33\,1\,4\,1\,5\,9\,2\,6\,5\,3 under SRC 44 gives 2 6 5 3 3 1 4 1 5 92\,6\,5\,3\,3\,1\,4\,1\,5\,9, the same as SLC 66. The time of each shift is 2u2u and does not depend on MM.

Definition P7.3.52 (MOVE). The instruction MOVE M(F)M(F) copies FF words, one after the other, from the cells M,M+1,…M, M + 1, \dots to the cells d,d+1,…d, d + 1, \dots, where dd is the value of I1 when the instruction starts. After the copy I1 holds d+Fd + F. The byte FF is a number from 00 to b−1b - 1, not a field specification. The time is 1+2F1 + 2F units.

Lean: TAOCP.MIX.State.moveWords copies the words one at a time, and TAOCP.MIX.State.execMove adds FF 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 s=d−Ms = d - M. If s≤0s \le 0 or s≥Fs \ge F, then after MOVE the cell d+kd + k holds the old contents of the cell M+kM + k for 0≤k<F0 \le k < F.

Proof. If s≥Fs \ge F the blocks are disjoint, and no write changes a cell that a later step reads. If s=0s = 0 nothing changes. Let s<0s < 0. At step kk the cell M+kM + k is read. It could have been written by a step jj with d+j=M+kd + j = M + k, that is j=k−s>kj = k - s > k. This step has not run yet, so the cell holds its old contents. □\square

Theorem P7.3.54 (MOVE with an overlap, the smear). If 0<s<F0 < s < F, then after MOVE the cell d+kd + k holds the old contents of the cell M+(k mod s)M + (k \bmod s) for 0≤k<F0 \le k < F.

Proof. By strong induction on kk. For k<sk < s the cell M+kM + k has not been written, since the steps before kk write the cells d,…,d+k−1d, \dots, d + k - 1, which start at M+s>M+kM + s > M + k. So step kk copies the old contents of M+kM + k, and k mod s=kk \bmod s = k. For k≥sk \ge s the cell M+k=d+(k−s)M + k = d + (k - s) was written at step k−sk - s, and the step kk reads it after that. By the hypothesis that holds for k−sk - s, this cell holds the old contents of M+((k−s) mod s)=M+(k mod s)M + ((k - s) \bmod s) = M + (k \bmod s). The step kk copies it. □\square

Example P7.3.55. The cells 10001000 to 10041004 hold 1,2,3,4,51, 2, 3, 4, 5. With I1 =1001= 1001 the instruction MOVE 1000(3)1000(3) has s=1s = 1, F=3F = 3. Every new cell holds the old contents of the cell 10001000 since k mod 1=0k \bmod 1 = 0. The cells become 1,1,1,1,51, 1, 1, 1, 5 and I1 becomes 10041004. The time is 1+6=71 + 6 = 7. With I1 =999= 999 the same instruction has s=−1s = -1 and gives the cells 999999 to 10041004 as 1,2,3,3,4,51, 2, 3, 3, 4, 5, a correct copy.

Definition P7.3.56 (NUM and CHAR). The instruction NUM (C=5C = 5, F=0F = 0) reads the ten bytes of A X as ten decimal digits, each byte counting as its value modulo 1010, and it puts the number of these digits in A, with the sign of A. If the number is at least b5b^5, OV goes on and A holds the number modulo b5b^5. The instruction CHAR (F=1F = 1) takes the magnitude of A as a number with ten decimal digits and puts the character code 30+d30 + d of each digit dd in a byte: the first five in A and the last five in X. The signs stay. Each takes 10u10u.

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 30+d mod 1030 + d \bmod 10 of a digit).

Example P7.3.57. A holds +83939271+83939271. CHAR writes the ten digits 00839392710083939271, so A =+ ∣ 30 ∣ 30 ∣ 38 ∣ 33 ∣ 39 ∣= +\,|\,30\,|\,30\,|\,38\,|\,33\,|\,39\,| and X =+ ∣ 33 ∣ 39 ∣ 32 ∣ 37 ∣ 31 ∣= +\,|\,33\,|\,39\,|\,32\,|\,37\,|\,31\,|. NUM on these gives the digits 0,0,8,3,9,3,9,2,7,10, 0, 8, 3, 9, 3, 9, 2, 7, 1 back, since 30 mod 10=030 \bmod 10 = 0 and 38 mod 10=838 \bmod 10 = 8, and so on, and the number 8393927183939271 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 mm of A is below b5≤1005=1010b^5 \le 100^5 = 10^{10}, so CHAR writes its ten decimal digits d1…d10d_1 \dots d_{10} with leading zeros. The code of did_i is 30+di30 + d_i, and (30+di) mod 10=di(30 + d_i) \bmod 10 = d_i. NUM reads these digits and forms ∑di1010−i=m\sum d_i 10^{10-i} = m. Since m<b5m < b^5, there is no overflow. The sign of A stays through both. □\square

Definition P7.3.59 (input and output). The instructions IN, OUT, IOC, JBUS and JRED (C=34C = 34 to 3838) work with the devices numbered from 00 to 2020: tapes, disks, a card reader, a card punch, a printer and others. Each device has a block of fixed size, for example 100100 words for a tape and 2424 words for the printer. The time of an instruction is 1u1u, 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 C=34C = 34 to 3838.

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 1010 and then a loop of DEC1 11 and J1P LL, where LL is the cell of the DEC1, run ENT1 once and the pair ten times. The time is 1+10(1+1)=211 + 10(1 + 1) = 21 units. The last J1P does not jump, since I1 is 00. 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 +5+5. SLA 22 gives 5⋅642=204805 \cdot 64^2 = 20480. SLA 55 gives 5⋅645 mod 645=05 \cdot 64^5 \bmod 64^5 = 0, so the value 55 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 10001000 holds a word ww and the cells 10011001 to 10601060 are free. Put I1 =1001= 1001 and run MOVE 1000(60)1000(60). Here s=1s = 1, so every new cell holds ww. The time is 1+120=1211 + 120 = 121 for a fill of 6060 cells, two units for each cell and one for the call.

Exercises

  1. [10] The instruction JMP in the cell 30203020 jumps to 35003500. What do pp and J hold afterwards for b=64b = 64?
  2. [10] A holds −0-0. Which of JAN, JAZ, JAP, JANN, JANZ and JANP jump?
  3. [15] OV is on. The instruction JNOV TT is run twice in a row at the same cell. Describe the two runs.
  4. [15] A holds + ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9+\,|\,5\,|\,6\,|\,7\,|\,8\,|\,9. Give A after SLA 22 and after SRA 33, each from the original A.
  5. [15] The row A X is (+ ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣) (+ ∣ 6 ∣ 7 ∣ 8 ∣ 9 ∣ 10 ∣)(+\,|\,1\,|\,2\,|\,3\,|\,4\,|\,5\,|)\,(+\,|\,6\,|\,7\,|\,8\,|\,9\,|\,10\,|) with the bytes 11 to 1010 in this order. Give the row after SRC 33.
  6. [M20] Show that SLA by kk followed by SRA by kk gives the magnitude m mod b5−km \bmod b^{5-k} for 0≤k≤50 \le k \le 5.
  7. [M20] Show that SRC by kk equals SLC by 10−(k mod 10)10 - (k \bmod 10) for kk not a multiple of 1010, and that both are the identity when kk is a multiple of 1010.
  8. [15] The cells 10001000 to 10051005 hold the words a,b,c,d,e,fa, b, c, d, e, f and I1 =1002= 1002. Run MOVE 1000(4)1000(4). Give the six cells, I1 and the time.
  9. [20] Write a MIX fragment that fills the 6060 cells 20012001 to 20602060 with the word in the cell 20002000, and give its time.
  10. [15] CHAR is run with A =+1000= +1000. Give A and X, and the time.
  11. [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.
  12. [M22] A loop has the instruction pair DEC1 11, J1P LL and the loop body takes TT units. Show that a run with I1 =n≥1= n \ge 1 at the start of the loop takes n(T+2)n(T + 2) 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 b=64b = 64.

A listing has one row for each cell: the address, the instruction, and the time of the instruction. The code is at 30003000 and the data are at 10001000 and 19991999. 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 10001000 holds a count nn with 0≤n≤9980 \le n \le 998. The cells 10011001 to 1000+n1000 + n hold the words x1,…,xnx_1, \dots, x_n. The program S puts the sum x1+⋯+xnx_1 + \dots + x_n in A and in the cell 19991999.

cell instruction time note
30003000 ENTA 00 11 A ←0\leftarrow 0
30013001 LD1 10001000 22 I1 ←n\leftarrow n
30023002 J1Z 30063006 11 if n=0n = 0 go to the end
30033003 ADD 1000,11000,1 22 A ←\leftarrow A ++ the word xkx_k, where k=k = I1
30043004 DEC1 11 11 I1 ←\leftarrow I1 −1- 1
30053005 J1P 30033003 11 if I1 >0> 0 go to 30033003
30063006 STA 19991999 22 the sum goes to the cell 19991999
30073007 HLT 11

Registers: A holds the partial sum, I1 holds the index kk 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 30033003 with I1 =n= n, and the first word read is the last word xnx_n of the table, since the sum is added from the top down.

Theorem P7.3.62 (correctness and time of S). Suppose 0≤n≤9980 \le n \le 998 and ∣x1∣+⋯+∣xn∣<b5|x_1| + \dots + |x_n| < b^5. After S halts, A and the cell 19991999 hold x1+⋯+xnx_1 + \dots + x_n, I1 holds 00, OV is off if it was off, and the time is 4n+74n + 7 units.

Proof. The invariant at the cell 30033003 is: I1 =k= k with 1≤k≤n1 \le k \le n, and A holds xk+1+⋯+xnx_{k+1} + \dots + x_n (the empty sum is 00). The cell 30033003 is first reached from 30023002, when n≥1n \ge 1, with k=nk = n and A =0= 0, an empty sum. The invariant holds. Take a pass from 30033003 with the invariant true. ADD adds xkx_k, since the address is 1000+k1000 + k and the cell 1000+k1000 + k holds xkx_k. A now holds xk+⋯+xnx_k + \dots + x_n. The overflow flag is false: the absolute value of this sum is at most ∣x1∣+⋯+∣xn∣<b5|x_1| + \dots + |x_n| < b^5, so by the rule of overflow in section 1 OV is not set and A holds the exact value. DEC1 makes I1 =k−1= k - 1. If k−1>0k - 1 > 0 the jump goes to 30033003, with I1 =k−1= k - 1 and A =x(k−1)+1+⋯+xn= x_{(k-1)+1} + \dots + x_n, which is the invariant for k−1k - 1. If k−1=0k - 1 = 0 control falls to 30063006 with A =x1+⋯+xn= x_1 + \dots + x_n. For n=0n = 0 the jump of 30023002 goes to 30063006 with A =0= 0, the empty sum. In all cases STA puts A in 19991999 and HLT stops. The variant is I1: it falls by one in each pass and the loop leaves when it reaches 00, so the loop ends after nn passes. The cells 30033003, 30043004 and 30053005 run nn times each, and the other cells run once. The time is 1+2+1+n(2+1+1)+2+1=4n+71 + 2 + 1 + n(2 + 1 + 1) + 2 + 1 = 4n + 7. For n=0n = 0 the cells 30033003 to 30053005 do not run, and the time 1+2+1+2+1=7=4⋅0+71 + 2 + 1 + 2 + 1 = 7 = 4 \cdot 0 + 7 agrees. □\square

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 17u17u, and TAOCP.MIX.MaxExample.maxSpec is a proved statement of the same shape (chapter 12).

Example P7.3.63 (a trace of S). Take n=4n = 4 and the table 4,−7,10,34, -7, 10, 3. The table is read from the top: 33, then 1010, then −7-7, then 44. The state after each step is:

step cell instruction A I1 clock
1 30003000 ENTA 00 00 00 11
2 30013001 LD1 10001000 00 44 33
3 30023002 J1Z 30063006 00 44 44
4 30033003 ADD 1000,11000,1 33 44 66
5 30043004 DEC1 11 33 33 77
6 30053005 J1P 30033003 33 33 88
7 30033003 ADD 1000,11000,1 1313 33 1010
8 30043004 DEC1 11 1313 22 1111
9 30053005 J1P 30033003 1313 22 1212
10 30033003 ADD 1000,11000,1 66 22 1414
11 30043004 DEC1 11 66 11 1515
12 30053005 J1P 30033003 66 11 1616
13 30033003 ADD 1000,11000,1 1010 11 1818
14 30043004 DEC1 11 1010 00 1919
15 30053005 J1P 30033003 1010 00 2020
16 30063006 STA 19991999 1010 00 2222
17 30073007 HLT 1010 00 2323

The sum is 1010 and the clock is 23=4⋅4+723 = 4 \cdot 4 + 7. At the cell 30033003 the invariant reads: I1 =4= 4, A =0= 0; I1 =3= 3, A =3= 3; I1 =2= 2, A =13= 13; I1 =1= 1, A =6= 6. In each case A is the sum xk+1+⋯+xnx_{k+1} + \dots + x_n for k=k = I1.

Example P7.3.64 (a table that overflows). The table has n=2n = 2, x1=1073741823x_1 = 1073741823 and x2=1x_2 = 1. The hypothesis ∣x1∣+∣x2∣<b5|x_1| + |x_2| < b^5 fails. The program reads x2x_2, then x1x_1, and the second ADD gives the true sum 1073741824=b51073741824 = b^5. A becomes +0+0, OV goes on, and the time is 15=4⋅2+715 = 4 \cdot 2 + 7 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 10001000 holds a word nn with any sign, and the cell 10011001 holds +10+10. The program D puts in the cell 10021002 the number dd of decimal digits of ∣n∣|n|, with d=1d = 1 for n=0n = 0.

cell instruction time note
30003000 ENTA 00 11 A ←0\leftarrow 0
30013001 LDX 10001000 22 X ←n\leftarrow n
30023002 ENT2 00 11 I2 ←0\leftarrow 0, the count
30033003 DIV 10011001 1212 A ←⌊\leftarrow \lfloor X /10⌋/ 10 \rfloor, X ←\leftarrow X  mod 10\bmod 10
30043004 INC2 11 11 I2 ←\leftarrow I2 +1+ 1
30053005 SRAX 55 22 the quotient moves from A to X, A ←0\leftarrow 0
30063006 JXP 30033003 11 if X >0> 0 go to 30033003
30073007 ST2 10021002 22 the count goes to the cell 10021002
30083008 HLT 11

Registers: the pair A X is the ten byte number that DIV divides. At the head of the loop A is 00 and X holds the part of ∣n∣|n| that is not yet divided. I2 is the count. The instruction DIV divides a number of ten bytes by 1010. The number is ∣a∣ b5+∣x∣|a|\,b^5 + |x| with a=0a = 0, so it is ∣x∣|x|, and the sign of X is not used. The instruction SRAX 55 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 nn be a word and let dd be the number of decimal digits of ∣n∣|n| for ∣n∣≥1|n| \ge 1 and d=1d = 1 for n=0n = 0. After D halts, the cell 10021002 holds dd and the time is 16d+716 d + 7 units.

Proof. Put m=∣n∣m = |n|. The invariant at the cell 30033003 after jj passes is: A =+0= +0, I2 =j= j, and X has the magnitude ⌊m/10j⌋\lfloor m / 10^j \rfloor with the sign plus, for j≥1j \ge 1, and X =n= n for j=0j = 0. At the first arrival j=0j = 0 and the magnitude of X is m=⌊m/100⌋m = \lfloor m / 10^0 \rfloor. A pass does this. The dividend is ∣0∣ b5+∣x∣=⌊m/10j⌋=:N|0|\,b^5 + |x| = \lfloor m / 10^j \rfloor =: N. The divisor is 10>0=∣a∣10 > 0 = |a|, so there is no overflow. DIV gives A =⌊N/10⌋=⌊m/10j+1⌋= \lfloor N / 10 \rfloor = \lfloor m / 10^{j+1} \rfloor with the sign plus, and X the remainder. INC2 gives I2 =j+1= j + 1. SRAX 55 moves the quotient to X and puts 00 in A, with the signs plus. The invariant holds for j+1j + 1. JXP jumps when X >0> 0, that is when ⌊m/10j+1⌋>0\lfloor m / 10^{j+1} \rfloor > 0, and then the loop goes on.

The loop ends after the first j≥1j \ge 1 with ⌊m/10j⌋=0\lfloor m / 10^j \rfloor = 0, that is 10j>m10^j > m. For m≥1m \ge 1 the least such jj is the number of digits dd of mm, because mm has dd digits exactly when 10d−1≤m<10d10^{d-1} \le m < 10^d. For m=0m = 0 the first pass gives X =0= 0, so j=1=dj = 1 = d. The variant is the magnitude of X: it falls strictly in each pass, since ⌊N/10⌋<N\lfloor N / 10 \rfloor < N for N≥1N \ge 1, and the loop ends at 00. At the end ST2 stores dd.

Frequencies: the cells 30033003 to 30063006 run dd times each and the other cells once. The time is 1+2+1+d(12+1+2+1)+2+1=16d+71 + 2 + 1 + d(12 + 1 + 2 + 1) + 2 + 1 = 16 d + 7. □\square

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 n=805n = 805, so d=3d = 3 and the time is 16⋅3+7=5516 \cdot 3 + 7 = 55.

step cell instruction A X I2 clock
1 30003000 ENTA 00 00 00 00 11
2 30013001 LDX 10001000 00 805805 00 33
3 30023002 ENT2 00 00 805805 00 44
4 30033003 DIV 10011001 8080 55 00 1616
5 30043004 INC2 11 8080 55 11 1717
6 30053005 SRAX 55 00 8080 11 1919
7 30063006 JXP 30033003 00 8080 11 2020
8 30033003 DIV 10011001 88 00 11 3232
9 30043004 INC2 11 88 00 22 3333
10 30053005 SRAX 55 00 88 22 3535
11 30063006 JXP 30033003 00 88 22 3636
12 30033003 DIV 10011001 00 88 22 4848
13 30043004 INC2 11 00 88 33 4949
14 30053005 SRAX 55 00 00 33 5151
15 30063006 JXP 30033003 00 00 33 5252
16 30073007 ST2 10021002 00 00 33 5454
17 30083008 HLT 00 00 33 5555

The cell 10021002 holds 33, the number of digits of 805805. The pass of the steps 4 to 7 leaves X =80=⌊805/10⌋= 80 = \lfloor 805 / 10 \rfloor, the steps 8 to 11 leave 88, and the steps 12 to 15 leave 00.

Example P7.3.68 (other inputs). For n=0n = 0 the loop runs once and d=1d = 1, time 2323. For n=9n = 9 the time is 2323. For n=10n = 10 and n=99n = 99 the time is 3939. For n=100n = 100 the time is 5555. For n=−805n = -805 the result and the time are the same as for 805805. The largest word 10737418231073741823 has 1010 digits and takes 167167 units.

Program P7.3.69 (C, a copy of a block). The cells 10001000 to 10061006 hold seven words. The program C copies them to the cells 20002000 to 20062006 by one MOVE.

cell instruction time note
30003000 ENT1 20002000 11 I1 ←2000\leftarrow 2000
30013001 MOVE 1000(7)1000(7) 1515 copy 77 words to the cells from I1
30023002 HLT 11

Theorem P7.3.70 (correctness and time of C). After C halts the cells 2000+k2000 + k hold the old contents of the cells 1000+k1000 + k for 0≤k<70 \le k < 7, I1 =2007= 2007 and the time is 1717. For a block of n≤63n \le 63 words the same program with MOVE 1000(n)1000(n) takes 2n+32n + 3 units.

Proof. The blocks 1000,…,10061000, \dots, 1006 and 2000,…,20062000, \dots, 2006 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 77 to 20072007. The time is 1+(1+2⋅7)+1=171 + (1 + 2 \cdot 7) + 1 = 17. For nn words it is 1+(1+2n)+1=2n+31 + (1 + 2n) + 1 = 2n + 3. □\square

Program P7.3.71 (L, the same copy by a loop). The program L copies n≥1n \ge 1 words with a loop that runs from the top of the block to the bottom.

cell instruction time note
30003000 ENT1 nn 11 I1 ←n\leftarrow n
30013001 LDA 999,1999,1 22 A ←\leftarrow the word at 999+999 + I1
30023002 STA 1999,11999,1 22 store it at 1999+1999 + I1
30033003 DEC1 11 11
30043004 J1P 30013001 11
30053005 HLT 11

Theorem P7.3.72 (time of L, and the comparison). The program L copies the cells 1000,…,999+n1000, \dots, 999 + n to 2000,…,1999+n2000, \dots, 1999 + n and takes 6n+26n + 2 units. The program C takes 2n+32n + 3 units. So 6n+2−(2n+3)=4n−16n + 2 - (2n + 3) = 4n - 1, and C is faster for each n≥1n \ge 1, and it uses three cells of code against six.

Proof. At I1 =k= k the loop copies the cell 999+k999 + k to the cell 1999+k1999 + k. The values k=n,n−1,…,1k = n, n - 1, \dots, 1 cover the nn cells of the block. The cells 30013001 to 30043004 run nn times, with the time 2+2+1+1=62 + 2 + 1 + 1 = 6 each, and ENT1 and HLT run once. The time is 6n+26n + 2. □\square

Example P7.3.73 (the times for n=7n = 7). C takes 1717 units and L takes 4444. The difference is 27=4⋅7−127 = 4 \cdot 7 - 1. For n=63n = 63, the largest block of one MOVE with b=64b = 64, C takes 129129 and L takes 380380.

Example P7.3.74 (a trace of C). The state after each step is:

step cell instruction I1 clock
1 30003000 ENT1 20002000 20002000 11
2 30013001 MOVE 1000(7)1000(7) 20072007 1616
3 30023002 HLT 20072007 1717

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 4u4u per round, and the whole run takes 17u17u. The formula 4n+74n + 7 at n=3n = 3 gives 1919. The project program has ENT1 in place of LD1 and has no J1Z, so it is 2u2u 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 11 if no overflow occurred.

Exercises

  1. [10] Give the time of S for a table of 55 words and for a table of 5050 words.
  2. [15] Run S by hand for n=2n = 2 and the table 6,−16, -1. Give the state after each step and the time.
  3. [M20] Show that if the table of S has n=2n = 2 words 10737418231073741823 and 11, then OV is on at the end and A =+0= +0. What is A for the table −1073741823,−2-1073741823, -2?
  4. [15] Run D for n=1073741823n = 1073741823. How many passes, and what time?
  5. [20] Change the cell 10011001 of D to +2+2. What does D compute now, and what is its time for n=1000n = 1000?
  6. [M22] Show that D never faults and never sets OV for any nn with ∣n∣<b5|n| < b^5 and the cell 10011001 equal to +10+10.
  7. [15] Give the times of C and of L for n=20n = 20.
  8. [M25] Let the source block start at M=1000M = 1000 and the destination block at d=M+sd = M + s, and change the address of the STA of L to d−1d - 1. Find all the values of ss for which L copies a block of n≥1n \ge 1 words correctly, and compare with the values of ss for which MOVE is correct.
  9. [M22] Write a version of S that reads the table from x1x_1 to xnx_n, with I1 running from −n-n to −1-1 and the address 1000+n+11000 + n + 1. The number n≥1n \ge 1 is known when the program is written. Give its time and say what goes wrong for n=0n = 0.
  10. [25] In D replace SRAX 55 by a store of A in the cell 10031003, a load of X from this cell and ENTA 00. 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 17u17u. 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 1010 costs most in the DIV, which takes 12u12u out of the 16u16u 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 2u2u for each word and a loop uses 6u6u, 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 b=64b = 64, 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.