Volume C1 · Basic concepts · TAOCP Section 1.3.2
The MIX assembly language
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.2 of the book. The nodes and the exercises of the section.
Contents
The MIX assembly language
Overview
Section 1.3.2 turns the machine of Section 1.3.1 into a language that a person can write. A MIXAL program is a list of lines. Each line has a location name, an operation and an address part, and the assembler turns the lines into the numeric words of memory. The language is small on purpose: it has symbols, expressions that are computed from left to right, a short way to write the address, index and field of an instruction, and five pseudo-operations that give orders to the assembler and are not instructions of the machine.
The section has two programs. The first finds the largest of numbers and is the program whose time is analyzed with the quantity of Section 1.2.10. The second finds the first 500 primes by trial division and prints them, and it shows features of the language, among them the literal constant and the pseudo-operations. In both programs the time is the sum over the lines of the time of the instruction times the number of runs of the line, so the work of an analysis is to find the number of runs of each line.
The chapter uses the machine of Section 1.3.1, the notation of Section 1.1 and the integers of Section 1.2.4. Its programs are not the listings of the book. They solve the same two problems by the same methods, and each count of the chapter was checked by a run of the listing for the byte size and also by hand.
The ideas
A line is a name, an operation and an address. The name field holds a symbol or is empty. The operation field holds the name of a MIX instruction or of a pseudo-operation. The address field holds the parts of the instruction, written as , and , and a remark may follow. The assembler reads the lines from the first to the last and keeps a location counter, which is the address of the next word to assemble.
A symbol has one value. A name in the name field gets a value, which is the location counter for an instruction line and the given number for a line of the type EQU. The name may then be used anywhere after the line, and a name that is used in an address part before its line is a future reference. The language allows a future reference only where the assembler can fill the value in later, and that is the reason for the rules of the section on future references.
Expressions are simple on purpose. An expression is a chain of numbers, symbols and the sign , joined by , , , , the double slash and the colon, and the chain is computed from left to right, with no precedence. The colon builds a field code, so that means . A W-value is a list of expressions with fields and gives a full word.
Literals and local symbols save names. A literal constant is a W-value between equal signs. The assembler stores it in a word after the program and uses the address of that word. A local symbol is a digit with the letter H, F or B. It lets a program have many short labels with the same name, so the programmer does not need to invent a name for each jump.
A program is analyzed by its profile. The profile is the list of the numbers of runs of the lines. The time of the program is the sum of the products of the profile with the times of the instructions. The profile of a loop follows from the law of conservation: the number of times that control enters a line is the number of times that it leaves the lines before it. Section 1.3.2 uses this law in both analyses.
Definitions, results and proofs
Definition (a MIXAL program and its lines, TAOCP Section 1.3.2). A MIXAL program is a finite list of lines that ends with a line of the operation END. A line is either a comment, which has the sign in its first column, or it has the fields LOC, OP and ADDRESS, followed by remarks. The field LOC is empty or has a symbol. The field OP has the name of a MIX operation or one of the pseudo-operations EQU, ORIG, CON, ALF, END. The field ADDRESS has the address part of the line. In the card form, LOC is in the columns 1 to 10, OP is in the columns 12 to 15, and ADDRESS starts in the column 17 and ends at the first blank, and the text after it is a remark. In the free form of a terminal, the fields are separated by blanks. The assembler goes through the lines in order, keeps a location counter that starts at , and puts the word of each instruction line, CON line and ALF line at the location counter, which it then increases by .
Node: 1.3.2-def-mixal · Lean: TAOCP.MIX.Asm.Source, TAOCP.MIX.Asm.Line, TAOCP.MIX.Asm.assemble
Explanation. The result of assembly is a set of memory words and a start address. The lines EQU, ORIG and END and the comments make no word. The remarks are for the reader and the assembler ignores them. The assembler is the only reader that needs the exact rules, and the rules of the next blocks say how each field is read.
Example. The program below sums the five words at the addresses , stores the sum, subtracts a constant, and halts. The address is named T, and the number of words is named N. It uses a local symbol, a literal constant and a word of text, which are explained in the next blocks.
N EQU 5
T EQU 1000
ORIG 3000
BEG ENT1 N
ENTA 0
1H ADD T,1
DEC1 1
J1P 1B
STA RES
SUB =10=
JMP DONE
RES CON 0
MSG ALF HELLO
DONE HLT
END BEG
The assembler gives the words in the table, for the byte size . A word is shown as its sign and the five bytes, and for an instruction the bytes are the two bytes of , then , and .
| Location | Line | Word |
|---|---|---|
| 3000 | ENT1 N | |
| 3001 | ENTA 0 | |
| 3002 | ADD T,1 | |
| 3003 | DEC1 1 | |
| 3004 | J1P 1B | |
| 3005 | STA RES | |
| 3006 | SUB =10= | |
| 3007 | JMP DONE | |
| 3008 | RES CON 0 | |
| 3009 | MSG ALF HELLO | |
| 3010 | DONE HLT | |
| 3011 | the literal |
The start address is . Let the words at be . The run takes u and ends with and the sum in RES. The instructions at , and run times each, and the other instructions run once, which gives .
Proof. The definition has nothing to prove, and the example is checked word by word. The line ORIG 3000 puts the location counter at , and ENT1 N is the first word. Its address part is , which is the bytes , its index is , and the pair is , since ENT1 is the operation code with the field . The line ADD T,1 has , the index , the field and the code . The line J1P 1B jumps to the nearest line above that has the name 1H, which is , with the field of J1P and the code . The line STA RES has the address , which is the place of the CON line, and the line JMP DONE has . The word of the literal is at , because the lines that follow the program end at , so the first free location is , and the line SUB has . The line ALF HELLO has the codes , , , , of the table of Section 1.3.1. In the run, ENT1 sets , and the loop adds the words at , which are , so rA takes the values . The instruction J1P falls through when is . Then STA puts in RES, SUB gives , and JMP goes to the line HLT.
Definition (symbols and numbers, TAOCP Section 1.3.2). A number is written with one to ten decimal digits. A symbol is written with one to ten characters, each a letter or a digit, and at least one of them is a letter. A symbol has exactly one value, a full word of MIX. The value comes from the line where the symbol is in the field LOC: it is the location counter if the line is an instruction, CON or ALF line, and it is the value of the address part if the line is an EQU line. A symbol that is never in the field LOC gets a word with the content after the program, and its value is the address of that word. A symbol is a defined symbol after the line of its definition, and it is a future reference before that line.
Node: 1.3.2-def-symbol · Lean: TAOCP.MIX.Asm.Syms, TAOCP.MIX.Asm.Atom (partial)
Explanation. The condition that a symbol has a letter makes it different from every number, so the assembler can tell a symbol from a number by its characters. A line EQU is a note to the assembler. It does not set a variable of the program. If a program must have a word with the value at run time, the word is made with CON or by instructions. The rule of one value means that a symbol can be in the field LOC of at most one line. The forms nH, nF and nB are the local symbols and belong to a separate block.
Example. The strings SUM, LOOP2 and 3RD are symbols, and 00072 is a number with the value . The string 12345 is a number, since it has no letter, and the string ELEVENCHARS has eleven letters and is too long, so it is not a symbol. In the program of the first example, N is , T is , BEG is , RES is , MSG is and DONE is .
Proof. The definition has nothing to prove. For the example, the symbols N and T have the values of their EQU lines. The location counter is after ORIG 3000. It increases by one at each of the lines ENT1, ENTA, ADD, DEC1, J1P, STA, SUB and JMP, so after eight lines it is and the CON line RES gets . The ALF line then has , and the line HLT has , which is the value of DONE.
Definition (expressions, TAOCP Section 1.3.2). An atomic expression is a number, a defined symbol, or the sign , which stands for the value of the location counter at the line. An expression is an atomic expression, perhaps with a sign or before it, followed by any number of pairs of a binary operator and an atomic expression. The operators are , , , , the double slash and the colon. The value is computed from left to right with no precedence. The operator gives the integer part of the quotient of nonnegative operands. The double slash gives for the operands and with , where is the byte size. The colon gives .
Node: 1.3.2-def-expression · Lean: TAOCP.MIX.Asm.Expr, TAOCP.MIX.Asm.Ctx.expr, TAOCP.MIX.Asm.Ctx.atom, TAOCP.MIX.Asm.Ctx.apply
Explanation. A chain such as is not but , because is done first. A programmer who needs the other order puts the factors first: . The double slash gives the fraction as a word: the point is at the left of the word, so is the word with the value one fourth of . The colon makes a field code from two numbers, which is the way that an F-part is written.
Example. Let the location counter be at the line, with and .
| Expression | Value | Steps |
|---|---|---|
| 20 | , then | |
| 15 | , then | |
| 138 | , then | |
| , then | ||
| 2010 | , then | |
| 3012 | ||
| 24005 | ||
| , for |
Proof. Each value follows from the definition. For : has the integer part , and . For : , then the colon gives . For : , and . For : , one fourth of it is , and , so the word is . The value of the double slash depends on , so a program that uses it must be right for each of the range of Section 1.3.1.
Definition (the instruction line, TAOCP Section 1.3.2). The address field of an instruction line has the form . The part is empty, which means , or an expression, or a future reference, or a literal constant. The part is empty, which means , or it is a comma followed by an expression. The part is empty, which means the normal field of the operation, or it is an expression in parentheses. The assembler gives the word , where is the value of written as two bytes and a sign, and are the values of the other parts, and is the operation code of the name in the field OP.
Node: 1.3.2-def-instruction-line · Lean: TAOCP.MIX.Asm.Item, TAOCP.MIX.Asm.Ctx.instr
Explanation. The normal field of an operation is the field of its table in Section 1.3.1: it is for the loads, the stores and the arithmetic, for STJ, ENT and HLT, and for the jumps with no condition. The comma and the parentheses make the address part easy to read by eye: the part before the comma is the address, the part after it is the index register, and the part in the parentheses is the field. A value that is not in the range of its place, for example an index above , is an error of the program.
Example. The table has some lines and their words for .
| Line | Word | Remark |
|---|---|---|
| LDA 2000,3(1:5) | , | |
| LDX 1002,4(2:3) | ||
| LDA -5 | the sign is that of the address | |
| JMP 3002 | normal field | |
| HLT | empty address, normal field | |
| NOP | code , field | |
| ENTA 0,1 | index , normal field |
Proof. The words follow from the definition and the codes of Section 1.3.1. In LDA 2000,3(1:5), the address is the bytes , the index is , the field is and the code of LDA is . In LDX 1002,4(2:3), , and the code of LDX is . In LDA -5, the value of the address part is , so the sign is and the bytes are , with . For JMP the normal field is and the code is . For HLT the code is and the field is . For ENTA the code is and the field is .
Definition (W-values, TAOCP Section 1.3.2). A W-value is a list , in which each is an expression and each is a field in parentheses, with the default if it is absent. Its value is the word that results when the values of the parts are stored, from the first to the last, into a word that starts as , in the way of the instruction STA: for the field , the sign and the low bytes of go into the positions of the field, and the other positions are not changed.
Node: 1.3.2-def-wvalue · Lean: TAOCP.MIX.Asm.Ctx.wvalue
Explanation. The W-value is the way that a program writes a word with the sign and the bytes set separately, for example a word that holds an address in the two left bytes and a small number in the right byte. The order matters: a later part may change what an earlier part stored, and a part with the default field replaces the whole word.
Example. For and , :
| W-value | Word |
|---|---|
Proof. In , the first part stores only the sign of , so the word becomes . The second part stores in the bytes and , and the field does not hold the sign, so the sign stays and the word is . In the second line the order is reversed: the part gives , and then the part with the default field replaces the whole word, sign included, so the result is . The third line stores in and the byte at the position . In the fourth line the byte goes to the position and the number takes the positions and .
Definition (the location counter, ORIG and EQU, TAOCP Section 1.3.2). The location counter is a nonnegative number that the assembler keeps while it reads the lines, and it must fit in two bytes. The sign in an expression has the value of the counter at the line. A line ORIG with the W-value in its address sets the counter to and makes no word. A line EQU with the W-value in its address gives the symbol of its field LOC the value , and it changes neither the counter nor memory. The address of an ORIG line and of an EQU line may use only defined symbols.
Node: 1.3.2-def-location · Lean: TAOCP.MIX.Asm.pass1, TAOCP.MIX.Asm.pass1Step
Explanation. The line ORIG with an expression of the form keeps words free at that place, which is the usual way to reserve an area. The line ORIG with a new number moves the program to another place, for example to put a table at a fixed address. A symbol of an EQU line is a name for a number, and it may be a constant, an address or a size. The text of the program may use the name at any later line, and one change of the EQU line changes the program in all places.
Example. The lines below start at the address . The assembler gives the words of the table for . The line ORIG with comes after the line at , where the counter is , so it sets the counter to .
ORIG 100
2H J1Z 2F
DEC1 1
JMP 2B
2H ENT1 0
ORIG *+3
V CON -7
W CON V+1
END 100
| Location | Line | Word |
|---|---|---|
| 100 | 2H J1Z 2F | |
| 101 | DEC1 1 | |
| 102 | JMP 2B | |
| 103 | 2H ENT1 0 | |
| 104 to 106 | not assembled | the words keep their old content |
| 107 | V CON | |
| 108 | W CON V+1 |
The symbol V has the value and W has the value . The word at holds , which is its own address.
Proof. The counter is after the first ORIG. The four instruction lines take the locations to and leave the counter at . The expression has the value , so the line V gets the location and W gets . The symbol V is defined at the line of W, so is allowed, and its value is , with the bytes . The word of V has the sign and the value . No word is made for the lines ORIG and END.
Definition (CON and ALF, TAOCP Section 1.3.2). A line CON with the W-value in its address makes the word at the location counter. A line ALF makes a word of five characters. Its address field holds the five characters, which start after one blank that follows OP, or after two blanks if the first character is a blank. The word has the character codes of Section 1.3.1 in the five bytes and the sign . In both lines, the symbol of the field LOC has the value of the counter before the line, and the counter increases by .
Node: 1.3.2-def-con-alf · Lean: TAOCP.MIX.Asm.alfWord, TAOCP.MIX.Asm.Opnd
Explanation. A line CON puts a number in the program as a word of data: a counter that starts at a given value, a constant for a comparison, an address. A line ALF puts a word of text, for example a title that the program prints. Both lines produce a word of data and are not instructions, so control must not run into them.
Example. In the first example of this chapter, the line RES CON 0 makes the word at , and the line MSG ALF HELLO makes at . The line CON with makes .
Proof. The codes are those of Section 1.3.1: the letters A to I are to , so and , and the letters J to R are to , so and . The W-value puts in the first byte and in the last two bytes, and the last two bytes are as a field, which leaves .
Definition (literal constants, TAOCP Section 1.3.2). A literal constant is a W-value of fewer than ten characters, between two equal signs, that is used as the -part of an instruction. For each literal constant, the assembler makes a new symbol and adds a line CON with the W-value after the lines of the program and before END. The address part of the instruction is the value of the new symbol, which is the location of that word. Each occurrence of a literal constant makes its own word. The language does not fix the order of these words, and a program must not depend on it.
Node: 1.3.2-def-literal · Lean: TAOCP.MIX.Asm.Opnd
Explanation. A literal constant saves the programmer from inventing a name and a CON line for a number that the program uses once. The instruction SUB =10= subtracts the number , since the instruction takes the word at the address of the new symbol, and that word holds . The value of a literal is a constant of the program, so the program must not store into it. The literal is a future reference, so it follows the rules of the block on future references.
Example. In the first example of this chapter, the line SUB =10= has the address and the word at is . In the program of primes below, the lines CMP1 =PL=, CMP5 =20= and CMP1 =PL= again make three words after the program. The assembler of this chapter puts them in the order of the lines, at , and , with the values , and .
Proof. In the first example the last line of the program is at , so the new word has the next location, . In the second program the last instruction, HLT, is at , and the three words are placed in the order of the lines that contain them, so they take , , . The value of PL is , and the word of the literal is .
Definition (local symbols, TAOCP Section 1.3.2). A local symbol is a digit from to followed by H, F or B. A line with H in the field LOC defines a new symbol that has the value of the location counter at that line. Many lines may have the same H. In the address part, B is the symbol defined by the nearest line H above the line that uses it, and F is the symbol defined by the nearest line H below it. The form F is a future reference. The forms B and F are not allowed in the field LOC, and H is not allowed in an address part. The form B must have a line H above it, and the form F must have one below it. The form F is allowed only where a future reference is allowed.
Node: 1.3.2-def-local · Lean: TAOCP.MIX.Asm.localKind, TAOCP.MIX.Asm.Syms.lookup, TAOCP.MIX.Asm.Syms.define
Explanation. Most labels of a program are used in one place a few lines away, for a short jump or a loop. A local symbol lets the programmer reuse the same short name in many places and so avoid inventing a name for each one. The letters B and F stand for backward and forward, and they say which of the lines H is meant. A line that defines H and that also uses F, as the first line of the fragment below does, refers to the next line H and not to itself.
Example. In the fragment of the example of ORIG, the line at is 2H and has the address 2F. The nearest 2H below it is the line at , so the word at has the address . The line JMP 2B at has the nearest 2H above it at , so its address is . In the first example the line J1P 1B at has the nearest 1H at , which is the address .
Proof. The proof is by reading the words of the two tables above. For 2F at : the lines below with the field 2H are at only, so the value is . For 2B at : the lines above with 2H are at only, since the line at is below, so the value is . For 1B at : the only line 1H is at .
Definition (future references, TAOCP Section 1.3.2). A symbol or a literal constant that is used before the line of its definition is a future reference. A future reference may be only the whole -part of an instruction. It must not be an operand of an operator in an expression, it must not be in the -part or the -part, and it must not be in the address of an EQU, ORIG or CON line. The address of an ALF line is text and has no symbols.
Node: 1.3.2-def-future · Lean: TAOCP.MIX.Asm.pass1, TAOCP.MIX.Asm.slotWord (partial)
Explanation. The rule is a limit that the assembler needs. When the assembler reads the lines from the first to the last, a future reference has no value yet. If the symbol were an operand, the value of the expression would have to wait, and if it were in an ORIG line, the counter would not be known and the values of all the next symbols would have to wait. The rule makes the next theorem true.
Example. The line JMP DONE, which comes before the line DONE, is correct. The lines ENT1 DONE-2 and CON DONE are not correct if DONE is not yet defined, because the symbol is an operand in the first one and the address of a CON line in the second one. The line ORIG DONE before the definition of DONE is not correct. The line ORIG DONE after the definition of DONE is correct.
Proof. The definition has nothing to prove. The examples follow from the rule: a lone future symbol in the -part of JMP is allowed, and in an expression, a CON address or an ORIG address it is not.
Theorem (one pass is enough, TAOCP Section 1.3.2). Let a program follow the rules for symbols, expressions and future references. Then the assembler can make the words of the program with one reading of the lines, if it keeps a table of the symbols with their values and a list of the words that wait for the value of a symbol.
Node: 1.3.2-thm-one-pass · Lean: not yet stated
Explanation. The theorem states the reason for the restriction. The assembler may be a small program with a simple loop. It does not need to read the lines twice. A word that waits has all its fields except the two bytes and the sign of the address, which it takes later.
Example. The table shows the first example of this chapter in the order of the reading. A row shows the state after that line. The symbols N and T are in the table from the two lines EQU at the start.
| Line read | Counter after | Symbol table gets | Words that wait |
|---|---|---|---|
| ORIG 3000 | 3000 | ||
| BEG ENT1 N | 3001 | BEG = 3000 | |
| ENTA 0 | 3002 | ||
| 1H ADD T,1 | 3003 | 1H = 3002 | |
| DEC1 1 | 3004 | ||
| J1P 1B | 3005 | none: 1B is known | |
| STA RES | 3006 | 3005 waits for RES | |
| SUB =10= | 3007 | 3006 waits for the literal | |
| JMP DONE | 3008 | 3007 waits for DONE | |
| RES CON 0 | 3009 | RES = 3008 | 3005 is done |
| MSG ALF HELLO | 3010 | MSG = 3009 | |
| DONE HLT | 3011 | DONE = 3010 | 3007 is done |
| END BEG | the literal gets the location 3011 | 3006 is done |
Proof. Let the lines be read in order. At each line, the counter is known: it starts at , an ORIG line sets it to an expression that has only defined symbols, and every other line that makes a word adds . For the same reason the value of each EQU line and of each CON line is known when the line is read, so each symbol that is in a field LOC gets its value at once. For an instruction line, the fields , and are known, since and are expressions of defined symbols, and the -part is either a value that is known, or it is a future reference by itself, so its value is the value of one symbol and no computation waits. The assembler writes the word without the -part and puts it on the list of the symbol. When the line of the symbol is read, the assembler writes the value in the words of the list, and it empties the list. At the line END, the assembler makes the words for the literals and for the symbols that were never defined, and so resolves the rest of the lists. Each line is read once.
Definition (the line END, TAOCP Section 1.3.2). The line END has a W-value in the address. The assembler first adds, before the end of the program, a word for each literal constant and a word with the content for each symbol that is used and never defined, in an order that the language does not fix. The symbol of the field LOC of the END line, if there is one, has the value of the first location after these words. The program starts at the location in the field of the W-value of the END line.
Node: 1.3.2-def-end · Lean: TAOCP.MIX.Asm.assemble, TAOCP.MIX.Asm.Source.program (partial)
Explanation. The line END does two things. It says where the loader must start the program, and it tells the assembler that no more lines follow, so the assembler may make the words that it owes. The start is often the first instruction, but it need not be: the first example has its words of data after the code, and the start is the label BEG.
Example. In the first example, END BEG has the W-value , whose field is , so the program starts at . The assembler adds one word for the literal at . In the program of primes, END BEGIN starts at and the three literals take to .
Proof. The definition has nothing to prove. In the first example the lines make the words to and the literal makes , so the first location after the inserted word is .
Algorithms and programs
This part has the two programs of the section. The listings are written for this handbook, and each frequency in them comes from the law of conservation and was checked by running the program.
Program (the largest of n words, TAOCP Section 1.3.2, Program 1.3.2M). The program is a subroutine. The caller sets with , puts the numbers at the addresses , and calls the subroutine with the instruction JMP MAX. The subroutine returns to the instruction after the call. It leaves the largest value in rA, and it leaves in the largest index with equal to that value. It destroys , which is at the end.
Node: Program 1.3.2M · Lean: TAOCP.S1_3_2.programM (partial)
Idea. The subroutine looks at the numbers from the last to the first. It keeps in rA the largest value seen so far and in the place where it is. The register is the index of the number that is compared next, and it counts down to . The return is done by the first instruction, which writes the address of the caller into the last instruction of the subroutine, so that the last instruction is a jump to the caller.
The program. The listing starts with the lines X EQU 1000 and ORIG 3000. The table has the listing with the times, the frequency of each line (the number of runs) and the words for . The number is the number of times that the line at runs.
| Location | Line | Time | Frequency | Word |
|---|---|---|---|---|
| 3000 | MAX STJ EX | 2 | 1 | |
| 3001 | ENT2 0,1 | 1 | 1 | |
| 3002 | LDA X,1 | 2 | 1 | |
| 3003 | LOOP DEC1 1 | 1 | ||
| 3004 | J1Z EX | 1 | ||
| 3005 | CMPA X,1 | 2 | ||
| 3006 | JGE LOOP | 1 | ||
| 3007 | ENT2 0,1 | 1 | ||
| 3008 | LDA X,1 | 2 | ||
| 3009 | JMP LOOP | 1 | ||
| 3010 | EX JMP * | 1 | 1 | before the call |
The first instruction STJ stores rJ in the address of the line EX, and the symbol EX is in the field LOC of the last line. The line at is a jump to itself in the listing, and the first instruction replaces its address by the return address.
A run. Let and , called from the address , so that rJ is at the entry. The table shows the state after each step. The column rI1 holds , and the column rA holds the largest value seen. The column ci shows the comparison indicator, which is for less, for equal and for greater.
| Step | Location | Instruction | Time added | Total | rA | rI1 | rI2 | ci |
|---|---|---|---|---|---|---|---|---|
| 1 | 3000 | STJ EX | 2 | 2 | 0 | 4 | 0 | 0 |
| 2 | 3001 | ENT2 0,1 | 1 | 3 | 0 | 4 | 4 | 0 |
| 3 | 3002 | LDA X,1 | 2 | 5 | 5 | 4 | 4 | 0 |
| 4 | 3003 | DEC1 1 | 1 | 6 | 5 | 3 | 4 | 0 |
| 5 | 3004 | J1Z EX | 1 | 7 | 5 | 3 | 4 | 0 |
| 6 | 3005 | CMPA X,1 | 2 | 9 | 5 | 3 | 4 | |
| 7 | 3006 | JGE LOOP | 1 | 10 | 5 | 3 | 4 | |
| 8 | 3007 | ENT2 0,1 | 1 | 11 | 5 | 3 | 3 | |
| 9 | 3008 | LDA X,1 | 2 | 13 | 9 | 3 | 3 | |
| 10 | 3009 | JMP LOOP | 1 | 14 | 9 | 3 | 3 | |
| 11 | 3003 | DEC1 1 | 1 | 15 | 9 | 2 | 3 | |
| 12 | 3004 | J1Z EX | 1 | 16 | 9 | 2 | 3 | |
| 13 | 3005 | CMPA X,1 | 2 | 18 | 9 | 2 | 3 | |
| 14 | 3006 | JGE LOOP | 1 | 19 | 9 | 2 | 3 | |
| 15 | 3003 | DEC1 1 | 1 | 20 | 9 | 1 | 3 | |
| 16 | 3004 | J1Z EX | 1 | 21 | 9 | 1 | 3 | |
| 17 | 3005 | CMPA X,1 | 2 | 23 | 9 | 1 | 3 | |
| 18 | 3006 | JGE LOOP | 1 | 24 | 9 | 1 | 3 | |
| 19 | 3003 | DEC1 1 | 1 | 25 | 9 | 0 | 3 | |
| 20 | 3004 | J1Z EX | 1 | 26 | 9 | 0 | 3 | |
| 21 | 3010 | JMP 102 | 1 | 27 | 9 | 0 | 3 |
The answer is rA and , which is the place of the number in . The time is u. The count is , since only the line at ran, once, at the step 8. The formula of the analysis gives . The last step is the jump to the caller.
Correctness. Let be the value of when control arrives at the label LOOP, before DEC1. The claim is that for at that moment, rA is the largest of , and is the largest index with and equal to rA. At the first arrival , the lines at and have set and , and the claim holds. Suppose that it holds for . The line DEC1 makes . If , the line J1Z jumps to EX, and the claim for says that rA is the largest of all numbers and is the largest index of it, which is the answer. Otherwise the line CMPA compares rA with . If , JGE goes back, rA and do not change, and the claim holds for : the largest value is still rA, and the largest index of it is still , since a tie with has a smaller index. If , the lines at and set and , and the claim holds for since is now the strict largest and has the only index. The jump JMP LOOP goes back. The register decreases at each pass, so the loop stops after arrivals at LOOP. The first instruction STJ stores the return address in the line EX, so the last jump goes back to the caller.
Theorem (the running time of Program 1.3.2M, TAOCP Section 1.3.2). For , the subroutine, from the first instruction to the return jump, takes u, where is the number of values of with for which is larger than each of . The time does not include the call JMP MAX, which takes u. The time is at least and at most .
Node: 1.3.2-anal-progM · Lean: TAOCP.S1_3_2.programM_time (partial)
Explanation. The count is the number of times that the largest value changes in the scan, and it is the quantity of Section 1.2.10 for the numbers read from down to . The terms and do not depend on the data. The term is the cost of the changes, with u for the three lines of the update and the jump that goes back. The listing of TAOCP for the same task orders its lines in another way and takes u. The count is the same for both listings, and only the constants differ.
Example. For the data has and takes u, the data has and takes u, and the data has and takes u. The case takes u. These times are those of a run of the program.
Proof. The line DEC1 runs once for each value of from down to , so it runs times, and the line J1Z runs times as well. The line J1Z jumps only the last time, so the lines CMPA and JGE run times. The lines of the update at , and run each time that JGE does not jump, and by definition this is times: JGE does not jump for the value exactly when is larger than the largest of the values to its right, which is the condition of . The line JGE jumps times, and the line JMP LOOP runs times, so control arrives at LOOP times, which is in agreement with the runs of DEC1. The times of the lines are for the first three, for DEC1 and J1Z, for CMPA and JGE, for the update, and for the last line. The sum is . The value ranges from to . It is if is at least all the others and if , so the time ranges from to . For the example of four numbers, and the time is .
Theorem (the mean and variance of the time of Program 1.3.2M, TAOCP Sections 1.2.10 and 1.3.2). Let the numbers be distinct and let each of the orders be equally likely. Then has the mean and the variance , and so the time of the subroutine has the mean and the variance , where and .
Node: 1.3.2-thm-progM-mean · Lean: TAOCP.S1_3_2.programM_mean_time (partial)
Explanation. The statement uses the result of Section 1.2.10 for the number of changes of the largest value, and it applies the rule that a constant factor multiplies the variance by its square. The mean grows like , so for large the time is plus a small term. The proof below uses the ranks of the numbers, which is a different route from the generating function of Section 1.2.10.
Example. For , the orders have with the counts , and the mean is , which equals since . The mean time is , which is the value of the formula , and a run of the program on all orders gives the same value. The variance of is , so the variance of the time is , which is the value that the run gives.
Proof. For , let if is larger than each of and otherwise, so . Among the numbers , each is equally likely to be the largest, so with the probability . The variables are independent. To see this, let be the rank of among , a number from to . An order determines the list , and from the list the order is rebuilt by placing the numbers from the right to the left. So the map is a bijection from the orders to the lists with , whose number is . Hence every list is equally likely, the are independent, and each is uniform. Since depends on only, the are independent. With , which runs from to , the mean is . The variance of a sum of independent variables is the sum of the variances, and has the variance , so the variance of is . The time is , so its mean is , and its variance is times that of .
Algorithm (the first 500 primes by trial division, TAOCP Section 1.3.2, Algorithm 1.3.2P). The input is the number of primes wanted, and the section uses . The output is the table of the first primes in increasing order. The table is built in place, and each odd candidate is tested only by the odd primes that are already in it.
Node: Algorithm 1.3.2P · Lean: not yet stated
The steps.
FirstPrimes(L): -- L >= 2
P[1] := 2; P[2] := 3; j := 2; n := 3
while j < L:
n := n + 2 -- the next odd candidate
if IsPrime(n, P):
j := j + 1; P[j] := n
return P[1..L]
IsPrime(n, P): -- n >= 5 odd, P holds the primes < n
k := 2
loop:
q := n div P[k]; r := n mod P[k]
if r = 0: return false -- P[k] divides n
if q <= P[k]: return true -- then n < (P[k] + 1)^2
k := k + 1
Explanation. The algorithm tests each odd number for division by the odd primes already found, in order. It answers "composite" when a prime divides . It answers "prime" when the quotient is not larger than the divisor , since then , and a composite would have a prime factor not larger than . It never divides by an even number, and it never needs a square root.
Example. The table starts as . For , the divisor gives and , so and is prime. For , and , so is not prime. For , the divisor gives and , so becomes , and the divisor gives , so is prime. For the last entry is .
Proof. The proof has four parts. Let be the number of primes .
(1) The invariant. At each test of the condition , the number is odd with , the table holds the primes in increasing order, and . This holds at the first test, with and the table . Let it hold for , with . The number is even and larger than , so it is not prime. Hence the primes are those , with added if is prime. By parts (2) and (3) below, IsPrime returns true if and only if is prime, and then the loop body appends it. So the invariant holds at the next test.
(2) A zero remainder. Let be odd, with all primes below in the table. If for the divisor , then divides and , since is in the table and is not. So is composite, and the answer false is right.
(3) The test . Suppose that IsPrime returns true at the divisor . No earlier divisor divided , the divisor does not divide , and does not divide the odd number . So no prime divides . Since and , the number satisfies . Suppose that is composite. Its smallest prime factor satisfies , and since no prime divides , so and . This contradicts , so is prime. Now let be composite, with the smallest prime factor , which is odd and less than , so is in the table. The loop cannot return true at a divisor , because that would prove prime. It cannot return false at such a divisor either, since does not divide . So it reaches , finds and returns false. Hence IsPrime returns true if and only if is prime.
(4) The loop stops and never goes past the table. For a composite , the loop ends at by part (3). For a prime , Bertrand's postulate with gives a prime with . This is in the table, it is odd, and , so and . So the loop stops at at the latest. Since there are infinitely many primes, reaches , and by the invariant the table then holds the first primes.
Program (the table of primes in MIX, TAOCP Section 1.3.2, Program 1.3.2P). The program builds the table in the words , so is at , and it prints the table on the line printer, unit , in lines of numbers each. Each number has four digits, and a blank follows it. A printed line is the words . The program writes only the bytes to of the words , so it requires that byte of these words and all of the words be when it starts, which is true after the memory is cleared.
Node: Program 1.3.2P · Lean: not yet stated
Idea. The register is , is and is . The division is done on the pair rA, rX, with and , which gives the quotient in rA and the remainder in rX. The test is a comparison of the quotient with the divisor. The loop of IsPrime is rotated: its first line increases , so ENT3 sets and the first division uses . A prime candidate falls through the last test of the loop into the lines that store it, and a composite one jumps back to the next candidate. For the output, the program turns each number into ten characters with CHAR, stores the last four digits in the first four bytes of one word of the buffer, and prints a line of words.
The program. The symbols are , , and . The table has the listing, with the time of each line, the frequency of each line in a run to the table of primes, and the words for . The numbers , and are defined below.
| Location | Line | Time | Frequency | Word |
|---|---|---|---|---|
| 3000 | BEGIN ENTA 2 | 1 | 1 | |
| 3001 | STA TAB+1 | 2 | 1 | |
| 3002 | ENT1 1 | 1 | 1 | |
| 3003 | ENT2 3 | 1 | 1 | |
| 3004 | JMP FOUND | 1 | 1 | |
| 3005 | NEXT INC2 2 | 1 | ||
| 3006 | ENT3 1 | 1 | ||
| 3007 | TRY INC3 1 | 1 | ||
| 3008 | ENTA 0 | 1 | ||
| 3009 | ENTX 0,2 | 1 | ||
| 3010 | DIV TAB,3 | 12 | ||
| 3011 | JXZ NEXT | 1 | ||
| 3012 | CMPA TAB,3 | 2 | ||
| 3013 | JG TRY | 1 | ||
| 3014 | FOUND INC1 1 | 1 | ||
| 3015 | ST2 TAB,1 | 2 | ||
| 3016 | CMP1 =PL= | 2 | ||
| 3017 | JL NEXT | 1 | ||
| 3018 | PRINT ENT1 1 | 1 | 1 | |
| 3019 | LINE JBUS *(PRN) | 1 | 25 | |
| 3020 | ENT5 0 | 1 | 25 | |
| 3021 | COL LDA TAB,1 | 2 | 500 | |
| 3022 | CHAR | 10 | 500 | |
| 3023 | STX BUF,5(1:4) | 2 | 500 | |
| 3024 | INC1 1 | 1 | 500 | |
| 3025 | INC5 1 | 1 | 500 | |
| 3026 | CMP5 =20= | 2 | 500 | |
| 3027 | JL COL | 1 | 500 | |
| 3028 | OUT BUF(PRN) | 1 | 25 | |
| 3029 | CMP1 =PL= | 2 | 25 | |
| 3030 | JLE LINE | 1 | 25 | |
| 3031 | HLT | 1 | 1 | |
| 3032 | the literal | |||
| 3033 | the literal | |||
| 3034 | the literal |
The frequencies of the lines of the printing part are those for . The four lines at FOUND record a prime, and they run once for each prime except . The line JL NEXT jumps after each of these runs except the last, so times, and after the last one control falls into PRINT.
A run. A small table shows the idea. Let , so the program stops after the primes . The table lists each execution of DIV: the register is the number of primes in the table, and the results are the quotient in rA and the remainder in rX. The last column gives the next step.
| Divisor | rA | rX | Next step | |||
|---|---|---|---|---|---|---|
| 5 | 2 | 2 | 3 | 1 | 2 | prime (JG does not jump) |
| 7 | 3 | 2 | 3 | 2 | 1 | prime |
| 9 | 4 | 2 | 3 | 3 | 0 | composite (JXZ jumps) |
| 11 | 4 | 2 | 3 | 3 | 2 | prime |
| 13 | 5 | 2 | 3 | 4 | 1 | divisor 5 (JG jumps) |
| 13 | 5 | 3 | 5 | 2 | 3 | prime |
| 15 | 6 | 2 | 3 | 5 | 0 | composite |
| 17 | 6 | 2 | 3 | 5 | 2 | divisor 5 |
| 17 | 6 | 3 | 5 | 3 | 2 | prime |
| 19 | 7 | 2 | 3 | 6 | 1 | divisor 5 |
| 19 | 7 | 3 | 5 | 3 | 4 | prime |
The values are (the odd numbers ), (the lines of the table) and (the numbers and ). The frequencies of the lines are for the five lines to , for NEXT and ENT3, for the five lines at TRY, for CMPA and JG, and for the four lines of FOUND. The time up to the fall into PRINT is u. The table at the end is .
Correctness. The program follows Algorithm 1.3.2P. The first four lines put in , in and in , and JMP FOUND then stores as with , which completes the first line of FirstPrimes. The four lines at FOUND add one to , store at and test : JL NEXT goes on to the next candidate, and otherwise control falls into PRINT. The line NEXT adds to , and ENT3 sets . The lines at TRY are the loop of IsPrime. INC3 increases , so the first divisor is . With and , the division by cannot overflow since , and it gives in rA and in rX. The line JXZ goes to NEXT if , which is the answer false. The lines CMPA and JG compare with the divisor. If is larger, JG goes back to TRY for the next divisor, and otherwise control falls into FOUND, which is the answer true. For the print, the register goes from to , and in each line the register counts columns. For each column, the line LDA brings into rA, CHAR puts its ten decimal digits in rA and rX with the digit as the code , and STX with the field puts the last four bytes of rX, which are the last four digits, in the bytes to of the word . The byte is a blank by the requirement of the program. All the primes are below , so four digits are enough, and the largest is . After columns, OUT sends the words, and the four words that the program does not fill give blanks. The line JBUS at LINE waits until the printer has finished the last line, so the buffer is not changed while it is printed. After a line, CMP1 compares with , and JLE goes back if , so the program prints lines. The first line is 0002 0003 0005 0007 0011 0013 0017 0019 0023 0029 0031 0037 0041 0043 0047 0053 0059 0061 0067 0071 and the last is 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571.
Theorem (the running time of Program 1.3.2P, TAOCP Section 1.3.2). Let be the number of odd candidates that are tested, let be the number of divisions, and let be the number of candidates that are composite. The time of the program from the start to the fall into PRINT is u. For , , and , and the time is u. The printing part takes u without the time that JBUS spends while it waits for the printer, so the whole run takes u plus the waiting.
Node: 1.3.2-anal-progP · Lean: not yet stated
Explanation. The division is the main cost: u of the u, which is about percent. The quantities and follow from the primes, and the count is found by running the algorithm. The count has no simple formula: the number of divisions for a prime is the number of odd primes up to the first one with , and for a composite it is the place of its smallest prime factor among the odd primes.
Example. For , the formula gives , which agrees with the run above. For , the formula gives .
Proof. The counts of the lines come from the structure. The five lines from BEGIN to JMP FOUND run once. The lines at FOUND run once for each prime that is entered in the table, except , which BEGIN stores: that is times. The line JL NEXT jumps each of these times except the last, so it enters NEXT times. The line JXZ also enters NEXT, when the remainder is , which happens once for each composite candidate: that is times. So NEXT and ENT3 run once for each candidate, and . The lines at TRY run once for each division, which is times. The other divisions go on to CMPA and JG, so these lines run times. The line JG does not jump exactly when the candidate is prime, and these are the entries into FOUND after the first one. The balance of the entries to TRY gives the check , which is again . The time of the part to PRINT is the sum of the times of the lines times their frequencies: . This is . The values are found as follows. The candidates are the odd numbers from to , so . The number is the 500th prime, so the primes among the candidates are (without and ), and . The count is the sum, over the candidates, of the number of the divisions of the candidate: the primes give and the composites give , which sum to . The largest divisor used is . It is reached in the test of : the divisor gives , since , and the divisor gives , since . No candidate goes further, since each candidate has . The printing part has the time for the first ENT1, times for the lines, where is the time of one column, and for HLT, which is in all. The sum is the time of the whole program when the waiting of JBUS is not counted.
The exercises
The table has the exercises of Section 1.3.2. The kind is the class of spec 06.3 that C1 proposes, and the Lean column says that no statement exists yet. The marker in the third column is the recommended mark in the data of the map.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 1.3.2-1 | 00 | ▶ | K3 | not yet stated | not yet written |
| 1.3.2-2 | 10 | ▶ | K3 | not yet stated | not yet written |
| 1.3.2-3 | 23 | ▶ | informal | not yet stated | not yet written |
| 1.3.2-4 | 25 | ▶ | informal | not yet stated | not yet written |
| 1.3.2-5 | 11 | K3 | not yet stated | not yet written | |
| 1.3.2-6 | 20 | HM | K1 | not yet stated | not yet written |
| 1.3.2-7 | 10 | K3 | not yet stated | not yet written | |
| 1.3.2-8 | 24 | ▶ | informal | not yet stated | not yet written |
| 1.3.2-9 | 25 | ▶ | informal | not yet stated | not yet written |
The exercises fall into four groups. Exercises 1 and 2 ask what an EQU line does and why the last line of Program 1.3.2M does not loop, and the answers are in the definition of symbols and in the description of the return. Exercises 3 and 8 ask for the effect of a given program, and they are solved by tracing it with the rules of the language. Exercises 4, 5 and 7 are about the assembly of a program by hand, the output instructions and the local symbols, and the tables of this chapter are models for them. Exercise 6 is the proof of the test , and block (3) of the proof of Algorithm 1.3.2P gives the argument. Exercise 9 asks for a program that checks the fields of an instruction word, and the good method is a table that has one entry for each operation code.
Lean
The Lean code of tamnd/taocp-lean at main has a model of MIXAL in TAOCP/MIX/Assembler/, in the namespace TAOCP.MIX.Asm. A program is TAOCP.MIX.Asm.Source, a list of TAOCP.MIX.Asm.Line. The function TAOCP.MIX.Asm.assemble gives a program of the machine of Section 1.3.1, and TAOCP.MIX.Asm.Source.program gives the memory and the start address. The Node lines name the declarations. Where the Lean statement differs from the handbook, the Node line says "partial".
- Expressions are
TAOCP.MIX.Asm.Expr, evaluated byTAOCP.MIX.Asm.Ctx.exprfrom left to right, with no precedence. The symbol table isTAOCP.MIX.Asm.Syms, and the local symbols are read byTAOCP.MIX.Asm.localKind. - W-values are
TAOCP.MIX.Asm.Ctx.wvalue, and an instruction line isTAOCP.MIX.Asm.Ctx.instr. An ALF line isTAOCP.MIX.Asm.alfWord. - The assembler has two passes,
TAOCP.MIX.Asm.pass1and the pass that makes the words. An error carries the index of its line, asTAOCP.MIX.Asm.ErrKind.
The Lean statement differs from the handbook in these places. The model reads the lines twice. The handbook assembler reads them once, and the theorem that one pass is enough is not stated. The model gives an error for a symbol that is used and never defined, where the handbook END line adds a word with the content . The model checks that a value fits in , but it has no theorem about future references.
The file TAOCP/V1/S1_3_2.lean has Program 1.3.2M as TAOCP.S1_3_2.programM, in the form of the listing of TAOCP at the addresses to , and not in the form of the listing of this handbook. So the three Node lines for this program say "partial". The declaration TAOCP.S1_3_2.sourceM is the same program as MIXAL lines, and the theorems TAOCP.S1_3_2.sourceM_loads_64 and TAOCP.S1_3_2.sourceM_loads_100 show that the assembler gives the words of TAOCP.S1_3_2.programM for and .
TAOCP.S1_3_2.programM_correctandTAOCP.S1_3_2.programM_timesay that for the subroutine returns to the caller with the largest value in rA and its largest index in , and that it takes u, where isTAOCP.S1_3_2.changes. The proof uses the program logic ofTAOCP/MIX/Logic/.TAOCP.S1_3_2.programM_calladds the call JMP, for a total of u.TAOCP.S1_3_2.programM_mean_timesays that over the orders of distinct numbers, the mean time is . The proof uses the count of changes of Section 1.2.10.
The bound keeps the addresses of the data below the program. The Lean code has also a worked program of its own, TAOCP.MIX.MaxExample, which runs in u, and the file TAOCP/MIX/Logic/Simulation.lean has TAOCP.MIX.simulation, which links a program to the algorithm that it implements.
Not yet stated: the theorem that one pass is enough, the variance of the time of Program 1.3.2M, the listing of this handbook for Program 1.3.2M, and Program 1.3.2P with its running time.
Notes and history
MIXAL is a notation made for the book, and it is not the language of a real machine. Its rules are few, because the aim is a one-pass assembler that a reader can follow by hand. The restriction on future references is the price of that aim.
The programs of this chapter solve the two problems of the section in the manner of the book: a scan with a count down to zero, and a trial division with a table. The listings are the handbook's own, so their times differ from the times of the listings of the book, while the counts that depend on the algorithm alone do not. In particular the number of divisions for the table of primes, , depends on the algorithm, and a listing that follows Algorithm 1.3.2P has the same number.
The method of analysis of the section is used again in every program of the book that follows: find the frequency of each line from the structure of the loops and from the law of conservation, check the frequencies on a small input, and then add the products of the times and the frequencies. Section 1.3.3 uses it for the programs on permutations.
Section 1.3.2 gives MIXAL, the symbolic notation in which the programs of the book are written, and the first two complete programs. This chapter states the rules of the language as blocks with examples and proofs: the line, the symbol, the expression, the parts of an instruction, the W-value, the pseudo-operations, the literal constant, the local symbol and the future reference. It then gives a program that finds the largest of n words with the analysis of its running time, and a program that prints a table of the first 500 primes with the analysis of its running time. The listings are the handbook's own, and their counts were checked by running them.
The source of this page. Something is wrong? Report it.