taocp

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

Volume C1 · Basic concepts · TAOCP Section 1.3.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
  1. The MIX assembly language

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 nn numbers and is the program whose time is analyzed with the quantity AA 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 6464 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 AA, II and FF, 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 L:RL:R means 8L+R8L + R. 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 00, and puts the word of each instruction line, CON line and ALF line at the location counter, which it then increases by 11.

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 1001,…,10051001, \ldots, 1005, stores the sum, subtracts a constant, and halts. The address 10001000 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 6464. A word is shown as its sign and the five bytes, and for an instruction the bytes are the two bytes of ±AA\pm AA, then II, FF and CC.

Location Line Word
3000 ENT1 N + 0  5  0  2  49+\,0\;5\;0\;2\;49
3001 ENTA 0 + 0  0  0  2  48+\,0\;0\;0\;2\;48
3002 ADD T,1 + 15  40  1  5  1+\,15\;40\;1\;5\;1
3003 DEC1 1 + 0  1  0  1  49+\,0\;1\;0\;1\;49
3004 J1P 1B + 46  58  0  2  41+\,46\;58\;0\;2\;41
3005 STA RES + 47  0  0  5  24+\,47\;0\;0\;5\;24
3006 SUB =10= + 47  3  0  5  2+\,47\;3\;0\;5\;2
3007 JMP DONE + 47  2  0  0  39+\,47\;2\;0\;0\;39
3008 RES CON 0 + 0  0  0  0  0+\,0\;0\;0\;0\;0
3009 MSG ALF HELLO + 8  5  13  13  16+\,8\;5\;13\;13\;16
3010 DONE HLT + 0  0  0  2  5+\,0\;0\;0\;2\;5
3011 the literal 1010 + 0  0  0  0  10+\,0\;0\;0\;0\;10

The start address is 30003000. Let the words at 1001,…,10051001, \ldots, 1005 be 4,9,1,7,34, 9, 1, 7, 3. The run takes 2828 u and ends with rA=14\mathrm{rA} = 14 and the sum 2424 in RES. The instructions at 30023002, 30033003 and 30043004 run 55 times each, and the other instructions run once, which gives 1+1+5(2+1+1)+2+2+1+1=281 + 1 + 5(2 + 1 + 1) + 2 + 2 + 1 + 1 = 28.

Proof. The definition has nothing to prove, and the example is checked word by word. The line ORIG 3000 puts the location counter at 30003000, and ENT1 N is the first word. Its address part is N=5N = 5, which is the bytes 0  50\;5, its index is 00, and the pair (F,C)(F, C) is (2,48+1)(2, 48 + 1), since ENT1 is the operation code 48+148 + 1 with the field 22. The line ADD T,1 has ±AA=1000=15⋅64+40\pm AA = 1000 = 15 \cdot 64 + 40, the index 11, the field 55 and the code 11. The line J1P 1B jumps to the nearest line above that has the name 1H, which is 3002=46⋅64+583002 = 46 \cdot 64 + 58, with the field 22 of J1P and the code 4141. The line STA RES has the address 3008=47⋅64+03008 = 47 \cdot 64 + 0, which is the place of the CON line, and the line JMP DONE has 3010=47⋅64+23010 = 47 \cdot 64 + 2. The word of the literal is at 30113011, because the lines that follow the program end at 30103010, so the first free location is 30113011, and the line SUB has 3011=47⋅64+33011 = 47 \cdot 64 + 3. The line ALF HELLO has the codes H=8H = 8, E=5E = 5, L=13L = 13, L=13L = 13, O=16O = 16 of the table of Section 1.3.1. In the run, ENT1 sets rI1=5\mathrm{rI}_1 = 5, and the loop adds the words at 1005,1004,…,10011005, 1004, \ldots, 1001, which are 3,7,1,9,43, 7, 1, 9, 4, so rA takes the values 3,10,11,20,243, 10, 11, 20, 24. The instruction J1P falls through when rI1\mathrm{rI}_1 is 00. Then STA puts 2424 in RES, SUB gives 24−10=1424 - 10 = 14, and JMP goes to the line HLT. □\square

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 00 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 10001000 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 7272. 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 55, T is 10001000, BEG is 30003000, RES is 30083008, MSG is 30093009 and DONE is 30103010.

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 30003000 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 30083008 and the CON line RES gets 30083008. The ALF line then has 30093009, and the line HLT has 30103010, which is the value of DONE. □\square

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 ⌊(a b5)/c⌋\lfloor (a\,b^5) / c \rfloor for the operands aa and cc with 0≤a<c0 \le a < c, where bb is the byte size. The colon gives 8a+c8a + c.

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 2+3∗42 + 3 * 4 is not 1414 but 2020, because 2+32 + 3 is done first. A programmer who needs the other order puts the factors first: 3∗4+23 * 4 + 2. The double slash gives the fraction a/ca/c as a word: the point is at the left of the word, so 1//41//4 is the word with the value one fourth of b5b^5. 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 30053005 at the line, with N=5N = 5 and T=1000T = 1000.

Expression Value Steps
2+3∗42+3*4 20 55, then 5⋅45 \cdot 4
7/2∗57/2*5 15 33, then 3⋅53 \cdot 5
20−3:220-3:2 138 1717, then 8⋅17+28 \cdot 17 + 2
−2∗3-2*3 −6-6 −2-2, then −2⋅3-2 \cdot 3
T+N∗2T+N*2 2010 10051005, then 1005⋅21005 \cdot 2
∗+7*+7 3012 3005+73005 + 7
3000:53000:5 24005 8⋅3000+58 \cdot 3000 + 5
1//41//4 16  0  0  0  016\;0\;0\;0\;0 ⌊645/4⌋=268435456=16⋅644\lfloor 64^5 / 4 \rfloor = 268435456 = 16 \cdot 64^4, for b=64b = 64

Proof. Each value follows from the definition. For 7/2∗57/2*5: 7/27/2 has the integer part 33, and 3⋅5=153 \cdot 5 = 15. For 20−3:220-3:2: 20−3=1720 - 3 = 17, then the colon gives 8⋅17+2=1388 \cdot 17 + 2 = 138. For T+N∗2T+N*2: 1000+5=10051000 + 5 = 1005, and 1005⋅2=20101005 \cdot 2 = 2010. For 1//41//4: 645=107374182464^5 = 1073741824, one fourth of it is 268435456268435456, and 268435456=16⋅644268435456 = 16 \cdot 64^4, so the word is + 16  0  0  0  0+\,16\;0\;0\;0\;0. The value of the double slash depends on bb, so a program that uses it must be right for each bb of the range of Section 1.3.1. □\square

Definition (the instruction line, TAOCP Section 1.3.2). The address field of an instruction line has the form A,I(F)A,I(F). The part AA is empty, which means 00, or an expression, or a future reference, or a literal constant. The part II is empty, which means 00, or it is a comma followed by an expression. The part FF is empty, which means the normal field of the operation, or it is an expression in parentheses. The assembler gives the word ± A1 A2 I F C\pm\,A_1\,A_2\,I\,F\,C, where ±A1A2\pm A_1 A_2 is the value of AA written as two bytes and a sign, II and FF are the values of the other parts, and CC 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 55 for the loads, the stores and the arithmetic, 22 for STJ, ENT and HLT, and 00 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 66, is an error of the program.

Example. The table has some lines and their words for b=64b = 64.

Line Word Remark
LDA 2000,3(1:5) + 31  16  3  13  8+\,31\;16\;3\;13\;8 2000=31⋅64+162000 = 31 \cdot 64 + 16, F=8⋅1+5=13F = 8 \cdot 1 + 5 = 13
LDX 1002,4(2:3) + 15  42  4  19  15+\,15\;42\;4\;19\;15 F=8⋅2+3=19F = 8 \cdot 2 + 3 = 19
LDA -5 − 0  5  0  5  8-\,0\;5\;0\;5\;8 the sign is that of the address
JMP 3002 + 46  58  0  0  39+\,46\;58\;0\;0\;39 normal field 00
HLT + 0  0  0  2  5+\,0\;0\;0\;2\;5 empty address, normal field 22
NOP + 0  0  0  0  0+\,0\;0\;0\;0\;0 code 00, field 00
ENTA 0,1 + 0  0  1  2  48+\,0\;0\;1\;2\;48 index 11, normal field 22

Proof. The words follow from the definition and the codes of Section 1.3.1. In LDA 2000,3(1:5), the address 20002000 is the bytes 31  1631\;16, the index is 33, the field is 8⋅1+5=138 \cdot 1 + 5 = 13 and the code of LDA is 88. In LDX 1002,4(2:3), 1002=15⋅64+421002 = 15 \cdot 64 + 42, and the code of LDX is 1515. In LDA -5, the value of the address part is −5-5, so the sign is −- and the bytes are 0  50\;5, with F=5F = 5. For JMP the normal field is 00 and the code is 3939. For HLT the code is 55 and the field is 22. For ENTA the code is 4848 and the field is 22. □\square

Definition (W-values, TAOCP Section 1.3.2). A W-value is a list E1(F1),E2(F2),…,Ek(Fk)E_1(F_1), E_2(F_2), \ldots, E_k(F_k), in which each EjE_j is an expression and each FjF_j is a field in parentheses, with the default (0:5)(0:5) 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 + 0  0  0  0  0+\,0\;0\;0\;0\;0, in the way of the instruction STA: for the field FjF_j, the sign and the low bytes of EjE_j 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 b=64b = 64 and N=5N = 5, T=1000T = 1000:

W-value Word
−1(0:0),300(4:5)-1(0:0),300(4:5) − 0  0  0  4  44-\,0\;0\;0\;4\;44
300(4:5),−1300(4:5),-1 − 0  0  0  0  1-\,0\;0\;0\;0\;1
2000(0:2),9(3:3)2000(0:2),9(3:3) + 31  16  9  0  0+\,31\;16\;9\;0\;0
N(4:4),T(2:3)N(4:4),T(2:3) + 0  15  40  5  0+\,0\;15\;40\;5\;0
−5-5 − 0  0  0  0  5-\,0\;0\;0\;0\;5

Proof. In −1(0:0),300(4:5)-1(0:0),300(4:5), the first part stores only the sign of −1-1, so the word becomes − 0  0  0  0  0-\,0\;0\;0\;0\;0. The second part stores 300=4⋅64+44300 = 4 \cdot 64 + 44 in the bytes 44 and 55, and the field (4:5)(4:5) does not hold the sign, so the sign stays −- and the word is − 0  0  0  4  44-\,0\;0\;0\;4\;44. In the second line the order is reversed: the part 300(4:5)300(4:5) gives + 0  0  0  4  44+\,0\;0\;0\;4\;44, and then the part −1-1 with the default field replaces the whole word, sign included, so the result is − 0  0  0  0  1-\,0\;0\;0\;0\;1. The third line stores 2000=31⋅64+162000 = 31 \cdot 64 + 16 in (0:2)(0:2) and the byte 99 at the position 33. In the fourth line the byte 55 goes to the position 44 and the number 10001000 takes the positions 22 and 33. □\square

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 ww in its address sets the counter to ww and makes no word. A line EQU with the W-value ww in its address gives the symbol of its field LOC the value ww, 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 ∗+k*+k keeps kk 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 100100. The assembler gives the words of the table for b=64b = 64. The line ORIG with ∗+3*+3 comes after the line at 103103, where the counter is 104104, so it sets the counter to 107107.

         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 + 1  39  0  1  41+\,1\;39\;0\;1\;41
101 DEC1 1 + 0  1  0  1  49+\,0\;1\;0\;1\;49
102 JMP 2B + 1  36  0  0  39+\,1\;36\;0\;0\;39
103 2H ENT1 0 + 0  0  0  2  49+\,0\;0\;0\;2\;49
104 to 106 not assembled the words keep their old content
107 V CON −7-7 − 0  0  0  0  7-\,0\;0\;0\;0\;7
108 W CON V+1 + 0  0  0  1  44+\,0\;0\;0\;1\;44

The symbol V has the value 107107 and W has the value 108108. The word at 108108 holds V+1=108=1⋅64+44V + 1 = 108 = 1 \cdot 64 + 44, which is its own address.

Proof. The counter is 100100 after the first ORIG. The four instruction lines take the locations 100100 to 103103 and leave the counter at 104104. The expression ∗+3*+3 has the value 104+3=107104 + 3 = 107, so the line V gets the location 107107 and W gets 108108. The symbol V is defined at the line of W, so V+1V + 1 is allowed, and its value is 108108, with the bytes 1  441\;44. The word of V has the sign −- and the value 77. No word is made for the lines ORIG and END. □\square

Definition (CON and ALF, TAOCP Section 1.3.2). A line CON with the W-value ww in its address makes the word ww 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 11.

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 + 0  0  0  0  0+\,0\;0\;0\;0\;0 at 30083008, and the line MSG ALF HELLO makes + 8  5  13  13  16+\,8\;5\;13\;13\;16 at 30093009. The line CON with 3(1:1),5(4:5)3(1:1),5(4:5) makes + 3  0  0  0  5+\,3\;0\;0\;0\;5.

Proof. The codes are those of Section 1.3.1: the letters A to I are 11 to 99, so H=8H = 8 and E=5E = 5, and the letters J to R are 1111 to 1919, so L=13L = 13 and O=16O = 16. The W-value 3(1:1),5(4:5)3(1:1),5(4:5) puts 33 in the first byte and 55 in the last two bytes, and the last two bytes are 0  50\;5 as a field, which leaves + 3  0  0  0  5+\,3\;0\;0\;0\;5. □\square

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 AA-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 1010, since the instruction takes the word at the address of the new symbol, and that word holds 1010. 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 30113011 and the word at 30113011 is + 0  0  0  0  10+\,0\;0\;0\;0\;10. 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 30323032, 30333033 and 30343034, with the values 500500, 2020 and 500500.

Proof. In the first example the last line of the program is at 30103010, so the new word has the next location, 30113011. In the second program the last instruction, HLT, is at 30313031, and the three words are placed in the order of the lines that contain them, so they take 30323032, 30333033, 30343034. The value of PL is 500=7⋅64+52500 = 7 \cdot 64 + 52, and the word of the literal is + 0  0  0  7  52+\,0\;0\;0\;7\;52. □\square

Definition (local symbols, TAOCP Section 1.3.2). A local symbol is a digit dd from 00 to 99 followed by H, F or B. A line with ddH 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 ddH. In the address part, ddB is the symbol defined by the nearest line ddH above the line that uses it, and ddF is the symbol defined by the nearest line ddH below it. The form ddF is a future reference. The forms ddB and ddF are not allowed in the field LOC, and ddH is not allowed in an address part. The form ddB must have a line ddH above it, and the form ddF must have one below it. The form ddF 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 ddH is meant. A line that defines ddH and that also uses ddF, as the first line of the fragment below does, refers to the next line ddH and not to itself.

Example. In the fragment of the example of ORIG, the line at 100100 is 2H and has the address 2F. The nearest 2H below it is the line at 103103, so the word at 100100 has the address 103=1⋅64+39103 = 1 \cdot 64 + 39. The line JMP 2B at 102102 has the nearest 2H above it at 100100, so its address is 100=1⋅64+36100 = 1 \cdot 64 + 36. In the first example the line J1P 1B at 30043004 has the nearest 1H at 30023002, which is the address 46  5846\;58.

Proof. The proof is by reading the words of the two tables above. For 2F at 100100: the lines below 100100 with the field 2H are at 103103 only, so the value is 103103. For 2B at 102102: the lines above 102102 with 2H are at 100100 only, since the line at 103103 is below, so the value is 100100. For 1B at 30043004: the only line 1H is at 30023002. □\square

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 AA-part of an instruction. It must not be an operand of an operator in an expression, it must not be in the II-part or the FF-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 AA-part of JMP is allowed, and in an expression, a CON address or an ORIG address it is not. □\square

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 00, an ORIG line sets it to an expression that has only defined symbols, and every other line that makes a word adds 11. 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 II, FF and CC are known, since II and FF are expressions of defined symbols, and the AA-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 AA-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. □\square

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 00 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 (4:5)(4:5) 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 30003000, whose (4:5)(4:5) field is 30003000, so the program starts at 30003000. The assembler adds one word for the literal 1010 at 30113011. In the program of primes, END BEGIN starts at 30003000 and the three literals take 30323032 to 30343034.

Proof. The definition has nothing to prove. In the first example the lines make the words 30003000 to 30103010 and the literal makes 30113011, so the first location after the inserted word is 30123012. □\square

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 rI1=n\mathrm{rI}_1 = n with n≥1n \ge 1, puts the numbers X[1],…,X[n]X[1], \ldots, X[n] at the addresses 1001,…,1000+n1001, \ldots, 1000 + n, 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 rI2\mathrm{rI}_2 the largest index jj with X[j]X[j] equal to that value. It destroys rI1\mathrm{rI}_1, which is 00 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 rI2\mathrm{rI}_2 the place where it is. The register rI1\mathrm{rI}_1 is the index of the number that is compared next, and it counts down to 00. 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 b=64b = 64. The number AA is the number of times that the line at 30073007 runs.

Location Line Time Frequency Word
3000 MAX STJ EX 2 1 + 47  2  0  2  32+\,47\;2\;0\;2\;32
3001 ENT2 0,1 1 1 + 0  0  1  2  50+\,0\;0\;1\;2\;50
3002 LDA X,1 2 1 + 15  40  1  5  8+\,15\;40\;1\;5\;8
3003 LOOP DEC1 1 1 nn + 0  1  0  1  49+\,0\;1\;0\;1\;49
3004 J1Z EX 1 nn + 47  2  0  1  41+\,47\;2\;0\;1\;41
3005 CMPA X,1 2 n−1n - 1 + 15  40  1  5  56+\,15\;40\;1\;5\;56
3006 JGE LOOP 1 n−1n - 1 + 46  59  0  7  39+\,46\;59\;0\;7\;39
3007 ENT2 0,1 1 AA + 0  0  1  2  50+\,0\;0\;1\;2\;50
3008 LDA X,1 2 AA + 15  40  1  5  8+\,15\;40\;1\;5\;8
3009 JMP LOOP 1 AA + 46  59  0  0  39+\,46\;59\;0\;0\;39
3010 EX JMP * 1 1 + 47  2  0  0  39+\,47\;2\;0\;0\;39 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 30103010 is a jump to itself in the listing, and the first instruction replaces its address by the return address.

A run. Let n=4n = 4 and X[1],…,X[4]=8,3,9,5X[1], \ldots, X[4] = 8, 3, 9, 5, called from the address 101101, so that rJ is 102102 at the entry. The table shows the state after each step. The column rI1 holds kk, and the column rA holds the largest value seen. The column ci shows the comparison indicator, which is −1-1 for less, 00 for equal and 11 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 −1-1
7 3006 JGE LOOP 1 10 5 3 4 −1-1
8 3007 ENT2 0,1 1 11 5 3 3 −1-1
9 3008 LDA X,1 2 13 9 3 3 −1-1
10 3009 JMP LOOP 1 14 9 3 3 −1-1
11 3003 DEC1 1 1 15 9 2 3 −1-1
12 3004 J1Z EX 1 16 9 2 3 −1-1
13 3005 CMPA X,1 2 18 9 2 3 11
14 3006 JGE LOOP 1 19 9 2 3 11
15 3003 DEC1 1 1 20 9 1 3 11
16 3004 J1Z EX 1 21 9 1 3 11
17 3005 CMPA X,1 2 23 9 1 3 11
18 3006 JGE LOOP 1 24 9 1 3 11
19 3003 DEC1 1 1 25 9 0 3 11
20 3004 J1Z EX 1 26 9 0 3 11
21 3010 JMP 102 1 27 9 0 3 11

The answer is rA =9= 9 and rI2=3\mathrm{rI}_2 = 3, which is the place of the number 99 in 8,3,9,58, 3, 9, 5. The time is 2727 u. The count AA is 11, since only the line at 30073007 ran, once, at the step 8. The formula of the analysis gives 5⋅4+4⋅1+3=275 \cdot 4 + 4 \cdot 1 + 3 = 27. The last step is the jump to the caller.

Correctness. Let kk be the value of rI1\mathrm{rI}_1 when control arrives at the label LOOP, before DEC1. The claim is that for 1≤k≤n1 \le k \le n at that moment, rA is the largest of X[k],…,X[n]X[k], \ldots, X[n], and rI2\mathrm{rI}_2 is the largest index jj with k≤j≤nk \le j \le n and X[j]X[j] equal to rA. At the first arrival k=nk = n, the lines at 30013001 and 30023002 have set rI2=n\mathrm{rI}_2 = n and rA=X[n]\mathrm{rA} = X[n], and the claim holds. Suppose that it holds for kk. The line DEC1 makes rI1=k−1\mathrm{rI}_1 = k - 1. If k−1=0k - 1 = 0, the line J1Z jumps to EX, and the claim for k=1k = 1 says that rA is the largest of all nn numbers and rI2\mathrm{rI}_2 is the largest index of it, which is the answer. Otherwise the line CMPA compares rA with X[k−1]X[k - 1]. If rA≥X[k−1]\mathrm{rA} \ge X[k - 1], JGE goes back, rA and rI2\mathrm{rI}_2 do not change, and the claim holds for k−1k - 1: the largest value is still rA, and the largest index of it is still rI2\mathrm{rI}_2, since a tie with X[k−1]X[k - 1] has a smaller index. If rA<X[k−1]\mathrm{rA} < X[k - 1], the lines at 30073007 and 30083008 set rI2=k−1\mathrm{rI}_2 = k - 1 and rA=X[k−1]\mathrm{rA} = X[k - 1], and the claim holds for k−1k - 1 since X[k−1]X[k - 1] is now the strict largest and has the only index. The jump JMP LOOP goes back. The register rI1\mathrm{rI}_1 decreases at each pass, so the loop stops after nn arrivals at LOOP. The first instruction STJ stores the return address in the line EX, so the last jump goes back to the caller. □\square

Theorem (the running time of Program 1.3.2M, TAOCP Section 1.3.2). For n≥1n \ge 1, the subroutine, from the first instruction to the return jump, takes 5n+4A+35n + 4A + 3 u, where AA is the number of values of kk with 1≤k<n1 \le k < n for which X[k]X[k] is larger than each of X[k+1],…,X[n]X[k+1], \ldots, X[n]. The time does not include the call JMP MAX, which takes 11 u. The time is at least 5n+35n + 3 and at most 9n−19n - 1.

Node: 1.3.2-anal-progM · Lean: TAOCP.S1_3_2.programM_time (partial)

Explanation. The count AA is the number of times that the largest value changes in the scan, and it is the quantity AA of Section 1.2.10 for the numbers read from X[n]X[n] down to X[1]X[1]. The terms 5n5n and 33 do not depend on the data. The term 4A4A is the cost of the changes, with 44 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 (5n+3A+5)(5n + 3A + 5) u. The count AA is the same for both listings, and only the constants differ.

Example. For n=5n = 5 the data 1,2,3,4,51,2,3,4,5 has A=0A = 0 and takes 2828 u, the data 5,4,3,2,15,4,3,2,1 has A=4A = 4 and takes 4444 u, and the data 31,27,64,11,4531,27,64,11,45 has A=1A = 1 and takes 5⋅5+4+3=325 \cdot 5 + 4 + 3 = 32 u. The case n=1n = 1 takes 88 u. These times are those of a run of the program.

Proof. The line DEC1 runs once for each value of kk from nn down to 11, so it runs nn times, and the line J1Z runs nn times as well. The line J1Z jumps only the last time, so the lines CMPA and JGE run n−1n - 1 times. The lines of the update at 30073007, 30083008 and 30093009 run each time that JGE does not jump, and by definition this is AA times: JGE does not jump for the value k−1k - 1 exactly when X[k−1]X[k - 1] is larger than the largest of the values to its right, which is the condition of AA. The line JGE jumps n−1−An - 1 - A times, and the line JMP LOOP runs AA times, so control arrives at LOOP 1+(n−1−A)+A=n1 + (n - 1 - A) + A = n times, which is in agreement with the nn runs of DEC1. The times of the lines are 2,1,22, 1, 2 for the first three, 1,11, 1 for DEC1 and J1Z, 2,12, 1 for CMPA and JGE, 1,2,11, 2, 1 for the update, and 11 for the last line. The sum is (2+1+2)+n(1+1)+(n−1)(2+1)+A(1+2+1)+1=5+2n+3n−3+4A+1=5n+4A+3(2 + 1 + 2) + n(1 + 1) + (n - 1)(2 + 1) + A(1 + 2 + 1) + 1 = 5 + 2n + 3n - 3 + 4A + 1 = 5n + 4A + 3. The value AA ranges from 00 to n−1n - 1. It is 00 if X[n]X[n] is at least all the others and n−1n - 1 if X[n]<X[n−1]<⋯<X[1]X[n] < X[n-1] < \cdots < X[1], so the time ranges from 5n+35n + 3 to 9n−19n - 1. For the example of four numbers, A=1A = 1 and the time is 2727. □\square

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 X[1],…,X[n]X[1], \ldots, X[n] be distinct and let each of the n!n! orders be equally likely. Then AA has the mean Hn−1H_n - 1 and the variance Hn−Hn(2)H_n - H_n^{(2)}, and so the time of the subroutine has the mean 5n+4Hn−15n + 4H_n - 1 and the variance 16(Hn−Hn(2))16(H_n - H_n^{(2)}), where Hn=∑j=1n1/jH_n = \sum_{j=1}^{n} 1/j and Hn(2)=∑j=1n1/j2H_n^{(2)} = \sum_{j=1}^{n} 1/j^2.

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 ln⁡n\ln n, so for large nn the time is 5n5n 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 n=5n = 5, the 120120 orders have A=0,1,2,3,4A = 0, 1, 2, 3, 4 with the counts 24,50,35,10,124, 50, 35, 10, 1, and the mean is (50+70+30+4)/120=77/60(50 + 70 + 30 + 4)/120 = 77/60, which equals H5−1H_5 - 1 since H5=137/60H_5 = 137/60. The mean time is 5⋅5+4⋅77/60+3=497/155 \cdot 5 + 4 \cdot 77/60 + 3 = 497/15, which is the value of the formula 5n+4Hn−15n + 4H_n - 1, and a run of the program on all 120120 orders gives the same value. The variance of AA is H5−H5(2)=137/60−5269/3600=2951/3600H_5 - H_5^{(2)} = 137/60 - 5269/3600 = 2951/3600, so the variance of the time is 16⋅2951/3600=2951/22516 \cdot 2951/3600 = 2951/225, which is the value that the run gives.

Proof. For 1≤k<n1 \le k < n, let ak=1a_k = 1 if X[k]X[k] is larger than each of X[k+1],…,X[n]X[k+1], \ldots, X[n] and ak=0a_k = 0 otherwise, so A=∑akA = \sum a_k. Among the n−k+1n - k + 1 numbers X[k],…,X[n]X[k], \ldots, X[n], each is equally likely to be the largest, so ak=1a_k = 1 with the probability 1/(n−k+1)1/(n - k + 1). The variables aka_k are independent. To see this, let rkr_k be the rank of X[k]X[k] among X[k],…,X[n]X[k], \ldots, X[n], a number from 11 to n−k+1n - k + 1. An order determines the list (r1,…,rn)(r_1, \ldots, r_n), 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 n!n! orders to the lists with 1≤rk≤n−k+11 \le r_k \le n - k + 1, whose number is n!n!. Hence every list is equally likely, the rkr_k are independent, and each is uniform. Since aka_k depends on rkr_k only, the aka_k are independent. With j=n−k+1j = n - k + 1, which runs from 22 to nn, the mean is ∑j=2n1/j=Hn−1\sum_{j=2}^{n} 1/j = H_n - 1. The variance of a sum of independent variables is the sum of the variances, and aka_k has the variance (1/j)(1−1/j)(1/j)(1 - 1/j), so the variance of AA is ∑j=2n(1/j−1/j2)=(Hn−1)−(Hn(2)−1)=Hn−Hn(2)\sum_{j=2}^{n} (1/j - 1/j^2) = (H_n - 1) - (H_n^{(2)} - 1) = H_n - H_n^{(2)}. The time is 5n+3+4A5n + 3 + 4A, so its mean is 5n+3+4(Hn−1)=5n+4Hn−15n + 3 + 4(H_n - 1) = 5n + 4H_n - 1, and its variance is 1616 times that of AA. □\square

Algorithm (the first 500 primes by trial division, TAOCP Section 1.3.2, Algorithm 1.3.2P). The input is the number L≥2L \ge 2 of primes wanted, and the section uses L=500L = 500. The output is the table P[1],…,P[L]P[1], \ldots, P[L] of the first LL primes in increasing order. The table is built in place, and each odd candidate nn 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 nn for division by the odd primes already found, in order. It answers "composite" when a prime divides nn. It answers "prime" when the quotient is not larger than the divisor pp, since then n<(p+1)2n < (p + 1)^2, and a composite nn would have a prime factor not larger than pp. It never divides by an even number, and it never needs a square root.

Example. The table starts as 2,32, 3. For n=5n = 5, the divisor P[2]=3P[2] = 3 gives q=1q = 1 and r=2r = 2, so q≤3q \le 3 and 55 is prime. For n=9n = 9, q=3q = 3 and r=0r = 0, so 99 is not prime. For n=13n = 13, the divisor 33 gives q=4>3q = 4 > 3 and r=1r = 1, so kk becomes 33, and the divisor 55 gives q=2≤5q = 2 \le 5, so 1313 is prime. For L=500L = 500 the last entry is P[500]=3571P[500] = 3571.

Proof. The proof has four parts. Let π(x)\pi(x) be the number of primes ≤x\le x.

(1) The invariant. At each test of the condition j<Lj < L, the number nn is odd with n≥3n \ge 3, the table P[1],…,P[j]P[1], \ldots, P[j] holds the primes ≤n\le n in increasing order, and j=π(n)j = \pi(n). This holds at the first test, with n=3n = 3 and the table 2,32, 3. Let it hold for nn, with j<Lj < L. The number n+1n + 1 is even and larger than 22, so it is not prime. Hence the primes ≤n+2\le n + 2 are those ≤n\le n, with n+2n + 2 added if n+2n + 2 is prime. By parts (2) and (3) below, IsPrime(n+2,P)(n + 2, P) returns true if and only if n+2n + 2 is prime, and then the loop body appends it. So the invariant holds at the next test.

(2) A zero remainder. Let n≥5n \ge 5 be odd, with all primes below nn in the table. If r=0r = 0 for the divisor p=P[k]p = P[k], then pp divides nn and 1<p<n1 < p < n, since pp is in the table and nn is not. So nn is composite, and the answer false is right.

(3) The test q≤P[k]q \le P[k]. Suppose that IsPrime returns true at the divisor pp. No earlier divisor divided nn, the divisor pp does not divide nn, and 22 does not divide the odd number nn. So no prime ≤p\le p divides nn. Since r≠0r \ne 0 and q=⌊n/p⌋≤pq = \lfloor n/p \rfloor \le p, the number satisfies n=qp+r<(q+1)p≤p(p+1)<(p+1)2n = qp + r < (q + 1)p \le p(p + 1) < (p + 1)^2. Suppose that nn is composite. Its smallest prime factor ℓ\ell satisfies ℓ2≤n\ell^2 \le n, and ℓ>p\ell > p since no prime ≤p\le p divides nn, so ℓ≥p+1\ell \ge p + 1 and n≥(p+1)2n \ge (p + 1)^2. This contradicts n<(p+1)2n < (p + 1)^2, so nn is prime. Now let nn be composite, with the smallest prime factor ℓ\ell, which is odd and less than nn, so ℓ\ell is in the table. The loop cannot return true at a divisor p<ℓp < \ell, because that would prove nn prime. It cannot return false at such a divisor either, since p<ℓp < \ell does not divide nn. So it reaches ℓ\ell, finds r=0r = 0 and returns false. Hence IsPrime returns true if and only if nn is prime.

(4) The loop stops and kk never goes past the table. For a composite nn, the loop ends at ℓ\ell by part (3). For a prime n≥5n \ge 5, Bertrand's postulate with m=(n−1)/2≥2m = (n - 1)/2 \ge 2 gives a prime pp with m<p<2m=n−1m < p < 2m = n - 1. This pp is in the table, it is odd, and 2p>n−12p > n - 1, so 2p≥n2p \ge n and q=⌊n/p⌋≤2<pq = \lfloor n/p \rfloor \le 2 < p. So the loop stops at pp at the latest. Since there are infinitely many primes, jj reaches LL, and by the invariant the table then holds the first LL primes. □\square

Program (the table of primes in MIX, TAOCP Section 1.3.2, Program 1.3.2P). The program builds the table in the words 1001,…,15001001, \ldots, 1500, so P[j]P[j] is at 1000+j1000 + j, and it prints the table on the line printer, unit 1818, in 2525 lines of 2020 numbers each. Each number has four digits, and a blank follows it. A printed line is the 2424 words 2000,…,20232000, \ldots, 2023. The program writes only the bytes 11 to 44 of the words 2000,…,20192000, \ldots, 2019, so it requires that byte 55 of these words and all of the words 2020,…,20232020, \ldots, 2023 be 00 when it starts, which is true after the memory is cleared.

Node: Program 1.3.2P · Lean: not yet stated

Idea. The register rI1\mathrm{rI}_1 is jj, rI2\mathrm{rI}_2 is nn and rI3\mathrm{rI}_3 is kk. The division is done on the pair rA, rX, with rA=0\mathrm{rA} = 0 and rX=n\mathrm{rX} = n, which gives the quotient in rA and the remainder in rX. The test q≤P[k]q \le P[k] is a comparison of the quotient with the divisor. The loop of IsPrime is rotated: its first line increases kk, so ENT3 sets k=1k = 1 and the first division uses P[2]=3P[2] = 3. 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 2424 words.

The program. The symbols are TAB=1000\mathrm{TAB} = 1000, BUF=2000\mathrm{BUF} = 2000, PL=500\mathrm{PL} = 500 and PRN=18\mathrm{PRN} = 18. The table has the listing, with the time of each line, the frequency of each line in a run to the table of PLPL primes, and the words for b=64b = 64. The numbers EE, DD and cc are defined below.

Location Line Time Frequency Word
3000 BEGIN ENTA 2 1 1 + 0  2  0  2  48+\,0\;2\;0\;2\;48
3001 STA TAB+1 2 1 + 15  41  0  5  24+\,15\;41\;0\;5\;24
3002 ENT1 1 1 1 + 0  1  0  2  49+\,0\;1\;0\;2\;49
3003 ENT2 3 1 1 + 0  3  0  2  50+\,0\;3\;0\;2\;50
3004 JMP FOUND 1 1 + 47  6  0  0  39+\,47\;6\;0\;0\;39
3005 NEXT INC2 2 1 EE + 0  2  0  0  50+\,0\;2\;0\;0\;50
3006 ENT3 1 1 EE + 0  1  0  2  51+\,0\;1\;0\;2\;51
3007 TRY INC3 1 1 DD + 0  1  0  0  51+\,0\;1\;0\;0\;51
3008 ENTA 0 1 DD + 0  0  0  2  48+\,0\;0\;0\;2\;48
3009 ENTX 0,2 1 DD + 0  0  2  2  55+\,0\;0\;2\;2\;55
3010 DIV TAB,3 12 DD + 15  40  3  5  4+\,15\;40\;3\;5\;4
3011 JXZ NEXT 1 DD + 46  61  0  1  47+\,46\;61\;0\;1\;47
3012 CMPA TAB,3 2 D−cD - c + 15  40  3  5  56+\,15\;40\;3\;5\;56
3013 JG TRY 1 D−cD - c + 46  63  0  6  39+\,46\;63\;0\;6\;39
3014 FOUND INC1 1 1 PL−1PL - 1 + 0  1  0  0  49+\,0\;1\;0\;0\;49
3015 ST2 TAB,1 2 PL−1PL - 1 + 15  40  1  5  26+\,15\;40\;1\;5\;26
3016 CMP1 =PL= 2 PL−1PL - 1 + 47  24  0  5  57+\,47\;24\;0\;5\;57
3017 JL NEXT 1 PL−1PL - 1 + 46  61  0  4  39+\,46\;61\;0\;4\;39
3018 PRINT ENT1 1 1 1 + 0  1  0  2  49+\,0\;1\;0\;2\;49
3019 LINE JBUS *(PRN) 1 25 + 47  11  0  18  34+\,47\;11\;0\;18\;34
3020 ENT5 0 1 25 + 0  0  0  2  53+\,0\;0\;0\;2\;53
3021 COL LDA TAB,1 2 500 + 15  40  1  5  8+\,15\;40\;1\;5\;8
3022 CHAR 10 500 + 0  0  0  1  5+\,0\;0\;0\;1\;5
3023 STX BUF,5(1:4) 2 500 + 31  16  5  12  31+\,31\;16\;5\;12\;31
3024 INC1 1 1 500 + 0  1  0  0  49+\,0\;1\;0\;0\;49
3025 INC5 1 1 500 + 0  1  0  0  53+\,0\;1\;0\;0\;53
3026 CMP5 =20= 2 500 + 47  25  0  5  61+\,47\;25\;0\;5\;61
3027 JL COL 1 500 + 47  13  0  4  39+\,47\;13\;0\;4\;39
3028 OUT BUF(PRN) 1 25 + 31  16  0  18  37+\,31\;16\;0\;18\;37
3029 CMP1 =PL= 2 25 + 47  26  0  5  57+\,47\;26\;0\;5\;57
3030 JLE LINE 1 25 + 47  11  0  9  39+\,47\;11\;0\;9\;39
3031 HLT 1 1 + 0  0  0  2  5+\,0\;0\;0\;2\;5
3032 the literal PLPL + 0  0  0  7  52+\,0\;0\;0\;7\;52
3033 the literal 2020 + 0  0  0  0  20+\,0\;0\;0\;0\;20
3034 the literal PLPL + 0  0  0  7  52+\,0\;0\;0\;7\;52

The frequencies of the lines of the printing part are those for PL=500PL = 500. The four lines at FOUND record a prime, and they run once for each prime except 22. The line JL NEXT jumps after each of these runs except the last, so PL−2PL - 2 times, and after the last one control falls into PRINT.

A run. A small table shows the idea. Let PL=8PL = 8, so the program stops after the primes 2,3,5,7,11,13,17,192, 3, 5, 7, 11, 13, 17, 19. The table lists each execution of DIV: the register rI1\mathrm{rI}_1 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.

nn rI1\mathrm{rI}_1 rI3\mathrm{rI}_3 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 E=8E = 8 (the odd numbers 5,7,…,195, 7, \ldots, 19), D=11D = 11 (the lines of the table) and c=2c = 2 (the numbers 99 and 1515). The frequencies of the lines are 11 for the five lines to 30043004, 88 for NEXT and ENT3, 1111 for the five lines at TRY, 99 for CMPA and JG, and 77 for the four lines of FOUND. The time up to the fall into PRINT is 6+8⋅2+11⋅16+9⋅3+7⋅6=2676 + 8 \cdot 2 + 11 \cdot 16 + 9 \cdot 3 + 7 \cdot 6 = 267 u. The table at the end is 2,3,5,7,11,13,17,192, 3, 5, 7, 11, 13, 17, 19.

Correctness. The program follows Algorithm 1.3.2P. The first four lines put 22 in TAB[1]\mathrm{TAB}[1], j=1j = 1 in rI1\mathrm{rI}_1 and n=3n = 3 in rI2\mathrm{rI}_2, and JMP FOUND then stores 33 as TAB[2]\mathrm{TAB}[2] with j=2j = 2, which completes the first line of FirstPrimes. The four lines at FOUND add one to rI1\mathrm{rI}_1, store rI2\mathrm{rI}_2 at TAB+rI1\mathrm{TAB} + \mathrm{rI}_1 and test j<Lj < L: JL NEXT goes on to the next candidate, and otherwise control falls into PRINT. The line NEXT adds 22 to nn, and ENT3 sets k=1k = 1. The lines at TRY are the loop of IsPrime. INC3 increases kk, so the first divisor is TAB[2]=3\mathrm{TAB}[2] = 3. With rA=0\mathrm{rA} = 0 and rX=n\mathrm{rX} = n, the division by TAB[k]\mathrm{TAB}[k] cannot overflow since 0<TAB[k]0 < \mathrm{TAB}[k], and it gives qq in rA and rr in rX. The line JXZ goes to NEXT if r=0r = 0, which is the answer false. The lines CMPA and JG compare qq with the divisor. If qq 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 rI1\mathrm{rI}_1 goes from 11 to 501501, and in each line the register rI5\mathrm{rI}_5 counts 2020 columns. For each column, the line LDA brings TAB[rI1]\mathrm{TAB}[\mathrm{rI}_1] into rA, CHAR puts its ten decimal digits in rA and rX with the digit dd as the code 30+d30 + d, and STX with the field (1:4)(1:4) puts the last four bytes of rX, which are the last four digits, in the bytes 11 to 44 of the word BUF+rI5\mathrm{BUF} + \mathrm{rI}_5. The byte 55 is a blank by the requirement of the program. All the primes are below 1000010000, so four digits are enough, and the largest is 35713571. After 2020 columns, OUT sends the 2424 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 rI1\mathrm{rI}_1 with 500500, and JLE goes back if rI1≤500\mathrm{rI}_1 \le 500, so the program prints 2525 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. □\square

Theorem (the running time of Program 1.3.2P, TAOCP Section 1.3.2). Let EE be the number of odd candidates n≥5n \ge 5 that are tested, let DD be the number of divisions, and let cc be the number of candidates that are composite. The time of the program from the start to the fall into PRINT is 6 PL+2E+19D−3c6\,PL + 2E + 19D - 3c u. For PL=500PL = 500, E=1784E = 1784, c=1286c = 1286 and D=9538D = 9538, and the time is 183932183932 u. The printing part takes 96529652 u without the time that JBUS spends while it waits for the printer, so the whole run takes 193584193584 u plus the waiting.

Node: 1.3.2-anal-progP · Lean: not yet stated

Explanation. The division is the main cost: 12⋅9538=11445612 \cdot 9538 = 114456 u of the 183932183932 u, which is about 6262 percent. The quantities EE and cc follow from the primes, and the count DD is found by running the algorithm. The count DD has no simple formula: the number of divisions for a prime nn is the number of odd primes up to the first one with q≤pq \le p, and for a composite it is the place of its smallest prime factor among the odd primes.

Example. For PL=8PL = 8, the formula gives 6⋅8+2⋅8+19⋅11−3⋅2=48+16+209−6=2676 \cdot 8 + 2 \cdot 8 + 19 \cdot 11 - 3 \cdot 2 = 48 + 16 + 209 - 6 = 267, which agrees with the run above. For PL=500PL = 500, the formula gives 3000+3568+181222−3858=1839323000 + 3568 + 181222 - 3858 = 183932.

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 22, which BEGIN stores: that is PL−1PL - 1 times. The line JL NEXT jumps each of these times except the last, so it enters NEXT PL−2PL - 2 times. The line JXZ also enters NEXT, when the remainder is 00, which happens once for each composite candidate: that is cc times. So NEXT and ENT3 run once for each candidate, and E=PL−2+cE = PL - 2 + c. The lines at TRY run once for each division, which is DD times. The other D−cD - c divisions go on to CMPA and JG, so these lines run D−cD - c times. The line JG does not jump exactly when the candidate is prime, and these are the PL−2PL - 2 entries into FOUND after the first one. The balance of the entries to TRY gives the check D=E+(D−c)−(PL−2)D = E + (D - c) - (PL - 2), which is again E=c+PL−2E = c + PL - 2. The time of the part to PRINT is the sum of the times of the lines times their frequencies: (1+2+1+1+1)+E(1+1)+D(1+1+1+12+1)+(D−c)(2+1)+(PL−1)(1+2+2+1)(1 + 2 + 1 + 1 + 1) + E(1 + 1) + D(1 + 1 + 1 + 12 + 1) + (D - c)(2 + 1) + (PL - 1)(1 + 2 + 2 + 1). This is 6+2E+16D+3D−3c+6PL−6=6PL+2E+19D−3c6 + 2E + 16D + 3D - 3c + 6PL - 6 = 6PL + 2E + 19D - 3c. The values are found as follows. The candidates are the odd numbers from 55 to 35713571, so E=(3571−5)/2+1=1784E = (3571 - 5)/2 + 1 = 1784. The number 35713571 is the 500th prime, so the primes among the candidates are 498498 (without 22 and 33), and c=1784−498=1286c = 1784 - 498 = 1286. The count DD is the sum, over the candidates, of the number of the divisions of the candidate: the 498498 primes give 58125812 and the 12861286 composites give 37263726, which sum to 95389538. The largest divisor used is 6161. It is reached in the test of 35713571: the divisor 5959 gives q=60>59q = 60 > 59, since 3571=60⋅59+313571 = 60 \cdot 59 + 31, and the divisor 6161 gives q=58≤61q = 58 \le 61, since 3571=58⋅61+333571 = 58 \cdot 61 + 33. No candidate goes further, since each candidate n≤3571n \le 3571 has ⌊n/61⌋≤58\lfloor n/61 \rfloor \le 58. The printing part has the time 11 for the first ENT1, 2525 times (1+1+20⋅19+1+2+1)=25⋅386=9650(1 + 1 + 20 \cdot 19 + 1 + 2 + 1) = 25 \cdot 386 = 9650 for the lines, where 19=2+10+2+1+1+2+119 = 2 + 10 + 2 + 1 + 1 + 2 + 1 is the time of one column, and 11 for HLT, which is 96529652 in all. The sum 183932+9652=193584183932 + 9652 = 193584 is the time of the whole program when the waiting of JBUS is not counted. □\square

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 q≤P[k]q \le P[k], 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 by TAOCP.MIX.Asm.Ctx.expr from left to right, with no precedence. The symbol table is TAOCP.MIX.Asm.Syms, and the local symbols are read by TAOCP.MIX.Asm.localKind.
  • W-values are TAOCP.MIX.Asm.Ctx.wvalue, and an instruction line is TAOCP.MIX.Asm.Ctx.instr. An ALF line is TAOCP.MIX.Asm.alfWord.
  • The assembler has two passes, TAOCP.MIX.Asm.pass1 and the pass that makes the words. An error carries the index of its line, as TAOCP.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 00. The model checks that a value fits in b5b^5, 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 30003000 to 30093009, 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 b=64b = 64 and b=100b = 100.

  • TAOCP.S1_3_2.programM_correct and TAOCP.S1_3_2.programM_time say that for 1≤n<20001 \le n < 2000 the subroutine returns to the caller with the largest value in rA and its largest index in rI2\mathrm{rI}_2, and that it takes 5+5n+3A5 + 5n + 3A u, where AA is TAOCP.S1_3_2.changes. The proof uses the program logic of TAOCP/MIX/Logic/.
  • TAOCP.S1_3_2.programM_call adds the call JMP, for a total of 6+5n+3A6 + 5n + 3A u.
  • TAOCP.S1_3_2.programM_mean_time says that over the n!n! orders of nn distinct numbers, the mean time is 5n+2+3Hn5n + 2 + 3H_n. The proof uses the count of changes of Section 1.2.10.

The bound n<2000n < 2000 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 6n+3A6n + 3A 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 500500 primes, 95389538, 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.