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
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 .
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 . 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 , I, F and C. The address of the operand is the sum of 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 , 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 to , where the byte size is a fixed number with . A word is a sign, or , and five bytes . The value of the word is . The words with value zero have two forms, and . A program is correct when it gives the right result for each 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 , which copies the sign.
Example. The characters M, I, X have codes (see the table of the character code below). The word spells MIX followed by two spaces. For its value is . For the same bytes have the value . 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 for a number of five digits, and each value from to has exactly one set of five bytes, by the uniqueness of the expansion in base (division with remainder, Section 1.2.4, applied four times). Hence a word has patterns and values.
Theorem (the capacity of bytes). For each allowed , two bytes hold every integer from to , so every memory address from to fits in two bytes. Five bytes hold every integer from to , so a word holds every signed integer of at most nine decimal digits. Each magnitude of a word is less than .
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 on . 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 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 the largest word magnitude is . With it is . The number is below both.
Proof. The set of values of bytes is by the definition. Since , and . A signed integer of at most nine digits has magnitude at most , because . Since , .
Definition (the registers and the indicators, TAOCP Section 1.3.1). MIX has a memory of 4000 words with addresses , 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 and rI4 to , the registers hold the values and for .
Proof. The definition has nothing to prove. The check of the example is , , , , and .
Definition (the field of a word, TAOCP Section 1.3.1). Number the positions of a word , with the sign at position and the bytes at the positions . A field is a pair of integers with . The field of a word is the part at the positions to . When the field is used as a number, its value is the integer given by the bytes of the field, read as base- digits, with the sign of the word if and with the sign if . The field is the sign alone and has the value . The full word is the field .
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 , the whole word, , the magnitude, and, for a word that is an instruction, , the sign with the address part, , the index part, , the F part and , the C part.
Example. Let the word be . Its fields are in the table, for .
| Field | Bytes taken | Value as a number |
|---|---|---|
| , the magnitude | ||
| sign, | ||
| sign only |
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 is written in an instruction as the single number . The map is injective on the pairs with , its values are at most , and and . So is a byte for every .
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 is the whole word, , 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 has . The field has . The code gives , .
Proof. Since , the number has as its remainder on division by and 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 .
Definition (an instruction and its effective address, TAOCP Section 1.3.1). An instruction is a word with the sign and the bytes , which are written , I, F, C: the pair is the address part , the byte I is the index (from to ), the byte F is the modification, and the byte C is the operation code. The effective address is where and the sign of is . The contents of the memory at , written , is the word at address , and is the field of it that F selects, when F is a field. If the sum does not fit in two bytes and a sign, is undefined. An instruction that uses an address needs .
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 , so a negative address part is allowed, and it is useful when the index register is large.
Example. The instruction has , index , and , which is the load of a word into rA (see the table of operation codes below). If rI4 holds then . The instruction has the address part , and with rI4 it gives .
Proof. The definition has nothing to prove. The value 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 , I is the index and F is the field or the other modification. The part ",I" is left out when , 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 of the example above is written LDA 2040,4, since is the normal field of LDA. The word is written LDA -12,1(1:3), since . The word is written ENT1 7.
Proof. The definition has nothing to prove. In the first word, , , and , which is LDA with its normal field. In the second word the sign is , , , gives the field , and is LDA. In the third word, is the code of the address transfers of rI1, and selects ENT1.
Theorem (the range of the address). For every allowed and every instruction with and any content of the index registers, , and may be negative or larger than . A program is correct only if every memory reference has . Each address from to can be reached by a suitable with .
Node: 1.3.1-thm-indexing · Lean: not yet stated
Explanation. This explains how an instruction with a negative address part, such as 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 and index refers to the address , so it is valid when , and an index register can hold such a value only up to . When rI2 is the effective address is .
Proof. The address part satisfies , and the index register has a magnitude of at most , so the sum has a magnitude of at most . When the effective address is , and each number from to is an with the sign , since .
Definition (the load operations LDA, LDX, LD1 to LD6, and the negated forms, TAOCP Section 1.3.1). The instruction LDr with the field puts the value of the field of in the register r, as a number with the sign of the word if 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 . If r is an index register, the value must fit in two bytes, or the result is undefined. A load takes 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 fields only. The N form is a load followed by a change of sign, in one instruction.
Example. Let location 1500 hold . The loads give these results.
| Instruction | Register after | Reason |
|---|---|---|
| LDA 1500 | whole word | |
| LDA 1500(1:5) | the sign is not in the field | |
| LDA 1500(2:4) | three bytes, shifted right | |
| LDA 1500(0:2) | sign and two bytes, shifted right | |
| LDA 1500(4:4) | one byte | |
| LDA 1500(0:0) | the sign only | |
| LDAN 1500(2:3) | the sign of the field, negated | |
| LDX 1500(5:5) | rX | the last byte |
| LD2 1500(4:5) | rI2 | two bytes fit in an index register |
| LD3N 1500(5:5) | rI3 | the last byte, negated |
Proof. Each line follows from the definition. For LDA 1500(2:4), the field is the bytes with the sign , since , and the right-justified value in a word of five bytes is . For LDAN 1500(2:3) the field is , with the sign negated, which gives . For LD2 1500(4:5) the value has two bytes and fits. The loading of a field such as 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 takes the rightmost bytes of the register r (or the rightmost bytes and the sign when ) and puts them in the field of . The other parts of the memory word do not change. The sign of the memory word changes only when , and then it becomes the sign of the register. STJ is the store of rJ, with the default field , and STZ stores zeros, with the sign . The default field is for the others. A store takes 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 and let location 1500 hold .
| Instruction | Location 1500 after |
|---|---|
| STA 1500 | |
| STA 1500(3:3) | |
| STA 1500(3:4) | |
| STA 1500(0:1) | |
| STA 1500(0:0) | |
| STZ 1500(2:3) |
If rJ is , STJ 1500 gives .
Proof. For STA 1500(3:4), two bytes of the register go into the positions 3 and 4: the rightmost two bytes of are , so position 3 gets and position 4 gets . 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 . For STJ 1500, the field is , the sign and the two bytes of . The rest follow in the same way.
Definition (ADD and SUB, TAOCP Section 1.3.1). ADD with field F adds the value of the field of to rA, and SUB subtracts it. The sum is formed as an exact integer. If , rA becomes , and if then the overflow toggle is set on and rA holds the low five bytes of with the sign of . If the sign of rA does not change. The toggle is never set off by these instructions. Each takes 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 :
| rA before | Memory value | Operation | rA after | Toggle |
|---|---|---|---|---|
| ADD | off | |||
| ADD | on | |||
| ADD | off | |||
| SUB | off | |||
| SUB | off |
Proof. The first line: . The second: the sum is , which is not below , and the low five bytes of are all zero. The third: , and the sign of rA stays . The fourth: . The fifth: with the old sign .
Theorem (the product, MUL). The instruction MUL forms the product where is the field of , and the ten bytes of 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 agree, and with the sign otherwise. In symbols, . The product never overflows, and the instruction takes 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 () and let the memory word have the value (). The product is , so rA and rX . If location 2000 holds , the field has the value , and the instruction MUL 2000(1:1) with rA gives rA and rX .
Proof. Both and are less than , so the product is less than , and is the size of ten bytes. The ten bytes of the number in base are the five high bytes, whose value is , and the five low bytes, whose value is , by the division with remainder (Section 1.2.4). The sign rule is the rule of signs for products. In the example, , and the bytes are at the end.
Theorem (the quotient and the remainder, DIV, and its overflow). Let and let be the magnitude of the divisor. The instruction DIV is defined when . Then it sets rA to the quotient and rX to the remainder , so that and . The sign of rA is if the old rA and have the same sign and otherwise, and the sign of rX is the old sign of rA. If (this includes ), the toggle goes on, and rA and rX become undefined. The instruction takes 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 , which is easy to test. The quotient and remainder obey the usual relation, and the remainder takes the sign of the dividend.
Example. With , let rA and rX , so , and let . Then and , and rA , rX . With rA and the same other data, rA and rX . With the same rA and , rA and rX . If rA and , then , and the instruction overflows.
Proof. The quotient is and it fits in a word exactly when , that is when . If then, since , . If then , so and the quotient has no place. This proves that the quotient fits if and only if , which is the criterion. The relation with is the division algorithm (Section 1.2.4). For the inequality always holds, so the division by zero is an overflow.
Definition (the address transfer operations, TAOCP Section 1.3.1). For the register r, ENTr puts the number in r, ENNr puts in r, INCr adds to r and DECr subtracts from r. The value 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 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 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 :
| Instruction | Before | After |
|---|---|---|
| ENTA 2000,1 | rI1 | rA () |
| ENNA 0 | rA | rA |
| ENTA 0 | rA | rA |
| INC1 7 (after ENT1 0,2) | rI2 | rI1 |
| INCA 1 | rA | rA , toggle on |
| DECA 5 | rA | rA |
| ENN1 | any | rI1 |
| DEC2 4 | rI2 | rI2 |
Proof. The line ENTA 2000,1 with rI1 has . The line ENNA 0 has 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 is , which overflows, and the low five bytes are zero. The line DEC2 4 gives .
Definition (comparison, TAOCP Section 1.3.1). The instruction CMPr with the field compares the field of the register r with the field of 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 and are equal. The registers and the memory are unchanged. The default field is , and the instruction takes 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 the signs are not used, so the comparison is on magnitudes.
Example. Let rA and let the memory word be .
| Instruction | Compared values | Indicator |
|---|---|---|
| CMPA (full word) | with bytes against with bytes | less |
| CMPA (0:3) | less | |
| CMPA (1:3) | greater |
The first two lines come out less since the register is negative. The third ignores the signs and gives greater. For rI1 and a memory word , CMP1 with the field gives equal, since the field of the register has the value and the sign is not in the field. A comparison of with 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 .
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 . 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 u. A register is tested by its value, so 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 u including the HLT, and ends with rA and rX . For a register jump, J2P at location 3000 with rI2 jumps and sets rJ to . With rA 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 . STJ with the default field stores the sign and the two bytes of rJ, (since ) 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 , STJ , ENTA , JMP , ENTX , HLT .
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 . 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 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 , and to the right it divides by and drops the remainder. The rotation keeps all bytes. With rotations a program can look at each byte in turn.
Example. Let rA and rX . Each line starts from these values.
| Instruction | rA | rX |
|---|---|---|
| SLA 2 | ||
| SRA 1 | ||
| SLAX 3 | ||
| SRAX 4 | ||
| SLC 4 | ||
| SRC 3 | ||
| SRC 10 | ||
| SLC 12 | ||
| SRA 7 |
Proof. The table is the definition applied to the ten bytes . For SLC 12, the rotation by is the rotation by , by the theorem below.
Theorem (the algebra of the shifts). Let be the ten bytes of rA and rX. Then SLC gives , SRC is the rotation to the right by , SRC is the inverse of SLC , and SRC equals SLC . A shift SLA , SRA with sets rA to zero bytes, and SLAX , SRAX with set both registers to zero bytes. A shift by of rA multiplies the magnitude by modulo to the left, and divides it by 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 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 : the rotation to the left by gives , the same ten bytes as the table.
Proof. A rotation by one place to the left sends to the position . Doing it times sends to , so the new sequence has at position . This depends only on . The rotation to the right by sends to , which is the inverse map, and it equals the rotation to the left by modulo . For SLA the new bytes of rA are , and they are all zero when ; the value of the new bytes is . For SRA the new bytes are , and the value is . The same holds for the ten bytes with in place of .
Definition (MOVE, TAOCP Section 1.3.1). The instruction MOVE with copies words, one at a time and in this order, from the addresses to the addresses , and at each copy rI1 is increased by . After the instruction rI1 has increased by . If nothing happens. The instruction takes 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 u per word and the move takes 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 . Each run below starts from these values. With rI1 and MOVE 1200(4), the locations 2000 to 2003 hold and rI1 , and the time is u. With rI1 and MOVE 1200(4), the locations 1200 to 1205 hold and rI1 . If the locations 1198 and 1199 hold , then with rI1 and MOVE 1200(3), the locations 1198 to 1202 hold afterwards: the words moved down by two places, and the locations 1201 and 1202 keep their old words and .
Proof. The results are the definition applied in order, one word at a time. The second run is the case , of the next theorem.
Theorem (the overlap of MOVE). Let rI1 with and let . Then after MOVE the word at the address is the original word at for . In particular for the words after all equal the original word at . If rI1 or rI1 , 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 , 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 be the original word at . The second run of the example above has , and , so the words at are , and the words at 1200 and 1201 do not change. With and , the three words after would all be .
Proof. Induction on . At the step the word at is read and the word at is written. For the read address has not been written, since all the writes so far are at the addresses or above, so the word read is the original . For the address was written at the step , with the value by the induction hypothesis, so the step copies to . If , every write at the step is at an address below and so below every address that is read at a later step, and no address read has been written. If , every write is at an address of at least and every read is below . In both cases each word read is an original word, so the block moved is the original block.
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 , and puts the number so formed, , in rA, with the sign of rA. If the number is or larger, the toggle is set on and rA holds the number modulo . CHAR puts in rA and rX the ten character codes of the decimal digits of , with leading zeros, each code being . The signs of rA and rX do not change. NOP does nothing and takes u. HLT stops the machine and takes u. NUM and CHAR take 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 at the code , and , 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 and rX . The bytes modulo are , so NUM gives and the sign stays : rA for , since . If all ten bytes are , the number is , which is not below , and rA gets with the toggle on. For rA , CHAR gives rA and rX , the digits .
Proof. The three values: , , , and the sum with is . And , which gives . In the CHAR example the digits of in ten places are and each code is .
Theorem (NUM and CHAR are inverse). For every allowed and each integer with , the pair CHAR, NUM applied to rA gives rA , the toggle is not set by it, and the sign of is kept. Conversely, if the ten bytes of rA and rX are the codes of ten digits and the number that they give is below , 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 makes so that the ten digits of CHAR are enough.
Example. For rA , CHAR gives the digits of the example above, and NUM gives rA , the number again, since . The simulator run agrees.
Proof. Since , the magnitude has at most ten decimal digits , and CHAR writes in the -th byte. A code is at most , so it is a byte. NUM takes each byte modulo , which is , since and . It forms , which is below , so there is no overflow, and the sign of rA is kept by both. For the converse, the number that NUM forms is and its ten decimal digits are the , so CHAR writes the codes again.
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 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 , , , , , so it is the word . The text 2024 with a leading space has the codes .
Proof. The letters J, K, L, M, N, O, P, Q, R have the codes in this order, so , and . The letter I is the ninth letter, with code . The letters S to Z have the codes , so . Digits have the code , so 2, 0, 2, 4 are .
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 starts the transfer of one block from the device into the words and OUT starts the transfer of one block from those words to the device. Both take 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 if the unit is busy, and JRED jumps if it is ready, both taking u. IOC is the control instruction: for a tape, rewinds, skips back blocks or to the start of the tape, and skips forward. For a disk or drum, should be and the unit is positioned by rX. For the line printer, goes to the top of the next page. For the paper tape, 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 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 to where is the size. For the printer, a block of words has 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 |
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 loads, stores, additions, one multiplication and one jump takes u.
Proof. The definition has nothing to prove. The sum in the example is .
Theorem (the time of a program). Let the instruction at the address run times in a run, and let it take u. The time of the run is . A program with an initial part of time that runs once and a loop body of time that runs times has the time . The program for the sum of words, given in the next part, takes 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 and the times are , so the sum is .
Proof. The time of a run is the sum of the times of the executed instructions. Group the executions by address: the address contributes once for each of its executions, which gives . The initial part contributes once and the body contributes in each of rounds, so the total is . For the program below, and .
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 words, a small example for this chapter). The program adds the numbers in the words at the addresses , as the values of their words, and leaves the sum in rA. The input is , set by the first instruction as in rI1. The output is rA, and rI1 ends at .
Node: none
Idea. The index register counts down from to , and the address walks over the words from the last to the first. The loop ends when the register becomes negative.
The program. For , the first instruction is ENT1 2.
| Location | Instruction | Time | Frequency |
|---|---|---|---|
| 3000 | ENT1 | 1 | 1 |
| 3001 | ENTA 0 | 1 | 1 |
| 3002 | ADD 1000,1 | 2 | |
| 3003 | DEC1 1 | 1 | |
| 3004 | J1NN 3002 | 1 |
The assembled words for are , , , and for (the first number has since ENT1 is the operation code with ).
A run. The words at 1000, 1001, 1002 hold . 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 | 3005 | |
| 11 | J1NN 3002 | 1 | 14 | 14 | 3005 |
Correctness. The invariant at the label 3002 is: rI1 with and rA is the sum of the words at the addresses for . At the first arrival, and the sum is empty, so rA holds. The body adds the word at , so rA is the sum for , and DEC1 makes the register , which restores the invariant for the new . The jump J1NN goes back when and stops when , so the loop stops after exactly runs, since the register decreases by and starts at . At the exit, , and rA is the sum over all with . For the run above, the sum is .
The time. The instruction ENT1 and ENTA each run once, for u. The three instructions ADD, DEC1 and J1NN run times each, for u. The time is u, and for it is u, as the table shows. The jump register ends with . The program does not depend on the byte size as long as the sum is below . A run with on the same data gives the same sum, , 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 . 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 isTAOCP.MIX.Half. The value of a word isTAOCP.MIX.Word.value. The bounds areTAOCP.MIX.Word.abs_value_ltandTAOCP.MIX.Half.mag_lt. - The state is
TAOCP.MIX.State, with 4000 cells (TAOCP.MIX.memSize) and the start stateTAOCP.MIX.Program.init. One step isTAOCP.MIX.State.step, andTAOCP.MIX.State.runruns a number of steps. The cases that the book leaves undefined are the constructors ofTAOCP.MIX.Fault, and the machine stops with one of them. - The fields are
TAOCP.MIX.Field, withTAOCP.MIX.Field.codeandTAOCP.MIX.Field.decode_code. The load and the store of a field areTAOCP.MIX.Word.loadandTAOCP.MIX.Word.store. - The instructions are in
TAOCP/MIX/Semantics.lean, with one function for each family, such asTAOCP.MIX.State.execLoadandTAOCP.MIX.State.execDiv. The operations on words are inTAOCP/MIX/Ops.lean. - The time of an instruction is
TAOCP.MIX.Instr.time. The time of a program is carried by the Hoare triplesTAOCP.MIX.TripleofTAOCP/MIX/Logic/Hoare.lean, andTAOCP.MIX.Triple.seqandTAOCP.MIX.Triple.loopcombine 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 . The bound 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 and are different words. The registers are read with
TAOCP.MIX.State.rAand 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 and 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 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.