taocp

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

Volume C1 · Basic concepts · TAOCP Section 1.3.1

Description of MIX

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read it next to Section 1.3.1 of the book. The nodes and the exercises of the section.

Contents
  1. The MIX machine

The MIX machine

Overview

Section 1.3.1 of TAOCP defines MIX, a computer that does not exist and that has the features of the machines of its time. A program for MIX has an exact running time, counted in units called u, so the cost of an algorithm is a number that any reader can recompute. The section gives the word and its byte structure, the registers and the two indicators, the format of an instruction, and then the instructions in groups: loading, storing, arithmetic, address transfer, comparison, jumps, shifts, moving a block, conversion between numbers and characters, and the input and output devices. It ends with the table of all operation codes and the table of times.

The section uses only the notation of Section 1.1 and the integers of Section 1.2.4. It is used by every program of the book from Section 1.3.2 on. The chapter C1 for Section 1.3.2 gives the assembly language and the first programs. All the numbers in this chapter that are bytes or words were computed on a simulator of MIX that follows the rules stated here. Unless a line says otherwise, an example uses the byte size b=64b = 64.

The ideas

A word is five bytes and a sign. MIX does not fix the size of a byte, only that a byte holds at least 64 and at most 100 values. A word is a sign and five bytes, so it holds a sign and a number of at most b5−1b^5 - 1. A correct program must give the right answer for every byte size, so a program never relies on the fact that a byte holds exactly 64 values. This is also why an address of at most 3999 always fits in two bytes.

Registers are small memories with fixed roles. The accumulator A and the extension X hold a full word. The six index registers I1 to I6 hold two bytes and a sign, and the jump register J holds two bytes and no sign. Two indicators record facts about earlier instructions: the overflow toggle is on or off, and the comparison indicator is less, equal or greater.

An instruction names a place, an index, a field and an operation. The five parts are ±AA\pm AA, I, F and C. The address of the operand is the sum of ±AA\pm AA and the content of the index register that I names, so a loop can walk through a table by changing one register. The part F usually names a field of the word, written (L:R)(L:R), and the instruction then touches only those bytes. For a few instructions F is a count, a condition or the number of a device instead.

The time is part of the definition. Each instruction takes a fixed number of units, except that the time of a move depends on its count and the time of an input or output instruction depends on the device. The time of a program is the sum, over the instructions that run, of the time of each. The proofs below that are about an instruction can be checked by hand on small values, and the examples have been checked on a simulator.

What is left undefined. An instruction whose result does not fit its destination (for example, a value of three bytes loaded into an index register), a memory reference outside 0 to 3999, and a division that overflows have no defined result. A correct program never does them, and this chapter says only that the result is undefined.

Definitions, results and proofs

Definition (the word and the byte, TAOCP Section 1.3.1). A byte is an integer from 00 to b−1b - 1, where the byte size bb is a fixed number with 64≤b≤10064 \le b \le 100. A word is a sign, ++ or −-, and five bytes b1b2b3b4b5b_1 b_2 b_3 b_4 b_5. The value of the word is ±(b1b4+b2b3+b3b2+b4b+b5)\pm (b_1 b^4 + b_2 b^3 + b_3 b^2 + b_4 b + b_5). The words with value zero have two forms, +0+0 and −0-0. A program is correct when it gives the right result for each bb in that range.

Node: 1.3.1-(1) · Lean: TAOCP.MIX.Word, TAOCP.MIX.Word.value, TAOCP.MIX.Word.mag, TAOCP.MIX.Byte, TAOCP.MIX.ByteSize

Explanation. The sign and the magnitude are separate parts, as on a machine with signed magnitude. The two zeros are different patterns of the same value. Every test of the machine that compares values treats them as equal. The pattern matters in two places: the sign that is kept when a result is zero, which the rules below fix case by case, and a store of a field with L=0L = 0, which copies the sign.

Example. The characters M, I, X have codes 14,9,2714, 9, 27 (see the table of the character code below). The word + 14  9  27  0  0+\,14\;9\;27\;0\;0 spells MIX followed by two spaces. For b=64b = 64 its value is 14⋅644+9⋅643+27⋅642=234881024+2359296+110592=23735091214 \cdot 64^4 + 9 \cdot 64^3 + 27 \cdot 64^2 = 234881024 + 2359296 + 110592 = 237350912. For b=100b = 100 the same bytes have the value 14⋅108+9⋅106+27⋅104=140927000014 \cdot 10^8 + 9 \cdot 10^6 + 27 \cdot 10^4 = 1409270000. The text is the same and the numeric value is not, which is why a program that uses a word as text and as a number at once is not correct for all byte sizes.

Proof. The definition has nothing to prove. The value formula is the positional notation of base bb for a number of five digits, and each value from 00 to b5−1b^5 - 1 has exactly one set of five bytes, by the uniqueness of the expansion in base bb (division with remainder, Section 1.2.4, applied four times). Hence a word has 2b52 b^5 patterns and 2b5−12 b^5 - 1 values.

Theorem (the capacity of bytes). For each allowed bb, two bytes hold every integer from 00 to 40954095, so every memory address from 00 to 39993999 fits in two bytes. Five bytes hold every integer from 00 to 230−1=10737418232^{30} - 1 = 1073741823, so a word holds every signed integer of at most nine decimal digits. Each magnitude of a word is less than b5≤1010b^5 \le 10^{10}.

Node: 1.3.1-thm-capacity · Lean: TAOCP.MIX.Half.mag_lt, TAOCP.MIX.Word.abs_value_lt (partial)

Explanation. This is the reason for the lower limit 6464 on bb. It is also the reason why a program may compute with decimal numbers of nine digits and may use two bytes for an address. The upper limit 100100 keeps the conversion of Section 1.3.1 between a word and ten decimal digits exact (see the theorem on NUM and CHAR below).

Example. With b=64b = 64 the largest word magnitude is 645−1=107374182364^5 - 1 = 1073741823. With b=100b = 100 it is 1010−110^{10} - 1. The number 999999999999999999 is below both.

Proof. The set of values of kk bytes is {0,…,bk−1}\{0, \ldots, b^k - 1\} by the definition. Since b≥64b \ge 64, b2≥4096>3999b^2 \ge 4096 > 3999 and b5≥645=230b^5 \ge 64^5 = 2^{30}. A signed integer of at most nine digits has magnitude at most 109−1<23010^9 - 1 < 2^{30}, because 230=1073741824>1092^{30} = 1073741824 > 10^9. Since b≤100b \le 100, b5≤1010b^5 \le 10^{10}. □\square

Definition (the registers and the indicators, TAOCP Section 1.3.1). MIX has a memory of 4000 words with addresses 0,…,39990, \ldots, 3999, and the registers rA and rX of one word each, rI1 to rI6 of two bytes and a sign each, and rJ of two bytes with the sign ++. Besides these there are the overflow toggle, which is on or off, and the comparison indicator, which has the values less, equal and greater. A register that holds a word is read and written as a word. A value that does not fit in two bytes may not be put in an index register or in the jump register.

Node: 1.3.1-def-machine · Lean: TAOCP.MIX.State, TAOCP.MIX.memSize, TAOCP.MIX.Mem, TAOCP.MIX.Program.init

The table has the data of the definition.

Register Content Size
rA, the accumulator a signed integer one word
rX, the extension a signed integer one word
rI1, ..., rI6, the index registers a signed integer two bytes and a sign
rJ, the jump register an unsigned integer two bytes, always ++

Explanation. The registers rA and rX are the only ones that can hold a full word, and many instructions use them as one ten-byte register, with rA the high part. An index register adds to an address, so a program that changes it changes the place that an instruction touches. The jump register is set by the jump instructions and is meant for the return from a subroutine. The sign of rJ is always ++.

Example. After rA is set to − 1  2  3  4  5-\,1\;2\;3\;4\;5 and rI4 to + 10  3+\,10\;3, the registers hold the values −(1⋅644+2⋅643+3⋅642+4⋅64+5)=−17314053-(1 \cdot 64^4 + 2 \cdot 64^3 + 3 \cdot 64^2 + 4 \cdot 64 + 5) = -17314053 and +(10⋅64+3)=643+(10 \cdot 64 + 3) = 643 for b=64b = 64.

Proof. The definition has nothing to prove. The check of the example is 644=1677721664^4 = 16777216, 2⋅643=5242882 \cdot 64^3 = 524288, 3⋅642=122883 \cdot 64^2 = 12288, 4⋅64=2564 \cdot 64 = 256, and 16777216+524288+12288+256+5=1731405316777216 + 524288 + 12288 + 256 + 5 = 17314053.

Definition (the field of a word, TAOCP Section 1.3.1). Number the positions of a word 0,1,…,50, 1, \ldots, 5, with the sign at position 00 and the bytes at the positions 1,…,51, \ldots, 5. A field is a pair (L:R)(L:R) of integers with 0≤L≤R≤50 \le L \le R \le 5. The field (L:R)(L:R) of a word is the part at the positions LL to RR. When the field is used as a number, its value is the integer given by the bytes of the field, read as base-bb digits, with the sign of the word if L=0L = 0 and with the sign ++ if L≥1L \ge 1. The field (0:0)(0:0) is the sign alone and has the value ±0\pm 0. The full word is the field (0:5)(0:5).

Node: 1.3.1-def-field · Lean: TAOCP.MIX.Field, TAOCP.MIX.Field.of, TAOCP.MIX.Word.fieldValue

Explanation. A field lets an instruction use a part of a word as a small number, such as one letter in a word of text. The fields that matter most are (0:5)(0:5), the whole word, (1:5)(1:5), the magnitude, and, for a word that is an instruction, (0:2)(0:2), the sign with the address part, (3:3)(3:3), the index part, (4:4)(4:4), the F part and (5:5)(5:5), the C part.

Example. Let the word ww be − 7  22  5  40  9-\,7\;22\;5\;40\;9. Its fields are in the table, for b=64b = 64.

Field Bytes taken Value as a number
(0:5)(0:5) 7  22  5  40  97\;22\;5\;40\;9 −(7⋅644+22⋅643+5⋅642+40⋅64+9)-(7 \cdot 64^4 + 22 \cdot 64^3 + 5 \cdot 64^2 + 40 \cdot 64 + 9)
(1:5)(1:5) 7  22  5  40  97\;22\;5\;40\;9 +(7⋅644+⋯+9)+(7 \cdot 64^4 + \cdots + 9), the magnitude
(3:5)(3:5) 5  40  95\;40\;9 5⋅4096+40⋅64+9=230495 \cdot 4096 + 40 \cdot 64 + 9 = 23049
(0:3)(0:3) sign, 7  22  57\;22\;5 −(7⋅4096+22⋅64+5)=−30085-(7 \cdot 4096 + 22 \cdot 64 + 5) = -30085
(4:4)(4:4) 4040 +40+40
(0:0)(0:0) sign only −0-0

Proof. The definition has nothing to prove. The values in the table follow by the formula of the value of a word applied to the bytes of the field.

Theorem (the code of a field). The field (L:R)(L:R) is written in an instruction as the single number F=8L+RF = 8L + R. The map (L,R)↦8L+R(L, R) \mapsto 8L + R is injective on the pairs with 0≤L≤R≤50 \le L \le R \le 5, its values are at most 4545, and L=⌊F/8⌋L = \lfloor F / 8 \rfloor and R=F mod 8R = F \bmod 8. So FF is a byte for every b≥64b \ge 64.

Node: 1.3.1-thm-field-code · Lean: TAOCP.MIX.Field.code, TAOCP.MIX.Field.decode_code, TAOCP.MIX.Field.code_lt

Explanation. The code puts the pair in one byte of the instruction. The value F=5=8⋅0+5F = 5 = 8 \cdot 0 + 5 is the whole word, (0:5)(0:5), and it is the value that the assembler of Section 1.3.2 supplies when no field is written, for most instructions. For an instruction where F is not a field, such as a shift or a jump, F has the other meaning of that instruction.

Example. The field (3:4)(3:4) has F=8⋅3+4=28F = 8 \cdot 3 + 4 = 28. The field (0:2)(0:2) has F=2F = 2. The code F=29F = 29 gives L=3L = 3, R=5R = 5.

Proof. Since 0≤R≤5<80 \le R \le 5 < 8, the number 8L+R8L + R has RR as its remainder on division by 88 and LL as its quotient, by the uniqueness of the division with remainder (Section 1.2.4). So two different pairs give different codes. The largest code is 8⋅5+5=45<64≤b8 \cdot 5 + 5 = 45 < 64 \le b. □\square

Definition (an instruction and its effective address, TAOCP Section 1.3.1). An instruction is a word with the sign ss and the bytes A1A2IFCA_1 A_2 I F C, which are written ±AA\pm AA, I, F, C: the pair A1A2A_1 A_2 is the address part AA=A1b+A2AA = A_1 b + A_2, the byte I is the index (from 00 to 66), the byte F is the modification, and the byte C is the operation code. The effective address is M=±AA+rIIM = \pm AA + \mathrm{rI}_I where rI0=0\mathrm{rI}_0 = 0 and the sign of AAAA is ss. The contents of the memory at MM, written CONTENTS(M)\mathrm{CONTENTS}(M), is the word at address MM, and VV is the field of it that F selects, when F is a field. If the sum ±AA+rII\pm AA + \mathrm{rI}_I does not fit in two bytes and a sign, MM is undefined. An instruction that uses an address needs 0≤M≤39990 \le M \le 3999.

Node: 1.3.1-(3) · Lean: TAOCP.MIX.Instr, TAOCP.MIX.Instr.addr, TAOCP.MIX.Word.decode, TAOCP.MIX.Instr.encode, TAOCP.MIX.Instr.decode_encode, TAOCP.MIX.Word.encode_decode, TAOCP.MIX.State.effAddr

Explanation. The instruction is a number like any other word, so a program can change its own instructions, and the instruction of the program is stored in the memory. The effective address makes an array accessible: the address part points to the start and the index register selects the element. The sign of the instruction is the sign of AAAA, so a negative address part is allowed, and it is useful when the index register is large.

Example. The instruction + 31  56  4  5  8+\,31\;56\;4\;5\;8 has AA=31⋅64+56=2040AA = 31 \cdot 64 + 56 = 2040, index 44, F=5F = 5 and C=8C = 8, which is the load of a word into rA (see the table of operation codes below). If rI4 holds −10-10 then M=2040−10=2030M = 2040 - 10 = 2030. The instruction − 31  56  4  5  8-\,31\;56\;4\;5\;8 has the address part −2040-2040, and with rI4 =3000= 3000 it gives M=960M = 960.

Proof. The definition has nothing to prove. The value AA=A1b+A2AA = A_1 b + A_2 is the value of two bytes.

Definition (the symbolic form of an instruction, TAOCP Section 1.3.1). An instruction is written in text as OP ADDRESS,I(F). Here OP is the symbolic name of the operation, ADDRESS is the signed value ±AA\pm AA, I is the index and F is the field or the other modification. The part ",I" is left out when I=0I = 0, and the part "(F)" is left out when F has the normal value of the operation. The name OP fixes the code C, and for some operations it fixes F as well.

Node: 1.3.1-(4) · Lean: TAOCP.MIX.Instr.mk'

Explanation. The text form and the word are two views of one instruction. For the operations that share a code, such as the jumps on a condition, the name stands for both C and F. The assembler of Section 1.3.2 reads this form.

Example. The word + 31  56  4  5  8+\,31\;56\;4\;5\;8 of the example above is written LDA 2040,4, since F=5F = 5 is the normal field of LDA. The word − 0  12  1  11  8-\,0\;12\;1\;11\;8 is written LDA -12,1(1:3), since 11=8⋅1+311 = 8 \cdot 1 + 3. The word + 0  7  0  2  49+\,0\;7\;0\;2\;49 is written ENT1 7.

Proof. The definition has nothing to prove. In the first word, AA=2040AA = 2040, I=4I = 4, C=8C = 8 and F=5F = 5, which is LDA with its normal field. In the second word the sign is −-, AA=12AA = 12, I=1I = 1, F=11F = 11 gives the field (1:3)(1:3), and C=8C = 8 is LDA. In the third word, C=49C = 49 is the code of the address transfers of rI1, and F=2F = 2 selects ENT1. □\square

Theorem (the range of the address). For every allowed bb and every instruction with I≠0I \ne 0 and any content of the index registers, ∣M∣≤2(b2−1)|M| \le 2(b^2 - 1), and MM may be negative or larger than 39993999. A program is correct only if every memory reference has 0≤M≤39990 \le M \le 3999. Each address from 00 to 39993999 can be reached by a suitable AAAA with I=0I = 0.

Node: 1.3.1-thm-indexing · Lean: not yet stated

Explanation. This explains how an instruction with a negative address part, such as −1500-1500 with the index register rI2, can be legal: the sign and the address part alone do not have to be a valid memory address, and only the sum must be one. It also shows that the processor never needs more than two bytes of address in an instruction, since the index register supplies the rest.

Example. The instruction with address part −1500-1500 and index 22 refers to the address rI2−1500\mathrm{rI}_2 - 1500, so it is valid when 1500≤rI2≤54991500 \le \mathrm{rI}_2 \le 5499, and an index register can hold such a value only up to b2−1b^2 - 1. When rI2 is 22502250 the effective address is 750750.

Proof. The address part AAAA satisfies 0≤AA≤b2−10 \le AA \le b^2 - 1, and the index register has a magnitude of at most b2−1b^2 - 1, so the sum has a magnitude of at most 2(b2−1)2(b^2 - 1). When I=0I = 0 the effective address is ±AA\pm AA, and each number from 00 to 39993999 is an AAAA with the sign ++, since 3999<4096≤b23999 < 4096 \le b^2. □\square

Definition (the load operations LDA, LDX, LD1 to LD6, and the negated forms, TAOCP Section 1.3.1). The instruction LDr with the field (L:R)(L:R) puts the value of the field of CONTENTS(M)\mathrm{CONTENTS}(M) in the register r, as a number with the sign of the word if L=0L = 0 and with the sign ++ otherwise, shifted to the right end of the register, with zeros at the left. The instruction LDrN does the same with the opposite sign. The default field is (0:5)(0:5). If r is an index register, the value must fit in two bytes, or the result is undefined. A load takes 22 u.

Node: 1.3.1-def-load · Lean: TAOCP.MIX.Word.load, TAOCP.MIX.Word.value_load, TAOCP.MIX.State.execLoad

Explanation. The load is the way to bring data from the memory, and it is a copy, not a move: the memory word does not change. A load of a field that has the sign gives a negative register when the word is negative. A load of a field that has no sign always gives a positive register, since the sign belongs to (0:⋅)(0:\cdot) fields only. The N form is a load followed by a change of sign, in one instruction.

Example. Let location 1500 hold − 7  22  5  40  9-\,7\;22\;5\;40\;9. The loads give these results.

Instruction Register after Reason
LDA 1500 − 7  22  5  40  9-\,7\;22\;5\;40\;9 whole word
LDA 1500(1:5) + 7  22  5  40  9+\,7\;22\;5\;40\;9 the sign is not in the field
LDA 1500(2:4) + 0  0  22  5  40+\,0\;0\;22\;5\;40 three bytes, shifted right
LDA 1500(0:2) − 0  0  0  7  22-\,0\;0\;0\;7\;22 sign and two bytes, shifted right
LDA 1500(4:4) + 0  0  0  0  40+\,0\;0\;0\;0\;40 one byte
LDA 1500(0:0) − 0  0  0  0  0-\,0\;0\;0\;0\;0 the sign only
LDAN 1500(2:3) − 0  0  0  22  5-\,0\;0\;0\;22\;5 the sign ++ of the field, negated
LDX 1500(5:5) rX =+ 0  0  0  0  9= +\,0\;0\;0\;0\;9 the last byte
LD2 1500(4:5) rI2 =+ 40  9= +\,40\;9 two bytes fit in an index register
LD3N 1500(5:5) rI3 =− 0  9= -\,0\;9 the last byte, negated

Proof. Each line follows from the definition. For LDA 1500(2:4), the field is the bytes 22  5  4022\;5\;40 with the sign ++, since L=2≥1L = 2 \ge 1, and the right-justified value in a word of five bytes is + 0  0  22  5  40+\,0\;0\;22\;5\;40. For LDAN 1500(2:3) the field is 22  522\;5, with the sign ++ negated, which gives −-. For LD2 1500(4:5) the value + 40  9+\,40\;9 has two bytes and fits. The loading of a field such as (3:5)(3:5) in rI2 would have three bytes and is undefined unless the first byte is zero. The results above were checked on a simulator.

Definition (the store operations STA, STX, ST1 to ST6, STJ, STZ, TAOCP Section 1.3.1). The instruction STr with the field (L:R)(L:R) takes the rightmost R−L+1R - L + 1 bytes of the register r (or the rightmost R−LR - L bytes and the sign when L=0L = 0) and puts them in the field (L:R)(L:R) of CONTENTS(M)\mathrm{CONTENTS}(M). The other parts of the memory word do not change. The sign of the memory word changes only when L=0L = 0, and then it becomes the sign of the register. STJ is the store of rJ, with the default field (0:2)(0:2), and STZ stores zeros, with the sign ++. The default field is (0:5)(0:5) for the others. A store takes 22 u.

Node: 1.3.1-def-store · Lean: TAOCP.MIX.Word.store, TAOCP.MIX.Word.load_store, TAOCP.MIX.State.execStore

Explanation. The store is the inverse of the load. The load takes a field to the right end of a register and the store puts the right end of a register in a field. A register that is shorter than the field is taken as if the missing left bytes were zero, as is true of an index register. The default field of STJ gives the address part and the sign of an instruction, and this is how a subroutine records where to return.

Example. Let rA =+ 1  2  3  4  5= +\,1\;2\;3\;4\;5 and let location 1500 hold − 9  8  7  6  5-\,9\;8\;7\;6\;5.

Instruction Location 1500 after
STA 1500 + 1  2  3  4  5+\,1\;2\;3\;4\;5
STA 1500(3:3) − 9  8  5  6  5-\,9\;8\;5\;6\;5
STA 1500(3:4) − 9  8  4  5  5-\,9\;8\;4\;5\;5
STA 1500(0:1) + 5  8  7  6  5+\,5\;8\;7\;6\;5
STA 1500(0:0) + 9  8  7  6  5+\,9\;8\;7\;6\;5
STZ 1500(2:3) − 9  0  0  6  5-\,9\;0\;0\;6\;5

If rJ is 3456=54⋅64+03456 = 54 \cdot 64 + 0, STJ 1500 gives + 54  0  7  6  5+\,54\;0\;7\;6\;5.

Proof. For STA 1500(3:4), two bytes of the register go into the positions 3 and 4: the rightmost two bytes of 1  2  3  4  51\;2\;3\;4\;5 are 4  54\;5, so position 3 gets 44 and position 4 gets 55. The other positions are unchanged, and the sign is not in the field. For STA 1500(0:1), the sign and one byte go in: the sign of the register is ++, and the rightmost byte is 55. For STJ 1500, the field is (0:2)(0:2), the sign ++ and the two bytes 54  054\;0 of 3456=54⋅64+03456 = 54 \cdot 64 + 0. The rest follow in the same way. □\square

Definition (ADD and SUB, TAOCP Section 1.3.1). ADD with field F adds the value VV of the field of CONTENTS(M)\mathrm{CONTENTS}(M) to rA, and SUB subtracts it. The sum s=rA+Vs = \mathrm{rA} + V is formed as an exact integer. If ∣s∣<b5|s| < b^5, rA becomes ss, and if ∣s∣≥b5|s| \ge b^5 then the overflow toggle is set on and rA holds the low five bytes of ∣s∣|s| with the sign of ss. If s=0s = 0 the sign of rA does not change. The toggle is never set off by these instructions. Each takes 22 u.

Node: 1.3.1-def-add · Lean: TAOCP.MIX.Word.addInt, TAOCP.MIX.State.execAdd

Explanation. The addition is exact until the result does not fit a word. Then the machine keeps the bytes that fit and remembers the fact in the toggle, so a program can test it later with the jumps JOV and JNOV. Because the sign of a zero result is the old sign of rA, the machine has no operation that produces a zero of the other sign from an addition.

Example. With b=64b = 64:

rA before Memory value Operation rA after Toggle
+ 0  0  0  0  40+\,0\;0\;0\;0\;40 + 0  0  0  0  30+\,0\;0\;0\;0\;30 ADD + 0  0  0  1  6+\,0\;0\;0\;1\;6 off
+ 63  63  63  63  63+\,63\;63\;63\;63\;63 + 0  0  0  0  1+\,0\;0\;0\;0\;1 ADD + 0  0  0  0  0+\,0\;0\;0\;0\;0 on
− 0  0  0  0  1-\,0\;0\;0\;0\;1 + 0  0  0  0  1+\,0\;0\;0\;0\;1 ADD − 0  0  0  0  0-\,0\;0\;0\;0\;0 off
+ 0  0  0  0  5+\,0\;0\;0\;0\;5 − 0  0  0  0  2-\,0\;0\;0\;0\;2 SUB + 0  0  0  0  7+\,0\;0\;0\;0\;7 off
+ 0  0  0  0  5+\,0\;0\;0\;0\;5 + 0  0  0  0  5+\,0\;0\;0\;0\;5 SUB + 0  0  0  0  0+\,0\;0\;0\;0\;0 off

Proof. The first line: 40+30=70=1⋅64+640 + 30 = 70 = 1 \cdot 64 + 6. The second: the sum is 64564^5, which is not below b5b^5, and the low five bytes of 64564^5 are all zero. The third: −1+1=0-1 + 1 = 0, and the sign of rA stays −-. The fourth: 5−(−2)=75 - (-2) = 7. The fifth: 5−5=05 - 5 = 0 with the old sign ++. □\square

Theorem (the product, MUL). The instruction MUL forms the product p=rA⋅Vp = \mathrm{rA} \cdot V where VV is the field of CONTENTS(M)\mathrm{CONTENTS}(M), and the ten bytes of ∣p∣|p| are put in rA (the five high bytes) and rX (the five low bytes), both with the sign ++ if the sign of rA and the sign of VV agree, and with the sign −- otherwise. In symbols, ∣rAnew∣b5+∣rXnew∣=∣rAold∣⋅∣V∣|\mathrm{rA}_{\text{new}}| b^5 + |\mathrm{rX}_{\text{new}}| = |\mathrm{rA}_{\text{old}}| \cdot |V|. The product never overflows, and the instruction takes 1010 u.

Node: 1.3.1-thm-mul · Lean: TAOCP.MIX.Word.mul, TAOCP.MIX.State.execMul

Explanation. The product of two words has at most ten bytes, so it fits exactly in the pair rA, rX. This is why the toggle is not used. The sign is set in both registers, including when the product is zero, where it is the product of the two signs.

Example. Let rA =+ 0  0  0  1  0= +\,0\;0\;0\;1\;0 (6464) and let the memory word have the value − 0  0  0  2  0-\,0\;0\;0\;2\;0 (−128-128). The product is −8192=−2⋅642-8192 = -2 \cdot 64^2, so rA =− 0  0  0  0  0= -\,0\;0\;0\;0\;0 and rX =− 0  0  2  0  0= -\,0\;0\;2\;0\;0. If location 2000 holds − 5  0  0  0  0-\,5\;0\;0\;0\;0, the field (1:1)(1:1) has the value +5+5, and the instruction MUL 2000(1:1) with rA =+ 0  0  0  0  7= +\,0\;0\;0\;0\;7 gives rA =+ 0  0  0  0  0= +\,0\;0\;0\;0\;0 and rX =+ 0  0  0  0  35= +\,0\;0\;0\;0\;35.

Proof. Both ∣rA∣|\mathrm{rA}| and ∣V∣|V| are less than b5b^5, so the product is less than b10b^{10}, and b10b^{10} is the size of ten bytes. The ten bytes of the number ∣p∣|p| in base bb are the five high bytes, whose value is ⌊∣p∣/b5⌋\lfloor |p| / b^5 \rfloor, and the five low bytes, whose value is ∣p∣ mod b5|p| \bmod b^5, by the division with remainder (Section 1.2.4). The sign rule is the rule of signs for products. In the example, 64⋅128=8192=2⋅642+0⋅64+064 \cdot 128 = 8192 = 2 \cdot 64^2 + 0 \cdot 64 + 0, and the bytes are 2  0  02\;0\;0 at the end. □\square

Theorem (the quotient and the remainder, DIV, and its overflow). Let n=∣rA∣b5+∣rX∣n = |\mathrm{rA}| b^5 + |\mathrm{rX}| and let v=∣V∣v = |V| be the magnitude of the divisor. The instruction DIV is defined when ∣rA∣<v|\mathrm{rA}| < v. Then it sets rA to the quotient q=⌊n/v⌋q = \lfloor n / v \rfloor and rX to the remainder r=n−qvr = n - q v, so that n=qv+rn = q v + r and 0≤r<v0 \le r < v. The sign of rA is ++ if the old rA and VV have the same sign and −- otherwise, and the sign of rX is the old sign of rA. If ∣rA∣≥v|\mathrm{rA}| \ge v (this includes v=0v = 0), the toggle goes on, and rA and rX become undefined. The instruction takes 1212 u.

Node: 1.3.1-thm-div · Lean: TAOCP.MIX.Word.div, TAOCP.MIX.State.execDiv

Explanation. The dividend has ten bytes and the divisor has five. The quotient must fit one word, and the theorem says exactly when it does: the condition ∣rA∣<v|\mathrm{rA}| < v, which is easy to test. The quotient and remainder obey the usual relation, and the remainder takes the sign of the dividend.

Example. With b=64b = 64, let rA =+ 0  0  0  0  0= +\,0\;0\;0\;0\;0 and rX =+ 0  0  0  1  4= +\,0\;0\;0\;1\;4, so n=68n = 68, and let V=+7V = +7. Then q=9q = 9 and r=5r = 5, and rA =+ 0  0  0  0  9= +\,0\;0\;0\;0\;9, rX =+ 0  0  0  0  5= +\,0\;0\;0\;0\;5. With rA =− 0  0  0  0  0= -\,0\;0\;0\;0\;0 and the same other data, rA =−9= -9 and rX =−5= -5. With the same rA =−0= -0 and V=−7V = -7, rA =+9= +9 and rX =−5= -5. If rA =+ 0  0  0  0  7= +\,0\;0\;0\;0\;7 and V=+7V = +7, then ∣rA∣=v|\mathrm{rA}| = v, and the instruction overflows.

Proof. The quotient is q=⌊n/v⌋q = \lfloor n / v \rfloor and it fits in a word exactly when q<b5q < b^5, that is when n<vb5n < v b^5. If ∣rA∣≤v−1|\mathrm{rA}| \le v - 1 then, since ∣rX∣≤b5−1|\mathrm{rX}| \le b^5 - 1, n≤(v−1)b5+b5−1<vb5n \le (v - 1) b^5 + b^5 - 1 < v b^5. If ∣rA∣≥v|\mathrm{rA}| \ge v then n≥vb5n \ge v b^5, so q≥b5q \ge b^5 and the quotient has no place. This proves that the quotient fits if and only if ∣rA∣<v|\mathrm{rA}| < v, which is the criterion. The relation n=qv+rn = q v + r with 0≤r<v0 \le r < v is the division algorithm (Section 1.2.4). For v=0v = 0 the inequality ∣rA∣≥0=v|\mathrm{rA}| \ge 0 = v always holds, so the division by zero is an overflow. □\square

Definition (the address transfer operations, TAOCP Section 1.3.1). For the register r, ENTr puts the number MM in r, ENNr puts −M-M in r, INCr adds MM to r and DECr subtracts MM from r. The value MM is the effective address, an integer with a sign, and the address part is not used as a memory address, so no memory access takes place. If the new value is zero, the sign is the sign of the address part of the instruction for ENT, the opposite sign for ENN, and the old sign of r for INC and DEC. For rA and rX an INC or DEC that gives a result of b5b^5 or more in magnitude sets the overflow toggle on and keeps the low five bytes. For an index register, a result that does not fit two bytes is undefined. Each takes 11 u.

Node: 1.3.1-def-enter · Lean: TAOCP.MIX.State.execEnter, TAOCP.MIX.State.addReg, TAOCP.MIX.State.enterReg

Explanation. These are the instructions that put a constant or the content of an index register, plus a constant, into a register, and the instructions that count. They run in one unit since they do not touch the memory. The sign rule for zero is why ENNA 0 gives the zero with sign −-, while ENTA 0 gives the zero with sign ++.

Example. With b=64b = 64:

Instruction Before After
ENTA 2000,1 rI1 =−30= -30 rA =+ 0  0  0  30  50= +\,0\;0\;0\;30\;50 (19701970)
ENNA 0 rA =+ 1  2  3  4  5= +\,1\;2\;3\;4\;5 rA =− 0  0  0  0  0= -\,0\;0\;0\;0\;0
ENTA 0 rA =− 1  2  3  4  5= -\,1\;2\;3\;4\;5 rA =+ 0  0  0  0  0= +\,0\;0\;0\;0\;0
INC1 7 (after ENT1 0,2) rI2 =−3= -3 rI1 =+4= +4
INCA 1 rA =+ 63  63  63  63  63= +\,63\;63\;63\;63\;63 rA =+ 0  0  0  0  0= +\,0\;0\;0\;0\;0, toggle on
DECA 5 rA =+ 0  0  0  0  3= +\,0\;0\;0\;0\;3 rA =− 0  0  0  0  2= -\,0\;0\;0\;0\;2
ENN1 −9-9 any rI1 =+9= +9
DEC2 4 rI2 =− 0  4= -\,0\;4 rI2 =− 0  8= -\,0\;8

Proof. The line ENTA 2000,1 with rI1 =−30= -30 has M=2000−30=1970=30⋅64+50M = 2000 - 30 = 1970 = 30 \cdot 64 + 50. The line ENNA 0 has M=0M = 0 with the sign of the instruction negated, so the sign of the result is −-. The line ENTA 0 sets the sign to that of the instruction, ++, whatever rA was. The sum 63⋯63+163 \cdots 63 + 1 is 64564^5, which overflows, and the low five bytes are zero. The line DEC2 4 gives −4−4=−8-4 - 4 = -8. □\square

Definition (comparison, TAOCP Section 1.3.1). The instruction CMPr with the field (L:R)(L:R) compares the field (L:R)(L:R) of the register r with the field (L:R)(L:R) of CONTENTS(M)\mathrm{CONTENTS}(M) as numbers (in the sense of the definition of a field) and sets the comparison indicator to less, equal or greater by the order of the value of the register field and the value of the memory field. The values +0+0 and −0-0 are equal. The registers and the memory are unchanged. The default field is (0:5)(0:5), and the instruction takes 22 u. The indicator is changed by no other instruction.

Node: 1.3.1-def-compare · Lean: TAOCP.MIX.State.execCmp

Explanation. The result is stored in the indicator so that later jumps can test it. Comparing a field of an index register compares a part of its two bytes, as the register is read as a word with leading zero bytes. When L≥1L \ge 1 the signs are not used, so the comparison is on magnitudes.

Example. Let rA =− 1  2  9  0  0= -\,1\;2\;9\;0\;0 and let the memory word be + 1  2  3  0  0+\,1\;2\;3\;0\;0.

Instruction Compared values Indicator
CMPA (full word) −- with bytes 1  2  9  0  01\;2\;9\;0\;0 against ++ with bytes 1  2  3  0  01\;2\;3\;0\;0 less
CMPA (0:3) − 1  2  9  vs  + 1  2  3-\,1\;2\;9 \;\text{vs}\; +\,1\;2\;3 less
CMPA (1:3) + 1  2  9  vs  + 1  2  3+\,1\;2\;9 \;\text{vs}\; +\,1\;2\;3 greater

The first two lines come out less since the register is negative. The third ignores the signs and gives greater. For rI1 =−1285= -1285 and a memory word + 0  0  0  20  5+\,0\;0\;0\;20\;5, CMP1 with the field (4:5)(4:5) gives equal, since the field of the register has the value +(20⋅64+5)=1285+(20 \cdot 64 + 5) = 1285 and the sign is not in the field. A comparison of +0+0 with −0-0 gives equal.

Proof. The field comparison is the comparison of two integers, so the indicator is determined by their order, and the three outcomes exclude each other. The example values follow from the definition of the value of a field, with the sign only when L=0L = 0. □\square

Definition (the jumps and the jump register, TAOCP Section 1.3.1). A jump instruction either goes on to the next instruction or sets the location counter to MM. When the jump is taken, rJ is set to the address of the instruction that follows the jump, except for JSJ, which does not change rJ. The conditions are in the table below. Each jump takes 11 u. A register is tested by its value, so −0-0 is zero.

Node: 1.3.1-def-jump · Lean: TAOCP.MIX.State.execJump, TAOCP.MIX.State.execRegJump, TAOCP.MIX.State.jump

The table has the data of the definition.

Instruction Jumps when
JMP always
JSJ always, and rJ is not set
JOV the toggle is on, and the toggle is set off
JNOV the toggle is off, and the toggle is set off in either case
JL, JE, JG, JGE, JNE, JLE the comparison indicator is less, equal, greater, not less, not equal, not greater
JrN, JrZ, JrP, JrNN, JrNZ, JrNP for r a register the register is negative, zero, positive, not negative, not zero, not positive

Explanation. The jump register makes a subroutine call possible: the caller jumps to the subroutine, and rJ holds the place to return to. The subroutine stores rJ in the address part of its exit instruction, and that instruction jumps back. The overflow toggle is set off by the two tests of it, so a program tests it once after the operation of interest. JSJ is for the jump that must not destroy the return address.

Example. A call and a return, with the locations written first.

Location Instruction Effect
3000 JMP 3100 rJ becomes 3001
3001 ENTX 5
3002 HLT
3100 STJ 3102 the address part of 3102 becomes 3001
3101 ENTA 9
3102 JMP 0 now JMP 3001

The run takes 1+2+1+1+1+1=71 + 2 + 1 + 1 + 1 + 1 = 7 u including the HLT, and ends with rA =9= 9 and rX =5= 5. For a register jump, J2P at location 3000 with rI2 =5= 5 jumps and sets rJ to 30013001. With rA =−0= -0 the instructions JAN and JAP do not jump and JAZ, JANN and JANP do, and with the indicator equal, JE, JGE and JLE jump and JL, JG and JNE do not.

Proof. The values follow from the table. In the call, the jump at 3000 is taken, and the next instruction has the address 3001, so rJ =3001= 3001. STJ with the default field (0:2)(0:2) stores the sign ++ and the two bytes of rJ, 46  5746\;57 (since 3001=46⋅64+573001 = 46 \cdot 64 + 57) in the address part of location 3102. The word is then the instruction JMP 3001, and it returns to ENTX 5. The time is the sum of the times of the six instructions that run: JMP 11, STJ 22, ENTA 11, JMP 11, ENTX 11, HLT 11. □\square

Definition (the shifts, TAOCP Section 1.3.1). The shift instructions move the bytes of rA, or of rA and rX as ten bytes, with the count M≥0M \ge 0. SLA, SRA shift rA left and right and bring in zero bytes. SLAX, SRAX do the same with the ten bytes of rA and rX. SLC and SRC rotate the ten bytes to the left and to the right, so that a byte that leaves one end comes in at the other. The signs of rA and rX do not change. Each takes 22 u.

Node: 1.3.1-def-shift · Lean: TAOCP.MIX.State.execShift, TAOCP.MIX.Word.shiftLeftA, TAOCP.MIX.Word.shiftRightA, TAOCP.MIX.Word.shiftLeftAX, TAOCP.MIX.Word.shiftRightAX, TAOCP.MIX.Word.rotateLeftAX, TAOCP.MIX.Word.rotateRightAX

Explanation. A shift of one byte to the left multiplies by bb, and to the right it divides by bb and drops the remainder. The rotation keeps all bytes. With rotations a program can look at each byte in turn.

Example. Let rA =+ 11  12  13  14  15= +\,11\;12\;13\;14\;15 and rX =− 21  22  23  24  25= -\,21\;22\;23\;24\;25. Each line starts from these values.

Instruction rA rX
SLA 2 + 13  14  15  0  0+\,13\;14\;15\;0\;0 − 21  22  23  24  25-\,21\;22\;23\;24\;25
SRA 1 + 0  11  12  13  14+\,0\;11\;12\;13\;14 − 21  22  23  24  25-\,21\;22\;23\;24\;25
SLAX 3 + 14  15  21  22  23+\,14\;15\;21\;22\;23 − 24  25  0  0  0-\,24\;25\;0\;0\;0
SRAX 4 + 0  0  0  0  11+\,0\;0\;0\;0\;11 − 12  13  14  15  21-\,12\;13\;14\;15\;21
SLC 4 + 15  21  22  23  24+\,15\;21\;22\;23\;24 − 25  11  12  13  14-\,25\;11\;12\;13\;14
SRC 3 + 23  24  25  11  12+\,23\;24\;25\;11\;12 − 13  14  15  21  22-\,13\;14\;15\;21\;22
SRC 10 + 11  12  13  14  15+\,11\;12\;13\;14\;15 − 21  22  23  24  25-\,21\;22\;23\;24\;25
SLC 12 + 13  14  15  21  22+\,13\;14\;15\;21\;22 − 23  24  25  11  12-\,23\;24\;25\;11\;12
SRA 7 + 0  0  0  0  0+\,0\;0\;0\;0\;0 − 21  22  23  24  25-\,21\;22\;23\;24\;25

Proof. The table is the definition applied to the ten bytes 11  12  13  14  15  21  22  23  24  2511\;12\;13\;14\;15\;21\;22\;23\;24\;25. For SLC 12, the rotation by 1212 is the rotation by 12 mod 10=212 \bmod 10 = 2, by the theorem below. □\square

Theorem (the algebra of the shifts). Let z0z1⋯z9z_0 z_1 \cdots z_9 be the ten bytes of rA and rX. Then SLC kk gives zk mod 10⋯z9z0⋯z(k mod 10)−1z_{k \bmod 10} \cdots z_9 z_0 \cdots z_{(k \bmod 10) - 1}, SRC kk is the rotation to the right by k mod 10k \bmod 10, SRC kk is the inverse of SLC kk, and SRC kk equals SLC (10−(k mod 10))(10 - (k \bmod 10)). A shift SLA kk, SRA kk with k≥5k \ge 5 sets rA to zero bytes, and SLAX kk, SRAX kk with k≥10k \ge 10 set both registers to zero bytes. A shift by k<5k < 5 of rA multiplies the magnitude by bkb^k modulo b5b^5 to the left, and divides it by bkb^k with the integer part to the right. No shift sets the overflow toggle.

Node: 1.3.1-thm-shift · Lean: not yet stated

Explanation. The theorem lets a program know what any count does, also a count of 1010 or more. It also says that a left shift loses the high bytes quietly: the toggle is not used.

Example. In the table above, SRC 10 is the identity, and SLC 12 is the same as SLC 2. Also SRC 3 is the same as SLC 7, since 10−3=710 - 3 = 7: the rotation to the left by 77 gives 23  24  25  11  12  13  14  15  21  2223\;24\;25\;11\;12\;13\;14\;15\;21\;22, the same ten bytes as the table.

Proof. A rotation by one place to the left sends ziz_i to the position i−1 mod 10i - 1 \bmod 10. Doing it kk times sends ziz_i to (i−k) mod 10(i - k) \bmod 10, so the new sequence has z(j+k) mod 10z_{(j + k) \bmod 10} at position jj. This depends only on k mod 10k \bmod 10. The rotation to the right by kk sends ziz_i to (i+k) mod 10(i + k) \bmod 10, which is the inverse map, and it equals the rotation to the left by −k≡10−(k mod 10)-k \equiv 10 - (k \bmod 10) modulo 1010. For SLA kk the new bytes of rA are zk⋯z40⋯0z_k \cdots z_4 0 \cdots 0, and they are all zero when k≥5k \ge 5; the value of the new bytes is bk⋅∣rA∣ mod b5b^k \cdot |\mathrm{rA}| \bmod b^5. For SRA kk the new bytes are 0⋯0z0⋯z4−k0 \cdots 0 z_0 \cdots z_{4-k}, and the value is ⌊∣rA∣/bk⌋\lfloor |\mathrm{rA}| / b^k \rfloor. The same holds for the ten bytes with 1010 in place of 55. □\square

Definition (MOVE, TAOCP Section 1.3.1). The instruction MOVE with F=cF = c copies cc words, one at a time and in this order, from the addresses M,M+1,…,M+c−1M, M + 1, \ldots, M + c - 1 to the addresses rI1,rI1+1,…\mathrm{rI}_1, \mathrm{rI}_1 + 1, \ldots, and at each copy rI1 is increased by 11. After the instruction rI1 has increased by cc. If c=0c = 0 nothing happens. The instruction takes 1+2c1 + 2c u.

Node: 1.3.1-def-move · Lean: TAOCP.MIX.State.execMove, TAOCP.MIX.State.moveWords

Explanation. The block move replaces a loop that loads and stores, and it is cheaper: such a loop takes more than 44 u per word and the move takes 22 u. The copy is made word by word, so the result when the two blocks overlap depends on the order.

Example. Let the locations 1200 to 1205 hold the numbers 1,2,3,4,5,61, 2, 3, 4, 5, 6. Each run below starts from these values. With rI1 =2000= 2000 and MOVE 1200(4), the locations 2000 to 2003 hold 1,2,3,41, 2, 3, 4 and rI1 =2004= 2004, and the time is 1+8=91 + 8 = 9 u. With rI1 =1202= 1202 and MOVE 1200(4), the locations 1200 to 1205 hold 1,2,1,2,1,21, 2, 1, 2, 1, 2 and rI1 =1206= 1206. If the locations 1198 and 1199 hold 00, then with rI1 =1198= 1198 and MOVE 1200(3), the locations 1198 to 1202 hold 1,2,3,2,31, 2, 3, 2, 3 afterwards: the words 1,2,31, 2, 3 moved down by two places, and the locations 1201 and 1202 keep their old words 22 and 33.

Proof. The results are the definition applied in order, one word at a time. The second run is the case d=2d = 2, c=4c = 4 of the next theorem.

Theorem (the overlap of MOVE). Let rI1 =M+d= M + d with d≥1d \ge 1 and let c>dc > d. Then after MOVE M(c)M(c) the word at the address M+d+kM + d + k is the original word at M+(k mod d)M + (k \bmod d) for 0≤k<c0 \le k < c. In particular for d=1d = 1 the cc words after MM all equal the original word at MM. If rI1 <M< M or rI1 ≥M+c\ge M + c, the block that is moved is the original block.

Node: 1.3.1-thm-move · Lean: not yet stated

Explanation. This is the way to fill a block with copies of one word or with a repeating pattern of length dd, and it is also the trap when the source and the target overlap in the wrong direction. A program that wants to move a block up by one place without damage uses a loop from the top down, not MOVE.

Example. Let wkw_k be the original word at M+kM + k. The second run of the example above has M=1200M = 1200, d=2d = 2 and c=4c = 4, so the words at 1202,…,12051202, \ldots, 1205 are w0,w1,w0,w1=1,2,1,2w_0, w_1, w_0, w_1 = 1, 2, 1, 2, and the words at 1200 and 1201 do not change. With d=1d = 1 and c=3c = 3, the three words after MM would all be w0w_0.

Proof. Induction on kk. At the step kk the word at M+kM + k is read and the word at M+d+kM + d + k is written. For k<dk < d the read address M+kM + k has not been written, since all the writes so far are at the addresses M+dM + d or above, so the word read is the original wkw_k. For k≥dk \ge d the address M+k=M+d+(k−d)M + k = M + d + (k - d) was written at the step k−dk - d, with the value w(k−d) mod d=wk mod dw_{(k - d) \bmod d} = w_{k \bmod d} by the induction hypothesis, so the step kk copies wk mod dw_{k \bmod d} to M+d+kM + d + k. If rI1<M\mathrm{rI}_1 < M, every write at the step kk is at an address below M+kM + k and so below every address that is read at a later step, and no address read has been written. If rI1≥M+c\mathrm{rI}_1 \ge M + c, every write is at an address of at least M+cM + c and every read is below M+cM + c. In both cases each word read is an original word, so the block moved is the original block. □\square

Definition (NUM, CHAR and the miscellaneous operations, TAOCP Section 1.3.1). NUM reads the ten bytes of rA and rX as ten decimal digits, the digit being the byte value modulo 1010, and puts the number so formed, ∑di109−i\sum d_i 10^{9 - i}, in rA, with the sign of rA. If the number is b5b^5 or larger, the toggle is set on and rA holds the number modulo b5b^5. CHAR puts in rA and rX the ten character codes of the decimal digits of ∣rA∣|\mathrm{rA}|, with leading zeros, each code being 30+d30 + d. The signs of rA and rX do not change. NOP does nothing and takes 11 u. HLT stops the machine and takes 11 u. NUM and CHAR take 1010 u.

Node: 1.3.1-def-conversion · Lean: TAOCP.MIX.Word.num, TAOCP.MIX.Word.char, TAOCP.MIX.State.execSpecial

Explanation. The character code puts the digit dd at the code 30+d30 + d, and 30≡0 mod 1030 \equiv 0 \bmod 10, so NUM reads the digits of a text written with that code. A program that reads a number from a card has the characters in memory words, and it makes the number with NUM. To print a number, it makes the digits with CHAR.

Example. Let rA =− 0  0  0  31  32= -\,0\;0\;0\;31\;32 and rX =+ 33  34  35  36  37= +\,33\;34\;35\;36\;37. The bytes modulo 1010 are 0,0,0,1,2,3,4,5,6,70, 0, 0, 1, 2, 3, 4, 5, 6, 7, so NUM gives ∣rA∣=1234567|\mathrm{rA}| = 1234567 and the sign stays −-: rA =− 0  4  45  26  7= -\,0\;4\;45\;26\;7 for b=64b = 64, since 1234567=4⋅643+45⋅642+26⋅64+71234567 = 4 \cdot 64^3 + 45 \cdot 64^2 + 26 \cdot 64 + 7. If all ten bytes are 99, the number is 99999999999999999999, which is not below 64564^5, and rA gets 9999999999−9⋅645=3363235839999999999 - 9 \cdot 64^5 = 336323583 with the toggle on. For rA =+3000= +3000, CHAR gives rA =+ 30  30  30  30  30= +\,30\;30\;30\;30\;30 and rX =+ 30  33  30  30  30= +\,30\;33\;30\;30\;30, the digits 00000030000000003000.

Proof. The three values: 4⋅262144=10485764 \cdot 262144 = 1048576, 45⋅4096=18432045 \cdot 4096 = 184320, 26⋅64=166426 \cdot 64 = 1664, and the sum with 77 is 12345671234567. And 9⋅645=96636764169 \cdot 64^5 = 9663676416, which gives 9999999999−9663676416=3363235839999999999 - 9663676416 = 336323583. In the CHAR example the digits of 30003000 in ten places are 00000030000000003000 and each code is 30+d30 + d. □\square

Theorem (NUM and CHAR are inverse). For every allowed bb and each integer nn with 0≤∣n∣<b50 \le |n| < b^5, the pair CHAR, NUM applied to rA =n= n gives rA =n= n, the toggle is not set by it, and the sign of nn is kept. Conversely, if the ten bytes of rA and rX are the codes 30+di30 + d_i of ten digits and the number that they give is below b5b^5, then CHAR after NUM gives the same ten bytes.

Node: 1.3.1-thm-conversion · Lean: not yet stated

Explanation. The conversion between a number and its text is exact, for each byte size. The upper limit b≤100b \le 100 makes b5≤1010b^5 \le 10^{10} so that the ten digits of CHAR are enough.

Example. For rA =−3000= -3000, CHAR gives the digits of the example above, and NUM gives rA =− 0  0  0  46  56= -\,0\;0\;0\;46\;56, the number −3000-3000 again, since 46⋅64+56=300046 \cdot 64 + 56 = 3000. The simulator run agrees.

Proof. Since ∣n∣<b5≤1010|n| < b^5 \le 10^{10}, the magnitude has at most ten decimal digits d0⋯d9d_0 \cdots d_9, and CHAR writes 30+di30 + d_i in the ii-th byte. A code is at most 39<6439 < 64, so it is a byte. NUM takes each byte modulo 1010, which is did_i, since 30≡0 mod 1030 \equiv 0 \bmod 10 and 0≤di≤90 \le d_i \le 9. It forms ∑di109−i=∣n∣\sum d_i 10^{9-i} = |n|, which is below b5b^5, so there is no overflow, and the sign of rA is kept by both. For the converse, the number that NUM forms is m=∑di109−i<b5m = \sum d_i 10^{9-i} < b^5 and its ten decimal digits are the did_i, so CHAR writes the codes 30+di30 + d_i again. □\square

Definition (the character code, TAOCP Section 1.3.1). The text on the devices is a string of characters, and each character has a code in a byte, as in the table below. Codes 5656 and above are not used.

Node: 1.3.1-def-char · Lean: TAOCP.MIX.charOf (partial)

The table has the data of the definition.

Code 0 1-9 10 11-19 20 21 22-29 30-39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Character space A to I Delta J to R Sigma Pi S to Z 0 to 9 . , ( ) + - * / = $ < > @ ; : '

Explanation. The code is the one that every output instruction of MIX assumes, and the text fields of a program are written in it. The codes of the letters are not consecutive, since Delta, Sigma and Pi sit among them, so the code of a letter is not its place in the alphabet. This does not matter for programs that only copy text.

Example. The text PRINT has the codes P=17P = 17, R=19R = 19, I=9I = 9, N=15N = 15, T=23T = 23, so it is the word + 17  19  9  15  23+\,17\;19\;9\;15\;23. The text 2024 with a leading space has the codes 0,32,30,32,340, 32, 30, 32, 34.

Proof. The letters J, K, L, M, N, O, P, Q, R have the codes 11,12,…,1911, 12, \ldots, 19 in this order, so P=11+6=17P = 11 + 6 = 17, R=19R = 19 and N=11+4=15N = 11 + 4 = 15. The letter I is the ninth letter, with code 99. The letters S to Z have the codes 22,…,2922, \ldots, 29, so T=23T = 23. Digits have the code 30+d30 + d, so 2, 0, 2, 4 are 32,30,32,3432, 30, 32, 34. □\square

Definition (the input and output operations, TAOCP Section 1.3.1). Each device has a number, a block size, given in the table below, and a state of busy or ready. IN with the unit in FF starts the transfer of one block from the device into the words M,M+1,…M, M + 1, \ldots and OUT starts the transfer of one block from those words to the device. Both take 11 u in the processor, and the processor goes on while the device works. If the device is busy when IN, OUT or IOC is given, the processor waits until it is ready. JBUS jumps to MM if the unit FF is busy, and JRED jumps if it is ready, both taking 11 u. IOC is the control instruction: for a tape, M=0M = 0 rewinds, M<0M < 0 skips back ∣M∣|M| blocks or to the start of the tape, and M>0M > 0 skips forward. For a disk or drum, MM should be 00 and the unit is positioned by rX. For the line printer, M=0M = 0 goes to the top of the next page. For the paper tape, M=0M = 0 rewinds. On a disk or drum, the block of an IN, OUT or IOC is named by the content of rX. In character input the signs of all words are ++, and on output the signs are ignored.

Node: 1.3.1-def-io · Lean: not yet stated

The table has the data of the definition.

Unit Device Block size in words
0 to 7 magnetic tape 100
8 to 15 disk or drum 100
16 card reader 16
17 card punch 16
18 line printer 24
19 typewriter terminal 14
20 paper tape 14

Explanation. The overlap of input and output with the computation is the point of the busy state. A program starts an output and does other work, then tests the device or just gives the next command, which waits if needed. A program that reuses a buffer must wait until its transfer is done.

Example. A program that has put 24 words of text at 2000 prints them with OUT 2000(18). The processor spends 11 u, and the line printer takes its own time. A program that must not change the words at 2000 before the line is out puts an instruction JBUS to its own address, with the unit 18, ahead of the first store into the buffer. It repeats until the printer is ready.

Proof. The definition has nothing to prove. The block sizes are the sizes in the table, and the words of the block are those at the addresses MM to M+n−1M + n - 1 where nn is the size. For the printer, a block of 2424 words has 5⋅24=1205 \cdot 24 = 120 characters, which is one line.

Definition (the table of times, TAOCP Section 1.3.1). The unit of time is the u. The time of an instruction does not depend on the operands, with the values of the table below. The time of a program run is the sum of the times of the instructions that are run.

Node: 1.3.1-def-timing · Lean: TAOCP.MIX.Instr.time, TAOCP.MIX.Instr.time_load, TAOCP.MIX.Instr.time_store, TAOCP.MIX.Instr.time_cmp, TAOCP.MIX.Instr.time_jump, TAOCP.MIX.Instr.time_move (partial)

The table has the data of the definition.

Instruction Time in u
NOP, HLT, ENT, ENN, INC, DEC, all jumps, IN, OUT, IOC, JBUS, JRED 1, plus the idle time of IN, OUT, IOC, HLT
LD, LDN, ST, STJ, STZ, ADD, SUB, CMP, SLA, SRA, SLAX, SRAX, SLC, SRC 2
MUL, NUM, CHAR 10
DIV 12
MOVE with count cc 1+2c1 + 2c

Explanation. The times are small integers so that the sums are easy. They have the real ratios in mind: the memory access costs one unit more than the instruction that does not use the memory, and a multiplication or a division costs much more.

Example. A program that has 55 loads, 33 stores, 22 additions, one multiplication and one jump takes 5⋅2+3⋅2+2⋅2+10+1=315 \cdot 2 + 3 \cdot 2 + 2 \cdot 2 + 10 + 1 = 31 u.

Proof. The definition has nothing to prove. The sum in the example is 10+6+4+10+1=3110 + 6 + 4 + 10 + 1 = 31. □\square

Theorem (the time of a program). Let the instruction at the address aa run NaN_a times in a run, and let it take tat_a u. The time of the run is ∑aNata\sum_a N_a t_a. A program with an initial part of time T0T_0 that runs once and a loop body of time TT that runs nn times has the time T0+nTT_0 + n T. The program for the sum of nn words, given in the next part, takes 4n+24n + 2 u.

Node: 1.3.1-thm-time · Lean: not yet stated

Explanation. This is the formula that Section 1.3.2 and the later chapters use all the time: the frequency of each instruction, found by reasoning about the program, times the time of the instruction. The frequencies can depend on the data, as in the analysis of Section 1.2.10.

Example. In the program of the next part the frequencies are 1,1,n,n,n1, 1, n, n, n and the times are 1,1,2,1,11, 1, 2, 1, 1, so the sum is 1+1+2n+n+n=4n+21 + 1 + 2n + n + n = 4n + 2.

Proof. The time of a run is the sum of the times of the executed instructions. Group the executions by address: the address aa contributes tat_a once for each of its NaN_a executions, which gives ∑aNata\sum_a N_a t_a. The initial part contributes T0T_0 once and the body contributes TT in each of nn rounds, so the total is T0+nTT_0 + nT. For the program below, T0=1+1=2T_0 = 1 + 1 = 2 and T=2+1+1=4T = 2 + 1 + 1 = 4. □\square

Algorithms and programs

Section 1.3.1 has no algorithm. The program below is not in the book. It is given to show a MIX program with its time, the way the programs of Section 1.3.2 are analyzed.

Program (the sum of nn words, a small example for this chapter). The program adds the numbers in the n≥1n \ge 1 words at the addresses 1000,…,1000+n−11000, \ldots, 1000 + n - 1, as the values of their words, and leaves the sum in rA. The input is nn, set by the first instruction as n−1n - 1 in rI1. The output is rA, and rI1 ends at −1-1.

Node: none

Idea. The index register counts down from n−1n - 1 to 00, and the address 1000+rI11000 + \mathrm{rI}_1 walks over the words from the last to the first. The loop ends when the register becomes negative.

The program. For n=3n = 3, the first instruction is ENT1 2.

Location Instruction Time Frequency
3000 ENT1 n−1n - 1 1 1
3001 ENTA 0 1 1
3002 ADD 1000,1 2 nn
3003 DEC1 1 1 nn
3004 J1NN 3002 1 nn

The assembled words for n=3n = 3 are + 0  2  0  2  49+\,0\;2\;0\;2\;49, + 0  0  0  2  48+\,0\;0\;0\;2\;48, + 15  40  1  5  1+\,15\;40\;1\;5\;1, + 0  1  0  1  49+\,0\;1\;0\;1\;49 and + 46  58  0  3  41+\,46\;58\;0\;3\;41 for b=64b = 64 (the first number has C=49C = 49 since ENT1 is the operation code 48+148 + 1 with F=2F = 2).

A run. The words at 1000, 1001, 1002 hold 5,7,25, 7, 2. The state is shown after each instruction.

Step Instruction Time added Total time rA rI1 rJ
1 ENT1 2 1 1 0 2 0
2 ENTA 0 1 2 0 2 0
3 ADD 1000,1 2 4 2 2 0
4 DEC1 1 1 5 2 1 0
5 J1NN 3002 1 6 2 1 3005
6 ADD 1000,1 2 8 9 1 3005
7 DEC1 1 1 9 9 0 3005
8 J1NN 3002 1 10 9 0 3005
9 ADD 1000,1 2 12 14 0 3005
10 DEC1 1 1 13 14 −1-1 3005
11 J1NN 3002 1 14 14 −1-1 3005

Correctness. The invariant at the label 3002 is: rI1 =k= k with 0≤k≤n−10 \le k \le n - 1 and rA is the sum of the words at the addresses 1000+j1000 + j for k<j≤n−1k < j \le n - 1. At the first arrival, k=n−1k = n - 1 and the sum is empty, so rA =0= 0 holds. The body adds the word at 1000+k1000 + k, so rA is the sum for j≥kj \ge k, and DEC1 makes the register k−1k - 1, which restores the invariant for the new kk. The jump J1NN goes back when k−1≥0k - 1 \ge 0 and stops when k−1=−1k - 1 = -1, so the loop stops after exactly nn runs, since the register decreases by 11 and starts at n−1n - 1. At the exit, k=−1k = -1, and rA is the sum over all jj with 0≤j≤n−10 \le j \le n - 1. For the run above, the sum is 5+7+2=145 + 7 + 2 = 14.

The time. The instruction ENT1 and ENTA each run once, for 22 u. The three instructions ADD, DEC1 and J1NN run nn times each, for n(2+1+1)n(2 + 1 + 1) u. The time is 4n+24n + 2 u, and for n=3n = 3 it is 1414 u, as the table shows. The jump register ends with 30053005. The program does not depend on the byte size as long as the sum is below 64564^5. A run with b=100b = 100 on the same data gives the same sum, 1414, and the same time.

The exercises

The table has the exercises of Section 1.3.1. The kind is the class of spec 06.3 that C1 proposes, and the Lean column says that no statement exists yet. No exercise has the book's mark of a recommended exercise in the data of the map.

Exercise Rating Markers Kind Lean Solution
1.3.1-1 00 K3 not yet stated not yet written
1.3.1-2 02 K3 not yet stated not yet written
1.3.1-3 02 K3 not yet stated not yet written
1.3.1-4 00 K3 not yet stated not yet written
1.3.1-5 10 K3 not yet stated not yet written
1.3.1-6 10 K3 not yet stated not yet written
1.3.1-7 15 M K1 not yet stated not yet written
1.3.1-8 15 K3 not yet stated not yet written
1.3.1-9 15 K3 not yet stated not yet written
1.3.1-10 15 K3 not yet stated not yet written
1.3.1-11 15 K3 not yet stated not yet written
1.3.1-12 10 K3 not yet stated not yet written
1.3.1-13 10 K3 not yet stated not yet written
1.3.1-14 20 informal not yet stated not yet written
1.3.1-15 10 K3 not yet stated not yet written
1.3.1-16 20 informal not yet stated not yet written
1.3.1-17 26 informal not yet stated not yet written
1.3.1-18 22 informal not yet stated not yet written
1.3.1-19 14 K3 not yet stated not yet written
1.3.1-20 20 informal not yet stated not yet written
1.3.1-21 24 informal not yet stated not yet written
1.3.1-22 28 informal not yet stated not yet written
1.3.1-23 27 informal not yet stated not yet written
1.3.1-24 21 informal not yet stated not yet written
1.3.1-25 30 informal not yet stated not yet written
1.3.1-26 32 informal not yet stated not yet written

The exercises fall into groups. Exercises 1 to 4 test the word, the byte and the field notation, and they need no more than the definitions above. Exercises 5 to 8 apply the rules of one instruction to small values, among them the rule of the sign and the division. Exercises 9 to 11 ask which instructions can change the overflow toggle, the comparison indicator and an index register, and the answer is read from the definitions in this chapter. Exercises 12 to 14 ask about the effect of single instructions and about all the ways to build an instruction for an operation. Exercise 15 is on the sizes of the blocks of the devices. Exercises 16 to 21 are short programs for MIX, some with the aim of the shortest program and some with the aim of the fastest one, and exercises 18 and 19 are about the meaning and the time of a given program. Exercises 22 to 24 are longer programs and a question on the contents of the registers. Exercise 25 is a design problem on a larger machine, and exercise 26 is a card loader. The best order is to do the exercises of each group in order, and to do a few programs of the group 16 to 21 with a trace table, as in the program above, before going on to Section 1.3.2.

Lean

The Lean code of tamnd/taocp-lean at main has a model of MIX in the directory TAOCP/MIX/, in the namespace TAOCP.MIX. Each statement is for a byte size b with the instance TAOCP.MIX.ByteSize b, which holds for 64≤b≤10064 \le b \le 100. The Node lines name the declarations. Where the Lean statement is weaker, the Node line says "partial".

  • A word is TAOCP.MIX.Word, a sign and five bytes, and a two byte register is TAOCP.MIX.Half. The value of a word is TAOCP.MIX.Word.value. The bounds are TAOCP.MIX.Word.abs_value_lt and TAOCP.MIX.Half.mag_lt.
  • The state is TAOCP.MIX.State, with 4000 cells (TAOCP.MIX.memSize) and the start state TAOCP.MIX.Program.init. One step is TAOCP.MIX.State.step, and TAOCP.MIX.State.run runs a number of steps. The cases that the book leaves undefined are the constructors of TAOCP.MIX.Fault, and the machine stops with one of them.
  • The fields are TAOCP.MIX.Field, with TAOCP.MIX.Field.code and TAOCP.MIX.Field.decode_code. The load and the store of a field are TAOCP.MIX.Word.load and TAOCP.MIX.Word.store.
  • The instructions are in TAOCP/MIX/Semantics.lean, with one function for each family, such as TAOCP.MIX.State.execLoad and TAOCP.MIX.State.execDiv. The operations on words are in TAOCP/MIX/Ops.lean.
  • The time of an instruction is TAOCP.MIX.Instr.time. The time of a program is carried by the Hoare triples TAOCP.MIX.Triple of TAOCP/MIX/Logic/Hoare.lean, and TAOCP.MIX.Triple.seq and TAOCP.MIX.Triple.loop combine them.

The Lean statement differs from the handbook in these places.

  • The capacity of two bytes is stated as Half.mag_lt, a magnitude below b2b^2. The bound b2≥4096b^2 \ge 4096 is not stated.
  • The character code is only TAOCP.MIX.charOf, the code of a decimal digit. The table of the other characters is not stated.
  • The times are stated for loads, stores, comparisons, jumps and MOVE. The other times are in the definition of TAOCP.MIX.Instr.time, and the input and output times are not stated.
  • A word is a sign and five bytes, so +0+0 and −0-0 are different words. The registers are read with TAOCP.MIX.State.rA and the others, which give integers.

Not yet stated: the range of the address with indexing, the algebra of the shifts, the overlap of MOVE, the inverse of NUM and CHAR, the input and output operations, and the formula for the time of a program as a sum over the instructions. The Node lines for them say "not yet stated".

Notes and history

MIX is a design made for the book. It has the usual parts of a machine of its time and no part that would distract from the counting of steps. The rule of a byte size between 6464 and 100100 is a device that keeps a program from using the size of a byte, so that it is correct on a binary machine and on a decimal machine.

The same plan appears in the chapters that follow. The capacity of the word fixes the largest number that a program may form, the times of the instructions are the weights in each analysis of a program, and the undefined cases mark the limits of a correct program. The author of the book later designed a machine, MMIX, with words of 6464 bits. The programs of Volume 1 are in MIX, and the sections 1.3´ of Fascicle 1 describe MMIX.

Section 1.3.1 defines MIX, the machine for which the programs of the book are written. This chapter states each part of the definition as a block with an example and a proof: the word and the byte, the registers, the fields, the format of an instruction, every group of instructions with its time, the comparison indicator, the overflow toggle, and the input and output devices.

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