taocp

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

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
  1. Solutions to Chapter 1: Machine words and number representations
  2. Solutions to Chapter 2: A model machine and its instruction cycle
  3. Solutions to Chapter 3: MIX as a case study

Solutions to Chapter 1: Machine words and number representations

Section 1.1: Positional notation

Exercise 1.1-1. Repeated division by 22 gives the remainders 0,0,0,1,0,1,1,1,1,10,0,0,1,0,1,1,1,1,1, so 1000=111110100021000 = 1111101000_2. Grouping by threes from the right gives 001 111 101 000=17508001\,111\,101\,000 = 1750_8. Grouping by fours gives 0011 1110 1000=3E8160011\,1110\,1000 = 3\mathrm{E}8_{16}. Check: 3⋅256+14⋅16+8=10003 \cdot 256 + 14 \cdot 16 + 8 = 1000.

Exercise 1.1-2. The number 10610^6 is below 644=1677721664^4 = 16777216 and above 643=26214464^3 = 262144, so the width is 44. The divisions are 106=15625⋅64+010^6 = 15625 \cdot 64 + 0, 15625=244⋅64+915625 = 244 \cdot 64 + 9, 244=3⋅64+52244 = 3 \cdot 64 + 52, 3=0⋅64+33 = 0 \cdot 64 + 3. The digits are 3,52,9,03, 52, 9, 0. Check: 3⋅262144+52⋅4096+9⋅64=786432+212992+576=10000003 \cdot 262144 + 52 \cdot 4096 + 9 \cdot 64 = 786432 + 212992 + 576 = 1000000.

Exercise 1.1-3. Since b≡1(modb−1)b \equiv 1 \pmod{b-1}, each bi≡1b^i \equiv 1, so ∑dibi≡∑di(modb−1)\sum d_i b^i \equiv \sum d_i \pmod{b - 1}. For b=10b = 10 this is the rule of casting out nines.

Exercise 1.1-4. The number is ∑i≤k−1dibi≥dk−1bk−1≥bk−1\sum_{i \le k-1} d_i b^i \ge d_{k-1} b^{k-1} \ge b^{k-1}, because the other terms are not negative and dk−1≥1d_{k-1} \ge 1.

Exercise 1.1-5. Each division reduces nn to ⌊n/b⌋\lfloor n/b \rfloor. After jj divisions the number is ⌊n/bj⌋\lfloor n / b^j \rfloor, which is positive exactly when bj≤nb^j \le n, that is j≤⌊log⁡bn⌋j \le \lfloor \log_b n \rfloor. So the loop runs for j=0,…,⌊log⁡bn⌋j = 0, \dots, \lfloor \log_b n \rfloor, which is ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1 divisions.

Exercise 1.1-6. After the first mm digits dk−1,…,dk−md_{k-1}, \dots, d_{k-m} have been read, the invariant is v=∑i=1mdk−ibm−iv = \sum_{i=1}^{m} d_{k-i} b^{m-i}. For m=0m = 0, v=0v = 0, the empty sum. One step replaces vv by vb+dk−m−1vb + d_{k-m-1}, which is ∑i=1mdk−ibm+1−i+dk−m−1b0\sum_{i=1}^{m} d_{k-i} b^{m+1-i} + d_{k-m-1} b^0, the sum for m+1m + 1. At the end m=km = k and vv is the value of the row.

Exercise 1.1-7. In binary 3=00113 = 0011, A=1010\mathrm{A} = 1010, F=1111\mathrm{F} = 1111. The bits 0011 1010 11110011\,1010\,1111 in pairs are 00,11,10,10,11,1100, 11, 10, 10, 11, 11, which are the radix 44 digits 0,3,2,2,3,30, 3, 2, 2, 3, 3. So the row is 32233432233_4. In decimal, 3⋅256+10⋅16+15=9433 \cdot 256 + 10 \cdot 16 + 15 = 943, and 3⋅256+2⋅64+2⋅16+3⋅4+3=9433 \cdot 256 + 2 \cdot 64 + 2 \cdot 16 + 3 \cdot 4 + 3 = 943.

Exercise 1.1-8. All rows of width 55 number 645=107374182464^5 = 1073741824. The rows with the most significant digit 00 have a row of width 44 after it, so there are 64464^4 of them. The rows with a nonzero first digit number 645−644=63⋅644=105696460864^5 - 64^4 = 63 \cdot 64^4 = 1056964608.

Exercise 1.1-9. Let x=∑i=1mdib−ix = \sum_{i=1}^{m} d_i b^{-i} be the first mm digits of the expansion by repeated multiplication, with the exact value xx. Then xbm=∑dibm−ix b^m = \sum d_i b^{m-i} is an integer. Conversely, suppose xbm=Nx b^m = N is an integer with 0≤N<bm0 \le N < b^m. Write NN in radix bb with mm digits d1…dmd_1 \dots d_m. Then x=N/bm=∑dib−ix = N / b^m = \sum d_i b^{-i}. Each multiplication step takes the integer part of bxj−1b x_{j-1} as the digit djd_j and keeps the fraction xjx_j, with xjbm−j=x_j b^{m - j} = the integer made of the rest of the digits. So xm=0x_m = 0 after mm steps.

Exercise 1.1-10. Write v(L)v(L) for the list LL whose first element is d0d_0. The recursion is v([d])=dv([d]) = d and v(d::L)=d+b⋅v(L)v(d :: L) = d + b \cdot v(L). 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 kk. For kk digits it uses k−1k - 1 multiplications and k−1k - 1 additions, the same as Horner evaluation on the reversed row.

Section 1.2: Signed numbers and overflow

Exercise 1.2-1. The row 1111111111111111 has u=255u = 255. In twos complement the value is 255−256=−1255 - 256 = -1. In ones complement it is 255−255=0255 - 255 = 0, the second zero.

Exercise 1.2-2. The row is 256−100=156=10011100256 - 100 = 156 = 10011100. Check: inverting 100=01100100100 = 01100100 gives 10011011=15510011011 = 155, and 155+1=156155 + 1 = 156.

Exercise 1.2-3. The row of −x-x is −u mod 2n-u \bmod 2^n. If −x=x-x = x as a value, then 2u≡0(mod2n)2u \equiv 0 \pmod{2^n}, so u≡0(mod2n−1)u \equiv 0 \pmod{2^{n-1}}. In the range 0≤u<2n0 \le u < 2^n this gives u=0u = 0 or u=2n−1u = 2^{n-1}. The value of the second row is −2n−1-2^{n-1}. So the only fixed values are 00 and −2n−1-2^{n-1}. The integer 2n−12^{n-1} is not in the range, which is why the negation of −2n−1-2^{n-1} overflows.

Exercise 1.2-4. Let uu be a row with the ones complement value xx. The inverted row is u′=2n−1−uu' = 2^n - 1 - u. If u<2n−1u < 2^{n-1} then x=ux = u, and u′≥2n−1u' \ge 2^{n-1} since u≤2n−1−1u \le 2^{n-1} - 1. The value of u′u' is u′−(2n−1)=−u=−xu' - (2^n - 1) = -u = -x. If u≥2n−1u \ge 2^{n-1} then x=u−(2n−1)x = u - (2^n - 1) and u′≤2n−1−1u' \le 2^{n-1} - 1, so the value of u′u' is u′=2n−1−u=−xu' = 2^n - 1 - u = -x.

Exercise 1.2-5. The value is uu or u−(2n−1)u - (2^n - 1), and the second differs from uu by a multiple of 2n−12^n - 1.

Exercise 1.2-6. The range is −4-4 to 33. The sum x+y≥4x + y \ge 4 with x≥y≥0x \ge y \ge 0 and x≤3x \le 3 gives the pairs (2,2)(2,2), (3,1)(3,1), (3,2)(3,2), (3,3)(3,3) with the sums 4,4,5,64, 4, 5, 6. These are four pairs.

Exercise 1.2-7. Let aa and bb be the top bits of the operands, cc the carry into the top bit, and r=a⊕b⊕cr = a \oplus b \oplus c the top bit of the result. The carry out is 11 when at least two of a,b,ca, b, c are 11. If a=b=0a = b = 0 the carry out is 00, and the overflow happens when the result is negative, r=c=1r = c = 1, so the overflow holds iff c≠0c \ne 0, which is c≠c \ne carry out. If a=b=1a = b = 1 the carry out is 11, and the overflow happens when the result is not negative, r=c=0r = c = 0, so the overflow holds iff c≠1c \ne 1, which is c≠c \ne carry out. If a≠ba \ne b there is no overflow, and the carry out equals cc, 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 w+ww + w is 2m2m for the magnitude mm, with no overflow iff 2m<6452m < 64^5, that is, m≤536870911m \le 536870911. For m=229=536870912m = 2^{29} = 536870912 the sum is 230=6452^{30} = 64^5, so the magnitude of the result is 00 and the flag is set. For m=109<645m = 10^9 < 64^5 the sum is 2⋅109≥6452 \cdot 10^9 \ge 64^5, and the magnitude of the result is 2⋅109−645=9262581762 \cdot 10^9 - 64^5 = 926258176.

Exercise 1.2-9. There are 22 signs and 64564^5 rows of bytes, so 2⋅645=21474836482 \cdot 64^5 = 2147483648 words. Each nonzero value has one word and the value 00 has two, so there are 2147483648−1=21474836472147483648 - 1 = 2147483647 values.

Exercise 1.2-10. The sum of +m1+m_1 and −m2-m_2 has the magnitude ∣m1−m2∣|m_1 - m_2| and the sign of the larger. The adder must compare m1m_1 and m2m_2 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 2n2^n is the answer for all sign combinations, by the theorem on arithmetic modulo 2n2^n. The adder needs no comparison.

Section 1.3: Bytes and fields

Exercise 1.3-1. The field (1:1)(1:1) has σ=4\sigma = 4, ℓ=1\ell = 1, F=9F = 9. The field (1:3)(1:3) has σ=2\sigma = 2, ℓ=3\ell = 3, F=11F = 11. The field (2:5)(2:5) has σ=0\sigma = 0, ℓ=4\ell = 4, F=21F = 21. The field (0:3)(0:3) has σ=2\sigma = 2, ℓ=3\ell = 3, F=3F = 3.

Exercise 1.3-2. The bytes of (1:2)(1:2) are 3,73, 7 and the sign of the field is ++, so the value is 3⋅64+7=1993 \cdot 64 + 7 = 199.

Exercise 1.3-3. If 8L+R=8L′+R′8L + R = 8L' + R' with 0≤R,R′≤50 \le R, R' \le 5, then R≡R′(mod8)R \equiv R' \pmod 8 and ∣R−R′∣<8|R - R'| < 8 force R=R′R = R', and then L=L′L = L'.

Exercise 1.3-4. The field (1:3)(1:3) has ℓ=3\ell = 3. The three low bytes of the source are 3,4,53, 4, 5. They fill the positions 1,2,31, 2, 3. As L=1L = 1 the sign of the target stays. The result is − 3 4 5 9 9-\,3\,4\,5\,9\,9.

Exercise 1.3-5. By the definition of the store, the sign is the sign of dd unless L=0L = 0.

Exercise 1.3-6. By the theorem "store keeps the rest", the store into a field Φ\Phi changes the bytes at the positions of Φ\Phi and no others, and it puts the byte sjs_j at position jj of Φ\Phi 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 Φ\Phi from ss, the bytes of Φ′\Phi' from s′s' and the other bytes from dd, in either order.

Exercise 1.3-7. Let T=σ+ℓ≤5T = \sigma + \ell \le 5. The first term A=⌊md/bT⌋bTA = \lfloor m_d / b^{T} \rfloor b^{T} is a multiple of bTb^{T} with A≤md≤b5−1A \le m_d \le b^5 - 1. Since bTb^T divides b5b^5, A≤b5−bTA \le b^5 - b^T. The other two terms add up to Fbσ+C≤bT−1F b^{\sigma} + C \le b^{T} - 1. So the sum is at most b5−1b^5 - 1.

Exercise 1.3-8. Only the field (0:0)(0:0) has ℓ=0\ell = 0, because ℓ=R+1−max⁡(L,1)=0\ell = R + 1 - \max(L, 1) = 0 needs R=0R = 0 and L=0L = 0. For this field σ=5\sigma = 5, and the store formula gives A=⌊md/b5⌋b5=0A = \lfloor m_d / b^5 \rfloor b^5 = 0, F=ms mod 1=0F = m_s \bmod 1 = 0 and C=md mod b5=mdC = m_d \bmod b^5 = m_d. The magnitude does not change, and the sign of ss replaces the sign of dd.

Exercise 1.3-9. Let L≥1L \ge 1. The magnitude of the load is φ=⌊md/bσ⌋ mod bℓ\varphi = \lfloor m_d / b^{\sigma} \rfloor \bmod b^{\ell}, and it is below bℓb^{\ell}, so φ mod bℓ=φ\varphi \bmod b^{\ell} = \varphi. The store gives A+φbσ+CA + \varphi b^{\sigma} + C with A=⌊md/bσ+ℓ⌋bσ+ℓA = \lfloor m_d / b^{\sigma+\ell} \rfloor b^{\sigma+\ell} and C=md mod bσC = m_d \bmod b^{\sigma}. Division of mdm_d by bσb^{\sigma} gives md=bσ⌊md/bσ⌋+Cm_d = b^{\sigma} \lfloor m_d / b^{\sigma} \rfloor + C, and division of the quotient by bℓb^{\ell} gives ⌊md/bσ⌋=bℓ⌊md/bσ+ℓ⌋+φ\lfloor m_d / b^{\sigma} \rfloor = b^{\ell} \lfloor m_d / b^{\sigma+\ell} \rfloor + \varphi. So md=A+φbσ+Cm_d = A + \varphi b^{\sigma} + C and the store gives mdm_d back, with the old sign. For L=0L = 0 the load has the sign of dd, and the store takes the sign of its source, so the sign is also dd's. The same result holds.

Exercise 1.3-10. The half word is the sign and b4b+b5=m mod b2b_4 b + b_5 = m \bmod b^2. The test is ⌊m/b2⌋=0\lfloor m / b^2 \rfloor = 0, which says that b1=b2=b3=0b_1 = b_2 = b_3 = 0, because ⌊m/b2⌋=(b1b+b2)b+b3\lfloor m / b^2 \rfloor = (b_1 b + b_2) b + b_3.

Exercise 1.3-11. For each RR from 00 to 55 there are R+1R + 1 values of LL, so the number of fields is 1+2+3+4+5+6=211 + 2 + 3 + 4 + 5 + 6 = 21. The fields with ℓ=0\ell = 0 are the fields with R=0R = 0 and L=0L = 0 and none other, one field. The fields that contain the sign are those with L=0L = 0, which are 66.

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 00 is 0,3,6,9,2,5,8,1,4,7,00, 3, 6, 9, 2, 5, 8, 1, 4, 7, 0, so it visits all ten states, because gcd⁡(3,10)=1\gcd(3, 10) = 1 and 3k mod 103k \bmod 10 takes each value once for k=0,…,9k = 0, \dots, 9. The run from 55 is 5,8,1,4,7,0,3,6,9,2,55, 8, 1, 4, 7, 0, 3, 6, 9, 2, 5, with the same cycle.

Exercise 2.1-2. A step must fix each element of Ω\Omega. For f(n)=n+1f(n) = n + 1 we have f(n)≠nf(n) \ne n for each nn, so no nn can be in Ω\Omega. So Ω=∅\Omega = \emptyset, and then no run halts.

Exercise 2.1-3. Put m≥0m \ge 0. By the theorem on two runs in a row, fj+m(q)=fm(fj(q))=fm(fj′(q′))=fj′+m(q′)f^{j+m}(q) = f^m(f^j(q)) = f^m(f^{j'}(q')) = f^{j'+m}(q'). So the run of qq after jj steps and the run of q′q' after j′j' steps are the same sequence.

Exercise 2.1-4. The set of kk with fk(q)∈Ωf^k(q) \in \Omega is a set of natural numbers, and if it is not empty it has a least element, so the length is unique. If q∈Ωq \in \Omega then k=0k = 0 is in the set and it is the least. If the length is 00 then f0(q)=q∈Ωf^0(q) = q \in \Omega.

Exercise 2.1-5. Let Q={0,1}Q = \{0, 1\}, Ω={0}\Omega = \{0\}, f(0)=1f(0) = 1 and f(1)=0f(1) = 0. The state f0(0)=0f^0(0) = 0 is in Ω\Omega but f1(0)=1f^1(0) = 1 is not equal to it. The condition f(q)=qf(q) = q for q∈Ωq \in \Omega 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 00 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 q∈Ωq \in \Omega. Then f(q)=qf(q) = q, and since gg also fixes Ω\Omega, g(f(q))=g(q)=qg(f(q)) = g(q) = q. So g∘fg \circ f fixes each element of Ω\Omega.

Exercise 2.1-8. The states q0,q1,…,qnq_0, q_1, \dots, q_n are n+1n + 1 states in a set of nn, so two are equal by the pigeonhole principle, say qi=qjq_i = q_j with i<j≤ni < j \le n. By determinism qi+m=qj+mq_{i+m} = q_{j+m} for all mm, so the run is periodic from ii with a period that divides j−ij - i (it has the period j−ij - i). The run repeats a state within the first n+1n + 1 states.

Exercise 2.1-9. Fetch: w←M[p]w \leftarrow M[p]. Decode: op←⌊w/100⌋op \leftarrow \lfloor w / 100 \rfloor and a←w mod 100a \leftarrow w \bmod 100. Execute: do the effect of the operation opop with the address aa, then p←p+1p \leftarrow p + 1 unless the instruction jumped.

Exercise 2.1-10. Let μ\mu be the least index with qμq_\mu on the cycle, and λ\lambda the period. The states q0,…,qμ+λ−1q_0, \dots, q_{\mu + \lambda - 1} are pairwise different: two states with indices below μ\mu differ because the least index on a cycle is μ\mu, and the states qμ,…,qμ+λ−1q_\mu, \dots, q_{\mu+\lambda-1} differ because λ\lambda is the least period. A state with an index below μ\mu is not equal to a state on the cycle, since then it would be on the cycle. So there are μ+λ\mu + \lambda different states, and μ+λ≤n\mu + \lambda \le n.

Section 2.2: A small machine

Exercise 2.2-1. LDA 5 is 105105, ADD 12 is 312312, JN 3 is 703703 and HLT is 00.

Exercise 2.2-2. The word 709709 is JN 99. With A=−4A = -4 it jumps, so p←9p \leftarrow 9. With A=0A = 0 it does not jump, and p←p+1p \leftarrow p + 1.

Exercise 2.2-3. With x=−3x = -3 and y=2y = 2 the partial products are −3-3 and −6-6, all in the range. The result is M[14]=−6M[14] = -6 and the time is 14⋅2+4=3214 \cdot 2 + 4 = 32. The simulation gives the same.

Exercise 2.2-4. Put the data in the cells 1212 and 1313 and the code at 00 to 88. The words are: cell 0: 112112 (LDA 12), cell 1: 413413 (SUB 13), cell 2: 706706 (JN 6), cell 3: 112112 (LDA 12), cell 4: 507507 (JMP 7), cell 6: 113113 (LDA 13), cell 7: 214214 (STA 14), cell 8: 00 (HLT). The cell 55 is not used. The difference M[12]−M[13]M[12] - M[13] is negative when M[13]M[13] is larger, and then control goes to the cell 66. For M[12]=4M[12] = 4 and M[13]=9M[13] = 9: LDA 22, SUB 22, JN taken 11, LDA 22, STA 22, HLT 11, total 1010. For M[12]=9M[12] = 9 and M[13]=4M[13] = 4: LDA 22, SUB 22, JN not taken 11, LDA 22, JMP 11, STA 22, HLT 11, total 1111. Both runs give M[14]=9M[14] = 9.

Exercise 2.2-5. The cell 1111 holds 00. Code: cell 0: 112112 (LDA 12), cell 1: 704704 (JN 4), cell 2: 213213 (STA 13), cell 3: 00 (HLT), cell 4: 111111 (LDA 11), cell 5: 412412 (SUB 12), cell 6: 213213 (STA 13), cell 7: 00 (HLT). For M[12]=5M[12] = 5 the time is 2+1+2+1=62 + 1 + 2 + 1 = 6. For M[12]=−5M[12] = -5 the path is 0,1,4,5,6,70, 1, 4, 5, 6, 7 with the time 2+1+2+2+2+1=102 + 1 + 2 + 2 + 2 + 1 = 10, and M[13]=5M[13] = 5.

Exercise 2.2-6. The instructions of T write the cells 1313 and 1414 only: the STA instructions at the cells 33 and 66 have the addresses 1313 and 1414. No instruction writes the cell 1212 or the cell 1515. By induction on the steps, those two cells keep their values.

Exercise 2.2-7. In each pass the SUB puts r−1r - 1 in AA and STA stores it in the cell 1313, so the cell 1313 is the value −1−k-1 - k after the kk-th pass. The JZ never jumps, since r<0r < 0. In each pass ADD adds 77 to M[14]M[14]. After kk passes M[14]=7kM[14] = 7k. The ADD of the pass k=143k = 143 gives A=7⋅143=1001>999A = 7 \cdot 143 = 1001 > 999. This is the first ADD that leaves the range, since 7⋅142=9947 \cdot 142 = 994. The fault comes in the sixth instruction of the pass 143143, after 142⋅8+6=1142142 \cdot 8 + 6 = 1142 instructions. The value of rr at that time is −143-143, so the SUB did not fault before.

Exercise 2.2-8. The pass number kk makes the partial product xkx k, and it leaves the range at the first kk with ∣x∣k>999|x| k > 999, that is k=⌊999/∣x∣⌋+1k = \lfloor 999 / |x| \rfloor + 1. The fault occurs in the ADD of the pass kk when k≤yk \le y. The cell 1414 holds ∣x∣(k−1)|x| (k - 1) in the sign of xx, the last good partial product. For x=600x = 600, k=2k = 2: the second pass faults and M[14]=600M[14] = 600. For x=7x = 7, k=143k = 143.

Exercise 2.2-9. The program: cell 0: 112112, cell 1: 608608, cell 2: 415415, cell 3: 212212, cell 4: 113113, cell 5: 315315, cell 6: 213213, cell 7: 500500, cell 8: 00. It is T with the cells 1212 and 1313 exchanged in the roles: M[12]M[12] counts down, and M[13]M[13] counts up by M[15]=1M[15] = 1. The time is 14n+414 n + 4, which is 7474 for n=5n = 5. The simulation gives 7474 and M[13]=5M[13] = 5.

Exercise 2.2-10. A cell holds at most 999999, and 29=512≤999<1024=2102^{9} = 512 \le 999 < 1024 = 2^{10}. So no program can have the value 2202^{20} in a cell or in AA: a result outside [−999,999][-999, 999] is a fault. The largest power of 22 that fits is 29=5122^{9} = 512.

Exercise 2.2-11. The accumulator has 19991999 values and each of the 1616 cells has 19991999 values. The counter pp has 1616 values for a running machine. The number of states is 16⋅19991716 \cdot 1999^{17}, which is about 2.08⋅10572.08 \cdot 10^{57}. 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 M[12]=9M[12] = 9 and M[13]=4M[13] = 4 the run executes the cells 0,1,2,3,4,7,80, 1, 2, 3, 4, 7, 8, each once, and no other cell. The costs of these cells are 2,2,1,2,1,2,12, 2, 1, 2, 1, 2, 1, with the sum 1111. This is the time of the solution of Exercise 2.2-4.

Exercise 2.3-2. Each step costs τ≥1\tau \ge 1, so T≥mT \ge m. Each step costs at most 22, so T≤2mT \le 2m.

Exercise 2.3-3. For y=4y = 4: N0=N1=5N_0 = N_1 = 5, N2=⋯=N7=4N_2 = \dots = N_7 = 4, N8=1N_8 = 1. The time is 2⋅5+1⋅5+4⋅(2+2+2+2+2+1)+1=10+5+44+1=60=14⋅4+42 \cdot 5 + 1 \cdot 5 + 4 \cdot (2 + 2 + 2 + 2 + 2 + 1) + 1 = 10 + 5 + 44 + 1 = 60 = 14 \cdot 4 + 4.

Exercise 2.3-4. The cell 11 is executed 55 times. Each is entered from the cell 00, so in(1)=5in(1) = 5. The first four executions leave to the cell 22, and the last leaves to the cell 88. So out(1)=4+1=5out(1) = 4 + 1 = 5, and in(1)=out(1)in(1) = out(1), with the cell 11 neither the start nor the end.

Exercise 2.3-5. A pass costs 4+1+3+4+4+3+4+1=244 + 1 + 3 + 4 + 4 + 3 + 4 + 1 = 24. The end costs 4+1+1=64 + 1 + 1 = 6. The time is 24y+624 y + 6.

Exercise 2.3-6. The time is 14y+414 y + 4 and the mean of yy on 0,…,90, \dots, 9 is 4.54.5. By the linearity of the mean, the mean time is 14⋅4.5+4=6714 \cdot 4.5 + 4 = 67. A direct sum of 4,18,…,1304, 18, \dots, 130 divided by 1010 gives 6767.

Exercise 2.3-7. Each instruction has Na≤1N_a \le 1. So the time ∑aNaτa≤∑aτa\sum_a N_a \tau_a \le \sum_a \tau_a, the sum over all instructions of the program.

Exercise 2.3-8. The memory has 1616 cells. The code takes 99, so at most 77 cells remain for data.

Exercise 2.3-9. For y=3y = 3: 0→10 \to 1 four times, 1→21 \to 2 three times, 1→81 \to 8 once, and each of 2→32 \to 3, 3→43 \to 4, 4→54 \to 5, 5→65 \to 6, 6→76 \to 7 and 7→07 \to 0 three times. At the node 00 the flow in is 33 from the cell 77 and 11 from the start, and the flow out is 44.

Exercise 2.3-10. The mm steps are split among nn addresses, ∑aNa=m\sum_a N_a = m. If all NaN_a were less than m/nm / n, the sum would be less than mm. So some Na≥m/nN_a \ge m / n, and as NaN_a is an integer, Na≥⌈m/n⌉N_a \ge \lceil m / n \rceil.

Exercise 2.3-11. The states along a run are the values of AA, pp 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 16⋅19991716 \cdot 1999^{17} 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 2⋅644+1=33554432+1=335544332 \cdot 64^4 + 1 = 33554432 + 1 = 33554433 and the sign is plus, so the value is 3355443333554433.

Exercise 3.1-2. The largest word has five bytes of 6363, so its value is 645−1=107374182364^5 - 1 = 1073741823. An index register has two bytes, so its largest value is 642−1=409564^2 - 1 = 4095.

Exercise 3.1-3. The true sum is 1073741823+2=1073741825=645+11073741823 + 2 = 1073741825 = 64^5 + 1. The magnitude modulo 64564^5 is 11 and the sign is plus, so A =+1= +1. The sum is at least 64564^5, so OV is on.

Exercise 3.1-4. The true sum is −5+5=0-5 + 5 = 0. 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 =−0= -0.

Exercise 3.1-5. Let x+vx + v overflow. If xx and vv 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 b5≤x+v≤2(b5−1)<2b5b^5 \le x + v \le 2(b^5 - 1) < 2 b^5, so the magnitude modulo b5b^5 is x+v−b5x + v - b^5, and the sign is plus. In the second case the same holds for the magnitudes, and the result is −(∣x∣+∣v∣−b5)=x+v+b5-(|x| + |v| - b^5) = x + v + b^5.

Exercise 3.1-6. A word has 22 signs and b5b^5 rows of bytes, so there are 2b52 b^5 words. The two words +0+0 and −0-0 have the value 00, and each other word has its own value, so the number of values is 2b5−12 b^5 - 1. For b=64b = 64: 2⋅230=21474836482 \cdot 2^{30} = 2147483648 words and 21474836472147483647 values.

Exercise 3.1-7. The address 39993999 must fit in a magnitude below b2b^2. For b=63b = 63 we have 632=3969<400063^2 = 3969 < 4000, so the addresses 39693969 to 39993999 do not fit. Since 642=4096≥400064^2 = 4096 \ge 4000 and 632<400063^2 < 4000, the least bb is 6464.

Exercise 3.1-8. J holds the address of the next instruction, 30113011. Since 3011=47⋅64+33011 = 47 \cdot 64 + 3, J holds + ∣ 47 ∣ 3+\,|\,47\,|\,3.

Section 3.2: Instruction format and timing

Exercise 3.2-1. AA=500=7⋅64+52AA = 500 = 7 \cdot 64 + 52, I=1I = 1, F=8⋅1+2=10F = 8 \cdot 1 + 2 = 10 and C=8+3=11C = 8 + 3 = 11. The word is + ∣ 7 ∣ 52 ∣ 1 ∣ 10 ∣ 11+\,|\,7\,|\,52\,|\,1\,|\,10\,|\,11.

Exercise 3.2-2. The address is 1⋅64+2=661 \cdot 64 + 2 = 66 with the sign plus, I=0I = 0, F=5F = 5, the field (0:5)(0:5), and C=26=24+2C = 26 = 24 + 2, a store of I2. The instruction is ST2 6666. It takes 2u2u.

Exercise 3.2-3. For LDA 20,220,2 the address is M=20−30=−10M = 20 - 30 = -10. The cell −10-10 does not exist, so the model stops with the fault address. For ENTA 20,220,2 the address is also −10-10, but the instruction does not read the memory, and A becomes −10-10. For LDA −20,2-20,2 the address is −50-50, and the model stops with the fault address.

Exercise 3.2-4. LDA takes 22, MUL takes 1010, STA takes 22 and STX takes 22. The sum is 1616 units.

Exercise 3.2-5. Let F=8L+RF = 8L + R with 0≤R≤5<80 \le R \le 5 < 8. Then L=⌊F/8⌋L = \lfloor F / 8 \rfloor and R=F mod 8R = F \bmod 8 by the uniqueness of the division with remainder, so FF determines the pair. The pairs with 0≤L≤R≤50 \le L \le R \le 5 number 6+5+4+3+2+1=216 + 5 + 4 + 3 + 2 + 1 = 21, one for each LL with 6−L6 - L values of RR. The largest code is 8⋅5+5=458 \cdot 5 + 5 = 45.

Exercise 3.2-6. Write N=∣a∣ b5+∣x∣N = |a|\,b^5 + |x|. When ∣v∣>∣a∣|v| > |a| the quotient q=⌊N/∣v∣⌋q = \lfloor N / |v| \rfloor and the remainder r=N mod ∣v∣r = N \bmod |v| are the quotient and the remainder of the division of the natural number NN by ∣v∣|v|, so N=q∣v∣+rN = q|v| + r and 0≤r<∣v∣0 \le r < |v| by the division with remainder of P1. The theorem on division shows that q<b5q < b^5, so both fit in a word.

Exercise 3.2-7. The product is 5⋅644⋅64=5⋅6455 \cdot 64^4 \cdot 64 = 5 \cdot 64^5. The high part is 55 and the low part is 00. A =+5= +5 and X =+0= +0.

Exercise 3.2-8. The dividend is 0⋅645+1000=10000 \cdot 64^5 + 1000 = 1000 and the divisor is 64>064 > 0, so the division is allowed. 1000=15⋅64+401000 = 15 \cdot 64 + 40. A =+15= +15 and X =+40= +40.

Exercise 3.2-9. LDA mm with the default field (0:5)(0:5) puts the whole word ww of the cell in A, by the law that loading the whole field gives the word. STA mm with (0:5)(0:5) puts the whole word of A in the cell, and by the law that storing the whole field replaces the word, the cell holds ww. This is the old contents.

Exercise 3.2-10. ENT1 50005000 is a fault: 5000≥4096=6425000 \ge 4096 = 64^2, so it does not fit in two bytes, and the model stops with the fault register. ENT1 40954095 is allowed. INC1 11 with I1 =4095= 4095 gives 40964096, 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. p=3500p = 3500. J holds the address of the next cell, 3021=47⋅64+133021 = 47 \cdot 64 + 13, so J =+ ∣ 47 ∣ 13= +\,|\,47\,|\,13.

Exercise 3.3-2. The value of A is 00. JAN needs <0< 0, so no. JAZ needs =0= 0, so yes. JAP needs >0> 0, so no. JANN needs ≥0\ge 0, so yes. JANZ needs ≠0\ne 0, so no. JANP needs ≤0\le 0, 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 TT and sets J. OV stays off.

Exercise 3.3-4. The magnitude of A is 5⋅644+6⋅643+7⋅642+8⋅64+95 \cdot 64^4 + 6 \cdot 64^3 + 7 \cdot 64^2 + 8 \cdot 64 + 9. SLA 22 drops the bytes 5,65, 6 and appends two zeros: + ∣ 7 ∣ 8 ∣ 9 ∣ 0 ∣ 0+\,|\,7\,|\,8\,|\,9\,|\,0\,|\,0. SRA 33 drops the bytes 8,98, 9 and the byte 77 and puts zeros on the left: + ∣ 0 ∣ 0 ∣ 0 ∣ 5 ∣ 6+\,|\,0\,|\,0\,|\,0\,|\,5\,|\,6.

Exercise 3.3-5. The ten bytes are 1,2,3,4,5,6,7,8,9,101, 2, 3, 4, 5, 6, 7, 8, 9, 10. A turn right by 33 moves the last three, 8,9,108, 9, 10, to the front: 8,9,10,1,2,3,4,5,6,78, 9, 10, 1, 2, 3, 4, 5, 6, 7. So A =+ ∣ 8 ∣ 9 ∣ 10 ∣ 1 ∣ 2= +\,|\,8\,|\,9\,|\,10\,|\,1\,|\,2 and X =+ ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7= +\,|\,3\,|\,4\,|\,5\,|\,6\,|\,7. The signs stay.

Exercise 3.3-6. Write m=q b5−k+rm = q\,b^{5-k} + r with 0≤r<b5−k0 \le r < b^{5-k}. Then m bk=q b5+r bkm\,b^k = q\,b^5 + r\,b^k and r bk<b5r\,b^k < b^5, so m bk mod b5=r bkm\,b^k \bmod b^5 = r\,b^k. The floor of r bk/bkr\,b^k / b^k is rr. So the magnitude after the two shifts is r=m mod b5−kr = m \bmod b^{5-k}. For k=0k = 0 it is m mod b5=mm \bmod b^5 = m, and for k=5k = 5 it is 00.

Exercise 3.3-7. By the definition, SRC by kk is the turn left by 10−(k mod 10)10 - (k \bmod 10) places. If kk is not a multiple of 1010, this is the number in the claim. If kk is a multiple of 1010, the turn left is by 1010 places, which has r=10 mod 10=0r = 10 \bmod 10 = 0, so it is the identity by the theorem on rotations.

Exercise 3.3-8. Here s=1002−1000=2s = 1002 - 1000 = 2 and F=4F = 4, so 0<s<F0 < s < F. The new cell 1002+k1002 + k holds the old cell 1000+(k mod 2)1000 + (k \bmod 2), so the cells 10021002 to 10051005 hold a,b,a,ba, b, a, b. The cells 10001000 and 10011001 are not changed. The six cells are a,b,a,b,a,ba, b, a, b, a, b. I1 =1006= 1006. The time is 1+2⋅4=91 + 2 \cdot 4 = 9.

Exercise 3.3-9. The fragment is ENT1 20012001 followed by MOVE 2000(60)2000(60). Here s=1s = 1, so each of the 6060 cells gets the word of the cell 20002000. The time is 1+(1+120)=1221 + (1 + 120) = 122.

Exercise 3.3-10. The digits of 10001000 are 00000010000000001000. A =+ ∣ 30 ∣ 30 ∣ 30 ∣ 30 ∣ 30= +\,|\,30\,|\,30\,|\,30\,|\,30\,|\,30 and X =+ ∣ 30 ∣ 31 ∣ 30 ∣ 30 ∣ 30= +\,|\,30\,|\,31\,|\,30\,|\,30\,|\,30. The time is 10u10u.

Exercise 3.3-11. The magnitude mm of A is below b5≤1010b^5 \le 10^{10}, so CHAR writes the digits of mm in ten places, and each byte is 30+di30 + d_i, which is did_i modulo 1010. NUM reads did_i and forms mm, as in the theorem. For the second part take A =+0= +0 and X =+ ∣ 0 ∣ 0 ∣ 0 ∣ 0 ∣ 5= +\,|\,0\,|\,0\,|\,0\,|\,0\,|\,5. NUM gives A =+5= +5. CHAR then gives A =+ ∣ 30 ∣ 30 ∣ 30 ∣ 30 ∣ 30= +\,|\,30\,|\,30\,|\,30\,|\,30\,|\,30 and X =+ ∣ 30 ∣ 30 ∣ 30 ∣ 30 ∣ 35= +\,|\,30\,|\,30\,|\,30\,|\,30\,|\,35, which is not the old A X.

Exercise 3.3-12. One pass takes T+1+1=T+2T + 1 + 1 = T + 2. I1 takes the values n,n−1,…,1n, n - 1, \dots, 1 at the start of the passes, and the last J1P does not jump, so there are nn passes. The time is n(T+2)n(T + 2).

Section 3.4: Three programs

Exercise 3.4-1. The time is 4n+74n + 7. For n=5n = 5 it is 2727 and for n=50n = 50 it is 207207.

Exercise 3.4-2. The table is x1=6x_1 = 6, x2=−1x_2 = -1. The run reads x2x_2 first.

step cell instruction A I1 clock
1 30003000 ENTA 00 00 00 11
2 30013001 LD1 10001000 00 22 33
3 30023002 J1Z 30063006 00 22 44
4 30033003 ADD 1000,11000,1 −1-1 22 66
5 30043004 DEC1 11 −1-1 11 77
6 30053005 J1P 30033003 −1-1 11 88
7 30033003 ADD 1000,11000,1 55 11 1010
8 30043004 DEC1 11 55 00 1111
9 30053005 J1P 30033003 55 00 1212
10 30063006 STA 19991999 55 00 1414
11 30073007 HLT 55 00 1515

The sum is 55 and the time is 15=4⋅2+715 = 4 \cdot 2 + 7.

Exercise 3.4-3. S reads x2=1x_2 = 1 first and A becomes 11. It then adds x1=1073741823x_1 = 1073741823 and the true sum is 1073741824=6451073741824 = 64^5. The magnitude modulo 64564^5 is 00, the sign is plus (the sign of the old A, as the sum is positive), and the flag is true. So A =+0= +0 and OV is on. For the table −1073741823,−2-1073741823, -2 the first word read is −2-2, so A =−2= -2. Then the true sum is −1073741825=−(645+1)-1073741825 = -(64^5 + 1). The magnitude modulo 64564^5 is 11 and the sign is minus. So A =−1= -1 and OV is on.

Exercise 3.4-4. The word 10737418231073741823 has 1010 digits, so d=10d = 10. The loop runs 1010 passes, with X =1073741823,107374182,…,1,0= 1073741823, 107374182, \dots, 1, 0 at the head of each pass after the first. The time is 16⋅10+7=16716 \cdot 10 + 7 = 167.

Exercise 3.4-5. The divisor 22 makes each pass halve X. The loop ends at the first j≥1j \ge 1 with ⌊∣n∣/2j⌋=0\lfloor |n| / 2^j \rfloor = 0, that is 2j>∣n∣2^j > |n|. So I2 counts the bits of ∣n∣|n| in radix 22, with 11 for n=0n = 0. The invariant and the time 16d+716 d + 7 hold with dd the number of bits, since the pass has the same cost. For n=1000n = 1000, 29=512≤1000<1024=2102^9 = 512 \le 1000 < 1024 = 2^{10}, so d=10d = 10 and the time is 167167.

Exercise 3.4-6. DIV overflows when ∣v∣≤∣a∣|v| \le |a|. At each DIV the register A holds +0+0 (it is set by ENTA at the start and by SRAX at the end of each pass), and ∣v∣=10>0|v| = 10 > 0, so there is no overflow and OV stays as it was. The count I2 is at most 1010 for a word below 64564^5, since ∣n∣<1010|n| < 10^{10}, and so it fits in two bytes. The effective address of each instruction is the address of its word, since the index is 00 and the cells are in 00 to 39993999. The shift count 55 is not negative. So no fault occurs.

Exercise 3.4-7. C takes 2⋅20+3=432 \cdot 20 + 3 = 43 and L takes 6⋅20+2=1226 \cdot 20 + 2 = 122. A MOVE of 2020 words is allowed since 20≤6320 \le 63.

Exercise 3.4-8. The loop L runs the steps for k=n−1,n−2,…,0k = n - 1, n - 2, \dots, 0 (the offset in the block), and step kk copies the cell M+kM + k to the cell M+s+kM + s + k. If s≥0s \ge 0, a cell M+jM + j with j<nj < n can be written only by the step j−s≤jj - s \le j. The step jj reads it first, and the steps with a smaller kk come later and read other cells. The step j−sj - s with s>0s > 0 comes after the step jj. So a cell is read before it is written, and the copy is correct. If s≤−ns \le -n the blocks are disjoint and the copy is correct. If −n<s<0-n < s < 0, take j=n−1+sj = n - 1 + s. We have 0≤j<n−10 \le j < n - 1. The step j−s=n−1j - s = n - 1 writes the cell M+jM + j first, because it is the first step, and the step jj reads it later. It reads the new value, not the old one, so the copy is wrong. So L is correct exactly when s≥0s \ge 0 or s≤−ns \le -n. MOVE is correct exactly when s≤0s \le 0 or s≥ns \ge n, by the theorem of the section. A block with s=0s = 0 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 00, ENT1 −n-n, a loop of ADD 1001+n,11001 + n, 1, INC1 11, J1N to the ADD, then STA 19991999 and HLT. With I1 =−n,…,−1= -n, \dots, -1 the ADD reads the cells 1001,…,1000+n1001, \dots, 1000 + n. The time is 1+1+n(2+1+1)+2+1=4n+51 + 1 + n(2 + 1 + 1) + 2 + 1 = 4n + 5. For n=0n = 0 the program runs the ADD once with I1 =0= 0, which adds the word of the cell 10011001, that is not part of the table, and then INC1 gives 11 and the jump is not taken. The result is wrong. The loop needs the test before the first pass for n=0n = 0.

Exercise 3.4-10. The new instructions are STA 10031003 (2u2u), LDX 10031003 (2u2u) and ENTA 00 (1u1u), which is 5u5u in place of 2u2u. A pass takes 12+1+5+1=1912 + 1 + 5 + 1 = 19 units, and the total is 1+2+1+19d+2+1=19d+71 + 2 + 1 + 19 d + 2 + 1 = 19 d + 7. 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 n=805n = 805 the time is 19⋅3+7=6419 \cdot 3 + 7 = 64.

This chapter has a full solution of each exercise of the volume, one file per chapter. Exercise kk of Section c.sc.s is called Exercise c.sc.s-kk. 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.