Volume P7 · Machines, programs and their correctness
Solutions
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.
Contents
Solutions to Chapter 1: Machine words and number representations
Section 1.1: Positional notation
Exercise 1.1-1. Repeated division by gives the remainders , so . Grouping by threes from the right gives . Grouping by fours gives . Check: .
Exercise 1.1-2. The number is below and above , so the width is . The divisions are , , , . The digits are . Check: .
Exercise 1.1-3. Since , each , so . For this is the rule of casting out nines.
Exercise 1.1-4. The number is , because the other terms are not negative and .
Exercise 1.1-5. Each division reduces to . After divisions the number is , which is positive exactly when , that is . So the loop runs for , which is divisions.
Exercise 1.1-6. After the first digits have been read, the invariant is . For , , the empty sum. One step replaces by , which is , the sum for . At the end and is the value of the row.
Exercise 1.1-7. In binary , , . The bits in pairs are , which are the radix digits . So the row is . In decimal, , and .
Exercise 1.1-8. All rows of width number . The rows with the most significant digit have a row of width after it, so there are of them. The rows with a nonzero first digit number .
Exercise 1.1-9. Let be the first digits of the expansion by repeated multiplication, with the exact value . Then is an integer. Conversely, suppose is an integer with . Write in radix with digits . Then . Each multiplication step takes the integer part of as the digit and keeps the fraction , with the integer made of the rest of the digits. So after steps.
Exercise 1.1-10. Write for the list whose first element is . The recursion is and . It reads the digits from the least significant, and it must walk to the end of the list before the first addition, by a recursion of depth . For digits it uses multiplications and additions, the same as Horner evaluation on the reversed row.
Section 1.2: Signed numbers and overflow
Exercise 1.2-1. The row has . In twos complement the value is . In ones complement it is , the second zero.
Exercise 1.2-2. The row is . Check: inverting gives , and .
Exercise 1.2-3. The row of is . If as a value, then , so . In the range this gives or . The value of the second row is . So the only fixed values are and . The integer is not in the range, which is why the negation of overflows.
Exercise 1.2-4. Let be a row with the ones complement value . The inverted row is . If then , and since . The value of is . If then and , so the value of is .
Exercise 1.2-5. The value is or , and the second differs from by a multiple of .
Exercise 1.2-6. The range is to . The sum with and gives the pairs , , , with the sums . These are four pairs.
Exercise 1.2-7. Let and be the top bits of the operands, the carry into the top bit, and the top bit of the result. The carry out is when at least two of are . If the carry out is , and the overflow happens when the result is negative, , so the overflow holds iff , which is carry out. If the carry out is , and the overflow happens when the result is not negative, , so the overflow holds iff , which is carry out. If there is no overflow, and the carry out equals , so the two carries agree. In all cases the overflow holds iff the carry into the top bit differs from the carry out.
Exercise 1.2-8. The sum is for the magnitude , with no overflow iff , that is, . For the sum is , so the magnitude of the result is and the flag is set. For the sum is , and the magnitude of the result is .
Exercise 1.2-9. There are signs and rows of bytes, so words. Each nonzero value has one word and the value has two, so there are values.
Exercise 1.2-10. The sum of and has the magnitude and the sign of the larger. The adder must compare and to know which to subtract from which, or it must subtract and then repair a negative result. In twos complement the sum of the rows modulo is the answer for all sign combinations, by the theorem on arithmetic modulo . The adder needs no comparison.
Section 1.3: Bytes and fields
Exercise 1.3-1. The field has , , . The field has , , . The field has , , . The field has , , .
Exercise 1.3-2. The bytes of are and the sign of the field is , so the value is .
Exercise 1.3-3. If with , then and force , and then .
Exercise 1.3-4. The field has . The three low bytes of the source are . They fill the positions . As the sign of the target stays. The result is .
Exercise 1.3-5. By the definition of the store, the sign is the sign of unless .
Exercise 1.3-6. By the theorem "store keeps the rest", the store into a field changes the bytes at the positions of and no others, and it puts the byte at position of for the low bytes of the source. The two fields are disjoint and neither has the sign, so each store reads and writes only its own positions. The first store does not touch the positions of the second, and the second does not touch those of the first. The final word has the bytes of from , the bytes of from and the other bytes from , in either order.
Exercise 1.3-7. Let . The first term is a multiple of with . Since divides , . The other two terms add up to . So the sum is at most .
Exercise 1.3-8. Only the field has , because needs and . For this field , and the store formula gives , and . The magnitude does not change, and the sign of replaces the sign of .
Exercise 1.3-9. Let . The magnitude of the load is , and it is below , so . The store gives with and . Division of by gives , and division of the quotient by gives . So and the store gives back, with the old sign. For the load has the sign of , and the store takes the sign of its source, so the sign is also 's. The same result holds.
Exercise 1.3-10. The half word is the sign and . The test is , which says that , because .
Exercise 1.3-11. For each from to there are values of , so the number of fields is . The fields with are the fields with and and none other, one field. The fields that contain the sign are those with , which are .
Solutions to Chapter 2: A model machine and its instruction cycle
Section 2.1: The state and the step
Exercise 2.1-1. The run from is , so it visits all ten states, because and takes each value once for . The run from is , with the same cycle.
Exercise 2.1-2. A step must fix each element of . For we have for each , so no can be in . So , and then no run halts.
Exercise 2.1-3. Put . By the theorem on two runs in a row, . So the run of after steps and the run of after steps are the same sequence.
Exercise 2.1-4. The set of with is a set of natural numbers, and if it is not empty it has a least element, so the length is unique. If then is in the set and it is the least. If the length is then .
Exercise 2.1-5. Let , , and . The state is in but is not equal to it. The condition for fails, and the conclusion of the theorem fails.
Exercise 2.1-6. The first rule: the machine faults, halts, and the state shows the status fault, as TAOCP.MIX.State.step does for pc ≥ memSize. The second rule: the counter wraps to and the program continues from the first cell. The first rule is the one that makes a missing HLT an error that can be seen.
Exercise 2.1-7. Take . Then , and since also fixes , . So fixes each element of .
Exercise 2.1-8. The states are states in a set of , so two are equal by the pigeonhole principle, say with . By determinism for all , so the run is periodic from with a period that divides (it has the period ). The run repeats a state within the first states.
Exercise 2.1-9. Fetch: . Decode: and . Execute: do the effect of the operation with the address , then unless the instruction jumped.
Exercise 2.1-10. Let be the least index with on the cycle, and the period. The states are pairwise different: two states with indices below differ because the least index on a cycle is , and the states differ because is the least period. A state with an index below is not equal to a state on the cycle, since then it would be on the cycle. So there are different states, and .
Section 2.2: A small machine
Exercise 2.2-1. LDA 5 is , ADD 12 is , JN 3 is and HLT is .
Exercise 2.2-2. The word is JN . With it jumps, so . With it does not jump, and .
Exercise 2.2-3. With and the partial products are and , all in the range. The result is and the time is . The simulation gives the same.
Exercise 2.2-4. Put the data in the cells and and the code at to . The words are: cell 0: (LDA 12), cell 1: (SUB 13), cell 2: (JN 6), cell 3: (LDA 12), cell 4: (JMP 7), cell 6: (LDA 13), cell 7: (STA 14), cell 8: (HLT). The cell is not used. The difference is negative when is larger, and then control goes to the cell . For and : LDA , SUB , JN taken , LDA , STA , HLT , total . For and : LDA , SUB , JN not taken , LDA , JMP , STA , HLT , total . Both runs give .
Exercise 2.2-5. The cell holds . Code: cell 0: (LDA 12), cell 1: (JN 4), cell 2: (STA 13), cell 3: (HLT), cell 4: (LDA 11), cell 5: (SUB 12), cell 6: (STA 13), cell 7: (HLT). For the time is . For the path is with the time , and .
Exercise 2.2-6. The instructions of T write the cells and only: the STA instructions at the cells and have the addresses and . No instruction writes the cell or the cell . By induction on the steps, those two cells keep their values.
Exercise 2.2-7. In each pass the SUB puts in and STA stores it in the cell , so the cell is the value after the -th pass. The JZ never jumps, since . In each pass ADD adds to . After passes . The ADD of the pass gives . This is the first ADD that leaves the range, since . The fault comes in the sixth instruction of the pass , after instructions. The value of at that time is , so the SUB did not fault before.
Exercise 2.2-8. The pass number makes the partial product , and it leaves the range at the first with , that is . The fault occurs in the ADD of the pass when . The cell holds in the sign of , the last good partial product. For , : the second pass faults and . For , .
Exercise 2.2-9. The program: cell 0: , cell 1: , cell 2: , cell 3: , cell 4: , cell 5: , cell 6: , cell 7: , cell 8: . It is T with the cells and exchanged in the roles: counts down, and counts up by . The time is , which is for . The simulation gives and .
Exercise 2.2-10. A cell holds at most , and . So no program can have the value in a cell or in : a result outside is a fault. The largest power of that fits is .
Exercise 2.2-11. The accumulator has values and each of the cells has values. The counter has values for a running machine. The number of states is , which is about . A run that does not repeat a state has at most that many different states, so its length is less than this number. No computer can run so many steps, so the bound says nothing about the practical length of a run.
Section 2.3: Time and space
Exercise 2.3-1. For and the run executes the cells , each once, and no other cell. The costs of these cells are , with the sum . This is the time of the solution of Exercise 2.2-4.
Exercise 2.3-2. Each step costs , so . Each step costs at most , so .
Exercise 2.3-3. For : , , . The time is .
Exercise 2.3-4. The cell is executed times. Each is entered from the cell , so . The first four executions leave to the cell , and the last leaves to the cell . So , and , with the cell neither the start nor the end.
Exercise 2.3-5. A pass costs . The end costs . The time is .
Exercise 2.3-6. The time is and the mean of on is . By the linearity of the mean, the mean time is . A direct sum of divided by gives .
Exercise 2.3-7. Each instruction has . So the time , the sum over all instructions of the program.
Exercise 2.3-8. The memory has cells. The code takes , so at most cells remain for data.
Exercise 2.3-9. For : four times, three times, once, and each of , , , , and three times. At the node the flow in is from the cell and from the start, and the flow out is .
Exercise 2.3-10. The steps are split among addresses, . If all were less than , the sum would be less than . So some , and as is an integer, .
Exercise 2.3-11. The states along a run are the values of , and the memory. A halting run that does not repeat a state visits distinct states, so the number of steps is less than the number of states. If the run has more steps than this, two of its states are equal, and by determinism the run is periodic from then on, so it cannot halt.
Solutions to Chapter 3: MIX as a case study
Section 3.1: Registers and words
Exercise 3.1-1. The magnitude is and the sign is plus, so the value is .
Exercise 3.1-2. The largest word has five bytes of , so its value is . An index register has two bytes, so its largest value is .
Exercise 3.1-3. The true sum is . The magnitude modulo is and the sign is plus, so A . The sum is at least , so OV is on.
Exercise 3.1-4. The true sum is . There is no overflow, so OV stays on, since no instruction of this group switches it off. A gets the value zero with the sign of the old A, which is minus, so A .
Exercise 3.1-5. Let overflow. If and are not both positive and not both negative, the theorem on opposite signs says that there is no overflow. So they are both positive or both negative. In the first case , so the magnitude modulo is , and the sign is plus. In the second case the same holds for the magnitudes, and the result is .
Exercise 3.1-6. A word has signs and rows of bytes, so there are words. The two words and have the value , and each other word has its own value, so the number of values is . For : words and values.
Exercise 3.1-7. The address must fit in a magnitude below . For we have , so the addresses to do not fit. Since and , the least is .
Exercise 3.1-8. J holds the address of the next instruction, . Since , J holds .
Section 3.2: Instruction format and timing
Exercise 3.2-1. , , and . The word is .
Exercise 3.2-2. The address is with the sign plus, , , the field , and , a store of I2. The instruction is ST2 . It takes .
Exercise 3.2-3. For LDA the address is . The cell does not exist, so the model stops with the fault address. For ENTA the address is also , but the instruction does not read the memory, and A becomes . For LDA the address is , and the model stops with the fault address.
Exercise 3.2-4. LDA takes , MUL takes , STA takes and STX takes . The sum is units.
Exercise 3.2-5. Let with . Then and by the uniqueness of the division with remainder, so determines the pair. The pairs with number , one for each with values of . The largest code is .
Exercise 3.2-6. Write . When the quotient and the remainder are the quotient and the remainder of the division of the natural number by , so and by the division with remainder of P1. The theorem on division shows that , so both fit in a word.
Exercise 3.2-7. The product is . The high part is and the low part is . A and X .
Exercise 3.2-8. The dividend is and the divisor is , so the division is allowed. . A and X .
Exercise 3.2-9. LDA with the default field puts the whole word of the cell in A, by the law that loading the whole field gives the word. STA with puts the whole word of A in the cell, and by the law that storing the whole field replaces the word, the cell holds . This is the old contents.
Exercise 3.2-10. ENT1 is a fault: , so it does not fit in two bytes, and the model stops with the fault register. ENT1 is allowed. INC1 with I1 gives , which does not fit, so it is a fault of the same kind.
Section 3.3: Jumps, shifts, MOVE and conversion
Exercise 3.3-1. . J holds the address of the next cell, , so J .
Exercise 3.3-2. The value of A is . JAN needs , so no. JAZ needs , so yes. JAP needs , so no. JANN needs , so yes. JANZ needs , so no. JANP needs , so yes.
Exercise 3.3-3. In the first run OV is on. JNOV switches it off and does not jump, so control goes to the next cell. In the second run OV is off, so JNOV jumps to and sets J. OV stays off.
Exercise 3.3-4. The magnitude of A is . SLA drops the bytes and appends two zeros: . SRA drops the bytes and the byte and puts zeros on the left: .
Exercise 3.3-5. The ten bytes are . A turn right by moves the last three, , to the front: . So A and X . The signs stay.
Exercise 3.3-6. Write with . Then and , so . The floor of is . So the magnitude after the two shifts is . For it is , and for it is .
Exercise 3.3-7. By the definition, SRC by is the turn left by places. If is not a multiple of , this is the number in the claim. If is a multiple of , the turn left is by places, which has , so it is the identity by the theorem on rotations.
Exercise 3.3-8. Here and , so . The new cell holds the old cell , so the cells to hold . The cells and are not changed. The six cells are . I1 . The time is .
Exercise 3.3-9. The fragment is ENT1 followed by MOVE . Here , so each of the cells gets the word of the cell . The time is .
Exercise 3.3-10. The digits of are . A and X . The time is .
Exercise 3.3-11. The magnitude of A is below , so CHAR writes the digits of in ten places, and each byte is , which is modulo . NUM reads and forms , as in the theorem. For the second part take A and X . NUM gives A . CHAR then gives A and X , which is not the old A X.
Exercise 3.3-12. One pass takes . I1 takes the values at the start of the passes, and the last J1P does not jump, so there are passes. The time is .
Section 3.4: Three programs
Exercise 3.4-1. The time is . For it is and for it is .
Exercise 3.4-2. The table is , . The run reads first.
| step | cell | instruction | A | I1 | clock |
|---|---|---|---|---|---|
| 1 | ENTA | ||||
| 2 | LD1 | ||||
| 3 | J1Z | ||||
| 4 | ADD | ||||
| 5 | DEC1 | ||||
| 6 | J1P | ||||
| 7 | ADD | ||||
| 8 | DEC1 | ||||
| 9 | J1P | ||||
| 10 | STA | ||||
| 11 | HLT |
The sum is and the time is .
Exercise 3.4-3. S reads first and A becomes . It then adds and the true sum is . The magnitude modulo is , the sign is plus (the sign of the old A, as the sum is positive), and the flag is true. So A and OV is on. For the table the first word read is , so A . Then the true sum is . The magnitude modulo is and the sign is minus. So A and OV is on.
Exercise 3.4-4. The word has digits, so . The loop runs passes, with X at the head of each pass after the first. The time is .
Exercise 3.4-5. The divisor makes each pass halve X. The loop ends at the first with , that is . So I2 counts the bits of in radix , with for . The invariant and the time hold with the number of bits, since the pass has the same cost. For , , so and the time is .
Exercise 3.4-6. DIV overflows when . At each DIV the register A holds (it is set by ENTA at the start and by SRAX at the end of each pass), and , so there is no overflow and OV stays as it was. The count I2 is at most for a word below , since , and so it fits in two bytes. The effective address of each instruction is the address of its word, since the index is and the cells are in to . The shift count is not negative. So no fault occurs.
Exercise 3.4-7. C takes and L takes . A MOVE of words is allowed since .
Exercise 3.4-8. The loop L runs the steps for (the offset in the block), and step copies the cell to the cell . If , a cell with can be written only by the step . The step reads it first, and the steps with a smaller come later and read other cells. The step with comes after the step . So a cell is read before it is written, and the copy is correct. If the blocks are disjoint and the copy is correct. If , take . We have . The step writes the cell first, because it is the first step, and the step reads it later. It reads the new value, not the old one, so the copy is wrong. So L is correct exactly when or . MOVE is correct exactly when or , by the theorem of the section. A block with is correct in both. A copy to a higher address that overlaps must run from the top down, as L does. A copy to a lower address must run from the bottom up, as MOVE does.
Exercise 3.4-9. The program: ENTA , ENT1 , a loop of ADD , INC1 , J1N to the ADD, then STA and HLT. With I1 the ADD reads the cells . The time is . For the program runs the ADD once with I1 , which adds the word of the cell , that is not part of the table, and then INC1 gives and the jump is not taken. The result is wrong. The loop needs the test before the first pass for .
Exercise 3.4-10. The new instructions are STA (), LDX () and ENTA (), which is in place of . A pass takes units, and the total is . The stored word has the sign plus and the value of the quotient, so X gets the quotient, and ENTA clears A for the next DIV. For the time is .
This chapter has a full solution of each exercise of the volume, one file per chapter. Exercise of Section is called Exercise -. Try an exercise for some time before you read its solution. Each solution gives the steps that a reader at the level of the rating needs, and a check by a small case where one is useful. Every number in a solution was computed by a program.
The source of this page. Something is wrong? Report it.