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

Machine words and number representations

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 first
Contents
  1. Positional notation
  2. Signed numbers and overflow
  3. Bytes and fields

Positional notation

Statement

A machine word is a row of places. Each place holds one digit. This section fixes the meaning of such a row as a number.

Definition P7.1.1 (radix representation). Let b≥2b \ge 2 be an integer, the radix. A row of digits dk−1dk−2…d0d_{k-1} d_{k-2} \dots d_0 with 0≤di<b0 \le d_i < b for each ii represents the number ∑i=0k−1dibi\sum_{i=0}^{k-1} d_i b^i. The digit d0d_0 is the least significant digit and dk−1d_{k-1} is the most significant digit. The row has width kk.

Lean: Mathlib Nat.ofDigits, which takes the digits with the least significant first (proved in Mathlib).

Example P7.1.2. In radix 77 the row 5 6 2 15\,6\,2\,1 represents 5⋅343+6⋅49+2⋅7+1=20245 \cdot 343 + 6 \cdot 49 + 2 \cdot 7 + 1 = 2024. In radix 22 the row 1 0 1 11\,0\,1\,1 represents 8+0+2+1=118 + 0 + 2 + 1 = 11.

Example P7.1.3. In radix 6464 the row 15 4015\,40 represents 15⋅64+40=100015 \cdot 64 + 40 = 1000. A digit of this radix is a number from 00 to 6363, so a digit is written as a number and not as a letter.

Theorem P7.1.4 (the range of a word). A row of width kk in radix bb represents a number nn with 0≤n≤bk−10 \le n \le b^k - 1. Each integer nn in this range has exactly one row of width kk that represents it.

Lean: Mathlib Nat.ofDigits_lt_base_pow_length for the bound; the project states the bound for a MIX word as TAOCP.MIX.Word.mag_lt (w.mag < b ^ 5).

Proof. The bound. Each digit is at most b−1b - 1, so the number is at most (b−1)∑i=0k−1bi=bk−1(b-1) \sum_{i=0}^{k-1} b^i = b^k - 1, by the sum of a geometric series. It is at least 00.

Uniqueness and existence together, by induction on kk. For k=0k = 0 the only row is the empty row, and the only number in the range 0≤n≤00 \le n \le 0 is 00. Let k≥1k \ge 1 and let 0≤n≤bk−10 \le n \le b^k - 1. Write n=qb+rn = qb + r with 0≤r<b0 \le r < b (division with remainder). Then q≤(bk−1)/b<bk−1q \le (b^k - 1)/b < b^{k-1}, so q≤bk−1−1q \le b^{k-1} - 1. Any row for nn has the form dk−1…d1d0d_{k-1} \dots d_1 d_0 with n=b⋅∑i≥1dibi−1+d0n = b \cdot \sum_{i \ge 1} d_i b^{i-1} + d_0. Since 0≤d0<b0 \le d_0 < b, the uniqueness of the division gives d0=rd_0 = r and ∑i≥1dibi−1=q\sum_{i \ge 1} d_i b^{i-1} = q. The row dk−1…d1d_{k-1} \dots d_1 has width k−1k - 1 and represents qq, and by the induction hypothesis there is exactly one such row. So there is exactly one row for nn. □\square

Corollary P7.1.5 (number of digits). A positive integer nn needs exactly ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1 digits, when its most significant digit must not be 00.

Proof. The width kk is the least integer with n≤bk−1n \le b^k - 1, that is, n<bkn < b^k. The least kk with bk>nb^k > n is ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1. □\square

Algorithm P7.1.6 (conversion by division). Input: n≥0n \ge 0 and the radix bb. Output: the digits of nn.

  1. Set i←0i \leftarrow 0.
  2. While n>0n > 0: set di←n mod bd_i \leftarrow n \bmod b, then n←⌊n/b⌋n \leftarrow \lfloor n / b \rfloor, then i←i+1i \leftarrow i + 1.
  3. The digits di−1…d0d_{i-1} \dots d_0 are the row. For n=0n = 0 the row is empty, and the number is written with one digit 00.

The invariant of the loop is: the original number equals nbi+∑j<idjbjn b^i + \sum_{j < i} d_j b^j. It holds at the start with i=0i = 0. One step replaces nn by qq with n=qb+rn = qb + r, and nbi+⋯=qbi+1+rbi+…n b^i + \dots = q b^{i+1} + r b^i + \dots, so it holds again. The loop stops because nn falls strictly while it is positive. At the end n=0n = 0 and the invariant says that the original number equals ∑djbj\sum d_j b^j.

Algorithm P7.1.7 (Horner evaluation). The value of a row is computed by v←0v \leftarrow 0, then v←vb+dv \leftarrow v b + d for each digit dd from the most significant to the least significant. After mm digits, vv is the number that the first mm digits represent. This is the form that a machine uses: it needs kk multiplications and kk additions, and no powers.

Lean: the project defines the value of a five-byte magnitude in this form as TAOCP.MIX.horner5, with TAOCP.MIX.horner_lt as the proof that a Horner step keeps the bound.

Proposition P7.1.8 (change of radix by grouping). Let c=bmc = b^m. A row in radix bb of width k=mtk = mt represents the same number as the row of width tt in radix cc whose digits are the groups of mm consecutive digits of the first row.

Proof. Put the groups Gj=∑i=0m−1djm+ibiG_j = \sum_{i=0}^{m-1} d_{jm+i} b^i for 0≤j<t0 \le j < t. Each GjG_j is between 00 and bm−1b^m - 1, so it is a digit of radix cc. Then ∑l=0k−1dlbl=∑j=0t−1∑i=0m−1djm+ibjm+i=∑jGjcj\sum_{l=0}^{k-1} d_l b^l = \sum_{j=0}^{t-1} \sum_{i=0}^{m-1} d_{jm+i} b^{jm+i} = \sum_j G_j c^j. □\square

Examples

Conversion by division. Take n=2024n = 2024 and b=7b = 7. The steps are 2024=289⋅7+12024 = 289 \cdot 7 + 1, 289=41⋅7+2289 = 41 \cdot 7 + 2, 41=5⋅7+641 = 5 \cdot 7 + 6, 5=0⋅7+55 = 0 \cdot 7 + 5. The remainders are 1,2,6,51, 2, 6, 5 from the first step to the last. The row is 5 6 2 15\,6\,2\,1, with the last remainder most significant. The table shows the invariant: 2024=n7i+(digits so far)2024 = n 7^i + (\text{digits so far}).

step nn before qq rr ii after q⋅7i+∑dj7jq \cdot 7^{i} + \sum d_j 7^j
1 2024 289 1 1 289⋅7+1=2024289 \cdot 7 + 1 = 2024
2 289 41 2 2 41⋅49+2⋅7+1=202441 \cdot 49 + 2 \cdot 7 + 1 = 2024
3 41 5 6 3 5⋅343+6⋅49+2⋅7+1=20245 \cdot 343 + 6 \cdot 49 + 2 \cdot 7 + 1 = 2024
4 5 0 5 4 0+5⋅343+6⋅49+2⋅7+1=20240 + 5 \cdot 343 + 6 \cdot 49 + 2 \cdot 7 + 1 = 2024

Grouping. The number 215215 in radix 22 is 1 1 0 1 0 1 1 11\,1\,0\,1\,0\,1\,1\,1, width 88. To use radix 8=238 = 2^3, pad the row on the left to width 99: 0 1 1 0 1 0 1 1 10\,1\,1\,0\,1\,0\,1\,1\,1. The groups of three digits are 011011, 010010, 111111, which are 33, 22, 77. So 215=3 2 7215 = 3\,2\,7 in radix 88, and 3⋅64+2⋅8+7=2153 \cdot 64 + 2 \cdot 8 + 7 = 215.

Range of a MIX word. With b=64b = 64 and k=5k = 5 the magnitudes are 00 to 645−1=107374182364^5 - 1 = 1073741823. With b=100b = 100 they are 00 to 1010−110^{10} - 1. An index register has two bytes, so its magnitude is at most 642−1=409564^2 - 1 = 4095 with b=64b = 64, and 99⋅100+99=999999 \cdot 100 + 99 = 9999 with b=100b = 100. The number 40964096 in radix 6464 is 1 0 01\,0\,0, three digits: it does not fit in two bytes.

Number of digits. The number 1024=2101024 = 2^{10} has ⌊log⁡21024⌋+1=11\lfloor \log_2 1024 \rfloor + 1 = 11 binary digits, and 10231023 has 1010.

Exercises

  1. [10] Write 10001000 in radix 22, radix 88 and radix 1616.
  2. [10] What is the least width in radix 6464 for the number 10610^6? Give its digits.
  3. [M15] Show that the sum of the digits of nn in radix bb is congruent to nn modulo b−1b - 1.
  4. [M20] Prove that a row of width kk whose most significant digit is not 00 represents a number that is at least bk−1b^{k-1}.
  5. [M20] Show that the conversion by division of nn takes ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1 divisions for n≥1n \ge 1.
  6. [M22] Prove that Horner evaluation gives the value of the row, by an invariant for the loop.
  7. [15] Convert the radix 1616 row 3 A F3\,\mathrm{A}\,\mathrm{F} to radix 44 by grouping, then to radix 1010.
  8. [M25] Let b=64b = 64. Find the number of rows of width 55, and the number of these rows with a nonzero most significant digit.
  9. [M23] Prove that the conversion of a fraction 0≤x<10 \le x < 1 to radix bb by repeated multiplication is exact in radix bb after mm steps if and only if xbmx b^m is an integer.
  10. [20] The row dk−1…d0d_{k-1} \dots d_0 is stored with d0d_0 first in a list. Write the Horner algorithm for this order of the digits, and say what it costs in additions and multiplications.

Lean

The digits of a number are Mathlib Nat.digits b n, with the least significant digit first, and Nat.ofDigits b l is the value of a list. The theorem Nat.ofDigits_digits says that Nat.ofDigits b (Nat.digits b n) = n, and Nat.digits_lt_base says that each digit is below the radix. The project does not restate these. For a MIX word the magnitude is TAOCP.MIX.Word.mag, defined by TAOCP.MIX.horner5 on the five bytes, and TAOCP.MIX.Word.mag_lt proves the bound 64564^5 of the theorem above for a general radix b: the magnitude is below b ^ 5.

Notes and history

The positional notation came to Europe from India through the Arabic books, and its wide use for calculation dates from the thirteenth century. TAOCP Section 1.3.1 uses a radix that a MIX byte takes, between 64 and 100, and Section 4.1 of Volume 2 treats the positional systems in full. The radix of a machine word is a power of 22 in nearly all real machines, and then the grouping of the proposition above converts between binary and hexadecimal by eye.

Signed numbers and overflow

Statement

A word of kk places holds bkb^k rows. This section gives three ways to read a row as an integer that may be negative, and the rule that tells when a sum leaves the range.

Definition P7.1.9 (sign and magnitude). A word is a sign s∈{+,−}s \in \{+, -\} and a row of kk digits that represents a magnitude mm with 0≤m≤bk−10 \le m \le b^k - 1. Its value is mm for s=+s = + and −m-m for s=−s = -. The values +0+0 and −0-0 are two words with the same value 00.

Definition P7.1.10 (twos complement). Let b=2b = 2. A row of nn bits represents the number uu with 0≤u≤2n−10 \le u \le 2^n - 1. Its twos complement value is uu when u<2n−1u < 2^{n-1} and u−2nu - 2^n when u≥2n−1u \ge 2^{n-1}.

Definition P7.1.11 (ones complement). With the same row, the ones complement value is uu when u<2n−1u < 2^{n-1} and u−(2n−1)u - (2^n - 1) when u≥2n−1u \ge 2^{n-1}.

Lean: Mathlib has Int.toNat, Int.natAbs, and the type Fin (2 ^ n) with the arithmetic modulo 2n2^n; the bit vector BitVec n has BitVec.toInt, the twos complement value (proved in Mathlib). The project defines the sign and magnitude word as TAOCP.MIX.Word with TAOCP.MIX.Word.value.

Theorem P7.1.12 (ranges). In sign and magnitude with kk digits the values are the integers from −(bk−1)-(b^k - 1) to bk−1b^k - 1. In ones complement with nn bits they are the integers from −(2n−1−1)-(2^{n-1} - 1) to 2n−1−12^{n-1} - 1, and the value 00 has two rows. In twos complement with nn bits they are the integers from −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1, and each has one row.

Proof. The first claim is the definition. For the twos complement, the rows with u<2n−1u < 2^{n-1} give 0,…,2n−1−10, \dots, 2^{n-1} - 1, and the rows with u≥2n−1u \ge 2^{n-1} give u−2nu - 2^n from −2n−1-2^{n-1} to −1-1. These 2n2^n values are distinct, so each has one row. For the ones complement, u<2n−1u < 2^{n-1} gives 0,…,2n−1−10, \dots, 2^{n-1} - 1 and u≥2n−1u \ge 2^{n-1} gives u−2n+1u - 2^n + 1 from −(2n−1−1)-(2^{n-1} - 1) to 00. The row of all zeros and the row of all ones both have value 00. □\square

Theorem P7.1.13 (twos complement is arithmetic modulo 2n2^n). Let xx and yy be integers in the twos complement range, with rows ux≡xu_x \equiv x and uy≡y(mod2n)u_y \equiv y \pmod{2^n}. The row (ux+uy) mod 2n(u_x + u_y) \bmod 2^n has the twos complement value x+yx + y when x+yx + y is in the range. When x+yx + y is out of the range, the value of this row is x+y±2nx + y \pm 2^n, the one that falls in the range.

Proof. The row uu of an integer xx in the range satisfies u≡x(mod2n)u \equiv x \pmod{2^n}, since u=xu = x or u=x+2nu = x + 2^n. So (ux+uy) mod 2n≡x+y(mod2n)(u_x + u_y) \bmod 2^n \equiv x + y \pmod{2^n}. The value of a row is the unique integer in the range [−2n−1,2n−1−1][-2^{n-1}, 2^{n-1} - 1] that is congruent to it modulo 2n2^n. If x+yx + y is in the range, it is this integer. The sum x+yx + y is between −2n-2^n and 2n−22^n - 2, so if it is out of the range, adding or subtracting 2n2^n brings it into the range. □\square

Definition P7.1.14 (overflow). The sum of two words overflows when the integer x+yx + y is not a value of the word.

Theorem P7.1.15 (the sign rule of the overflow). In twos complement, the sum of xx and yy overflows if and only if xx and yy have the same sign and the row of the result has the other sign. The sign here is the top bit of the row.

Proof. Let x≥0x \ge 0 and y<0y < 0. Then −2n−1≤x+y≤2n−1−2-2^{n-1} \le x + y \le 2^{n-1} - 2, so there is no overflow, and the rule says the same because the signs differ. Let x,y≥0x, y \ge 0. The sum is between 00 and 2n−22^n - 2. It overflows exactly when x+y≥2n−1x + y \ge 2^{n-1}. Then the row of the result is x+yx + y with x+y≥2n−1x + y \ge 2^{n-1}, so its top bit is 11, and the signs differ. Without overflow the row is x+y<2n−1x + y < 2^{n-1} with top bit 00. The case x,y<0x, y < 0 is the same: the sum is between −2n-2^n and −2-2, it overflows exactly when it is below −2n−1-2^{n-1}, and then the result x+y+2nx + y + 2^n is at least 00, with the top bit 00. □\square

Theorem P7.1.16 (negation). In twos complement, the row of −x-x is the row of xx with each bit inverted, plus one, modulo 2n2^n.

Proof. Inverting each bit of uu gives 2n−1−u2^n - 1 - u. Adding 11 gives 2n−u≡−u(mod2n)2^n - u \equiv -u \pmod{2^n}. The one case where −x-x is out of the range is x=−2n−1x = -2^{n-1}, and the rule maps this row to itself. □\square

Definition P7.1.17 (a MIX word). A MIX word has a sign and five bytes b1,…,b5b_1, \dots, b_5 of radix bb (with b=64b = 64 or b=100b = 100 in the book). Its magnitude is ((((b1b+b2)b+b3)b+b4)b+b5)((((b_1 b + b_2) b + b_3) b + b_4) b + b_5) and it is below b5b^5. Its value is the magnitude with the sign. The addition of an integer vv to a word with sign ss has the result of magnitude ∣w+v∣ mod b5|w + v| \bmod b^5, with the sign of w+vw + v, or the sign ss when w+v=0w + v = 0, and the overflow flag set when ∣w+v∣≥b5|w + v| \ge b^5.

Lean: TAOCP.MIX.Word (the structure with sign, b1 to b5), TAOCP.MIX.Word.mag, TAOCP.MIX.Word.value, TAOCP.MIX.Sign.ofInt and TAOCP.MIX.Word.addInt, which returns the pair of the result and the flag.

Examples

Twos complement, 8 bits. The row 1011010010110100 has u=180≥128u = 180 \ge 128, so its value is 180−256=−76180 - 256 = -76. In ones complement the same row has the value 180−255=−75180 - 255 = -75. The row of −77-77 is 256−77=179=10110011256 - 77 = 179 = 10110011. The inversion of 77=0100110177 = 01001101 is 10110010=17810110010 = 178, and 178+1=179178 + 1 = 179, as the negation theorem says.

Overflow, 4 bits. The range is −8-8 to 77. The table has the sum of the values, the row of the result and its value.

xx yy x+yx + y value of the result row overflow
5 3 8 −8-8 yes
5 4 9 −7-7 yes
−6-6 −3-3 −9-9 7 yes
−6-6 −2-2 −8-8 −8-8 no
7 1 8 −8-8 yes
−8-8 −1-1 −9-9 7 yes
3 −5-5 −2-2 −2-2 no

In each row with overflow the two operands have the same sign and the result has the other sign. In the row −6+(−2)-6 + (-2) the sum is exactly −8-8, which is in the range.

An 8 bit sum. The sum 100+50=150100 + 50 = 150 is out of the range −128-128 to 127127. The row of the result is 150 mod 256=150150 \bmod 256 = 150, with the value 150−256=−106150 - 256 = -106. The overflow rule agrees: both operands are positive and the result is negative.

MIX overflow. With b=64b = 64 take the word with sign ++ and all five bytes 6363, so its value is 645−1=107374182364^5 - 1 = 1073741823. Adding 55 gives 1073741828≥6451073741828 \ge 64^5. The magnitude of the result is 1073741828 mod 1073741824=41073741828 \bmod 1073741824 = 4, the sign is ++, and the flag is set. The result is the word + 0 0 0 0 4+\,0\,0\,0\,0\,4 with overflow. In the same radix, the word −0-0 plus the integer 00 gives the sum 00, which has no sign, so the result keeps the old sign −- and the flag is clear.

Exercises

  1. [10] What are the twos complement and the ones complement values of the 8 bit row 1111111111111111?
  2. [10] Write −100-100 as an 8 bit twos complement row.
  3. [M15] Show that in twos complement with nn bits the negation of −2n−1-2^{n-1} is itself, and that this is the only such case apart from 00.
  4. [M20] Prove that in ones complement the row of −x-x is the row of xx with each bit inverted.
  5. [M20] Show that the ones complement value of uu is congruent to uu modulo 2n−12^n - 1, for 0≤u<2n−10 \le u < 2^n - 1.
  6. [15] List the pairs (x,y)(x, y) of 3 bit twos complement values with x+yx + y out of range, for x≥y≥0x \ge y \ge 0.
  7. [M22] Prove that in twos complement the overflow of a sum x+yx + y holds if and only if the carry into the top bit differs from the carry out of the top bit.
  8. [M25] Let b=64b = 64. A MIX word of sign ++ is added to itself. Find the largest magnitude for which there is no overflow, and the magnitude of the result for the magnitudes 2292^{29} and 10910^9.
  9. [20] How many different words does MIX have with b=64b = 64, and how many different values?
  10. [M24] Show that sign and magnitude addition needs a comparison of magnitudes when the signs differ, and explain why twos complement does not.

Lean

The twos complement value of a row is BitVec.toInt in the core library, and the sum of two bit vectors is the sum modulo 2n2^n (BitVec.add). The project does not state the overflow rule. For MIX, TAOCP.MIX.Word.value is the value of a word, TAOCP.MIX.Word.abs_value_lt proves ∣w∣<b5|w| < b^5, and TAOCP.MIX.Word.value_ofInt proves that Word.ofInt r old has value r when r.natAbs < b ^ 5. The overflow flag of an addition is the second component of TAOCP.MIX.Word.addInt, and it is decide (b ^ 5 ≤ (w.value + v).natAbs) by the definition.

Notes and history

Sign and magnitude is the form of the decimal calculator and of MIX. Twos complement became the standard form in the 1960s and 1970s, because it has one zero and its adder needs no comparison. TAOCP Section 4.1 of Volume 2 treats the signed number systems, and the MIX description in 1.3.1 uses sign and magnitude with a sign that stays on a zero result. The sign rule of the overflow is the one that a hardware designer builds as a gate on the three sign bits.

Bytes and fields

Statement

A program often needs a part of a word: one byte, the address part of an instruction, or the sign with the first bytes. A field names such a part. This section defines the field of a MIX word, gives its extraction by division and remainder, and proves the laws of load and store.

Definition P7.1.18 (positions and fields). The positions of a MIX word are numbered 00 to 55 from the left. Position 00 is the sign, and positions 11 to 55 are the bytes b1,…,b5b_1, \dots, b_5. A field is a pair (L:R)(L:R) with 0≤L≤R≤50 \le L \le R \le 5. It names the positions L,L+1,…,RL, L+1, \dots, R. The first byte position is f=max⁡(L,1)f = \max(L, 1), the length is ℓ=R+1−f\ell = R + 1 - f bytes, and the shift is σ=5−R\sigma = 5 - R, the number of bytes to the right of the field.

Example P7.1.19. The field (2:4)(2:4) has f=2f = 2, ℓ=3\ell = 3, σ=1\sigma = 1. The field (0:2)(0:2) has f=1f = 1, ℓ=2\ell = 2, σ=3\sigma = 3, and it contains the sign. The field (0:0)(0:0) has f=1f = 1, ℓ=0\ell = 0, σ=5\sigma = 5: it is the sign alone, with no byte.

Example P7.1.20. The code of a field in an instruction is F=8L+RF = 8L + R. So (0:5)(0:5) has the code 55, (1:5)(1:5) has 1313, (3:4)(3:4) has 2828 and (0:0)(0:0) has 00. The code is unique because R≤5<8R \le 5 < 8.

Lean: TAOCP.MIX.Field (the structure with L, R), Field.code, Field.decode, Field.first, Field.len, Field.shift, with TAOCP.MIX.Field.decode_code (the decode of the code is the field) and Field.code_lt (the code is below 64).

Definition P7.1.21 (the value of a field). Let ww be a word with magnitude mm. The field magnitude of (L:R)(L:R) is ⌊m/bσ⌋ mod bℓ\lfloor m / b^{\sigma} \rfloor \bmod b^{\ell}. The field sign is the sign of ww if L=0L = 0 and ++ otherwise. The field value is the field magnitude with the field sign.

Lean: TAOCP.MIX.Word.fieldMag, Word.fieldSign, Word.fieldValue.

Theorem P7.1.22 (the field is the digits of its positions). The field magnitude of (L:R)(L:R) equals the number that the bytes bf,…,bRb_f, \dots, b_R represent in radix bb. For ℓ=0\ell = 0 it is 00.

Lean: the project defines the field magnitude by the formula above and does not restate this theorem; the whole-word cases are TAOCP.MIX.Word.fieldValue_whole and TAOCP.MIX.Word.fieldValue_one_five.

Proof. The magnitude is m=∑i=15bib5−im = \sum_{i=1}^{5} b_i b^{5-i}. Dividing by bσ=b5−Rb^{\sigma} = b^{5-R} and taking the floor removes the terms with 5−i<5−R5 - i < 5 - R, that is i>Ri > R, because their sum is less than b5−Rb^{5-R}. This is the bound of the theorem on the range of a word, for the bytes bR+1,…,b5b_{R+1}, \dots, b_5. The terms that stay have the weights bR−ib^{R-i} for i≤Ri \le R, so ⌊m/bσ⌋=∑i=1RbibR−i\lfloor m / b^{\sigma} \rfloor = \sum_{i=1}^{R} b_i b^{R-i}. Taking the remainder modulo bℓb^{\ell} keeps the terms with R−i<ℓ=R+1−fR - i < \ell = R + 1 - f, that is i≥fi \ge f, and the terms with i<fi < f have a weight that is a multiple of bℓb^{\ell}. What stays is ∑i=fRbibR−i\sum_{i=f}^{R} b_i b^{R-i}, which is the number of the row bf…bRb_f \dots b_R. □\square

Definition P7.1.23 (load). The load of the field (L:R)(L:R) of a word ww is the word with the magnitude of the field magnitude and the field sign. This is the content of the register after an instruction such as LDA with that field.

Definition P7.1.24 (store). The store of the right end of a word ss into the field (L:R)(L:R) of the word dd is the word whose magnitude is ⌊mdbσ+ℓ⌋bσ+ℓ+(ms mod bℓ) bσ+(md mod bσ),\Bigl\lfloor \frac{m_d}{b^{\sigma + \ell}} \Bigr\rfloor b^{\sigma + \ell} + (m_s \bmod b^{\ell})\, b^{\sigma} + (m_d \bmod b^{\sigma}), with the sign of ss when L=0L = 0 and the sign of dd otherwise. Here msm_s and mdm_d are the magnitudes. The ℓ\ell low bytes of ss replace the field. The other bytes and, when L>0L > 0, the sign of dd, do not change.

Lean: TAOCP.MIX.Word.load and TAOCP.MIX.Word.store, with the magnitude TAOCP.MIX.Word.mag_store and the sign TAOCP.MIX.Word.sign_store.

Theorem P7.1.25 (load after store). The load of (L:R)(L:R) from the result of the store of ss into (L:R)(L:R) of dd has magnitude ms mod bℓm_s \bmod b^{\ell} and sign equal to the sign of ss when L=0L = 0, and ++ otherwise.

Lean: TAOCP.MIX.Word.load_store (proved in the project).

Proof. Let A=⌊md/bσ+ℓ⌋bσ+ℓA = \lfloor m_d / b^{\sigma+\ell} \rfloor b^{\sigma+\ell}, F=ms mod bℓF = m_s \bmod b^{\ell} and C=md mod bσC = m_d \bmod b^{\sigma}. The new magnitude is A+Fbσ+CA + F b^{\sigma} + C. Here C<bσC < b^{\sigma} and F<bℓF < b^{\ell}. Dividing by bσb^{\sigma} gives A/bσ+FA / b^{\sigma} + F, since AA is a multiple of bσb^{\sigma} and C<bσC < b^{\sigma}. Then A/bσA / b^{\sigma} is a multiple of bℓb^{\ell}, so the remainder modulo bℓb^{\ell} is FF, because F<bℓF < b^{\ell}. The sign follows from the definition of the field sign, applied to the sign of the result. □\square

Theorem P7.1.26 (store keeps the rest). The store changes no byte outside the field. Precisely, the new magnitude and mdm_d have the same quotient by bσ+ℓb^{\sigma+\ell} and the same remainder modulo bσb^{\sigma}.

Lean: TAOCP.MIX.Word.store_high and TAOCP.MIX.Word.store_low (proved in the project).

Proof. The new magnitude is A+Fbσ+CA + F b^{\sigma} + C with Fbσ+C<bσ+ℓF b^{\sigma} + C < b^{\sigma+\ell}, since F≤bℓ−1F \le b^{\ell} - 1 and C≤bσ−1C \le b^{\sigma} - 1 give Fbσ+C≤bσ+ℓ−1F b^{\sigma} + C \le b^{\sigma+\ell} - 1. So the quotient by bσ+ℓb^{\sigma+\ell} is A/bσ+ℓA / b^{\sigma+\ell}, which is the quotient of mdm_d. The remainder modulo bσb^{\sigma} is CC, since A+FbσA + F b^{\sigma} is a multiple of bσb^{\sigma}. □\square

Corollary P7.1.27 (whole word). The load of (0:5)(0:5) is the word itself, and the store into (0:5)(0:5) gives the source word.

Lean: TAOCP.MIX.Word.load_whole and TAOCP.MIX.Word.store_whole.

Proof. For (0:5)(0:5), σ=0\sigma = 0, ℓ=5\ell = 5 and the sign is the sign of the word. The field magnitude is m mod b5=mm \bmod b^5 = m since m<b5m < b^5. In the store formula, the first term is 00 since md<b5m_d < b^5, and the remaining terms are msm_s and 00. □\square

Definition P7.1.28 (half word). The index registers and the register J hold a sign and two bytes. The magnitude of such a half word is b4b+b5b_4 b + b_5 and it is below b2b^2. A word fits a half word when its bytes b1,b2,b3b_1, b_2, b_3 are zero.

Lean: TAOCP.MIX.Half, Half.mag, Half.mag_lt, Word.fitsHalf, Word.toHalf, Half.toWord.

Examples

Load. Take b=64b = 64 and the word − ∣ 17 ∣ 0 ∣ 5 ∣ 63 ∣ 2-\,|\,17\,|\,0\,|\,5\,|\,63\,|\,2 with magnitude 17⋅644+0+5⋅642+63⋅64+2=28523718617 \cdot 64^4 + 0 + 5 \cdot 64^2 + 63 \cdot 64 + 2 = 285237186. The field (2:4)(2:4) has σ=1\sigma = 1 and ℓ=3\ell = 3. The field magnitude is ⌊285237186/64⌋ mod 643\lfloor 285237186 / 64 \rfloor \bmod 64^3, and the bytes 0,5,630, 5, 63 give 0⋅4096+5⋅64+63=3830 \cdot 4096 + 5 \cdot 64 + 63 = 383. The field (0:2)(0:2) has the sign, so its value is −(17⋅64+0)=−1088-(17 \cdot 64 + 0) = -1088. The field (4:4)(4:4) has the value 6363. The field (3:5)(3:5) has the bytes 5,63,25, 63, 2 and the value 5⋅4096+63⋅64+2=245145 \cdot 4096 + 63 \cdot 64 + 2 = 24514. The field (1:5)(1:5) has the plus sign and the value 285237186285237186.

Store. Let s=+ ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5s = +\,|\,1\,|\,2\,|\,3\,|\,4\,|\,5 and d=− ∣ 9 ∣ 9 ∣ 9 ∣ 9 ∣ 9d = -\,|\,9\,|\,9\,|\,9\,|\,9\,|\,9. Store into (3:4)(3:4): ℓ=2\ell = 2, σ=1\sigma = 1. The two low bytes of ss are 4,54, 5. They go to the positions 33 and 44. The result is − ∣ 9 ∣ 9 ∣ 4 ∣ 5 ∣ 9-\,|\,9\,|\,9\,|\,4\,|\,5\,|\,9, and the sign of dd stays. Store into (0:2)(0:2): ℓ=2\ell = 2, σ=3\sigma = 3. The low bytes 4,54, 5 go to the positions 11 and 22, and the sign of ss is taken, so the result is + ∣ 4 ∣ 5 ∣ 9 ∣ 9 ∣ 9+\,|\,4\,|\,5\,|\,9\,|\,9\,|\,9. Store into (2:3)(2:3): the result is − ∣ 9 ∣ 4 ∣ 5 ∣ 9 ∣ 9-\,|\,9\,|\,4\,|\,5\,|\,9\,|\,9.

The formula. For the first store, md=9⋅644+9⋅643+9⋅642+9⋅64+9m_d = 9 \cdot 64^4 + 9 \cdot 64^3 + 9 \cdot 64^2 + 9 \cdot 64 + 9 and σ+ℓ=3\sigma + \ell = 3. So A=⌊md/643⌋⋅643=(9⋅64+9)⋅643=153354240A = \lfloor m_d / 64^3 \rfloor \cdot 64^3 = (9 \cdot 64 + 9) \cdot 64^3 = 153354240. The two low bytes of ss are 4,54, 5, so ms mod 642=4⋅64+5=261m_s \bmod 64^2 = 4 \cdot 64 + 5 = 261, and 261⋅64σ=16704261 \cdot 64^{\sigma} = 16704. The low part is C=md mod 64=9C = m_d \bmod 64 = 9. The sum is 153354240+16704+9=153370953153354240 + 16704 + 9 = 153370953. Its bytes are 9,9,4,5,99, 9, 4, 5, 9.

Half word. With b=64b = 64 a half word holds a magnitude up to 40954095. The word + 0 0 0 4 5+\,0\,0\,0\,4\,5 fits, with magnitude 4⋅64+5=2614 \cdot 64 + 5 = 261. The word + 0 0 1 0 0+\,0\,0\,1\,0\,0 does not fit, since b3=1b_3 = 1.

Exercises

  1. [10] Give σ\sigma, ℓ\ell and F=8L+RF = 8L + R for the fields (1:1)(1:1), (1:3)(1:3), (2:5)(2:5) and (0:3)(0:3).
  2. [10] With b=64b = 64, find the value of the field (1:2)(1:2) of the word + 3 7 0 0 1+\,3\,7\,0\,0\,1.
  3. [M15] Show that two different fields have two different codes 8L+R8L + R.
  4. [15] Store the word + 1 2 3 4 5+\,1\,2\,3\,4\,5 into the field (1:3)(1:3) of − 9 9 9 9 9-\,9\,9\,9\,9\,9. What is the result?
  5. [M20] Show that the store of ss into the field (L:R)(L:R) of dd with L≥1L \ge 1 never changes the sign of dd.
  6. [M22] Prove that storing into the field (L:R)(L:R) and then storing into a disjoint field (L′:R′)(L':R') gives the same word in either order, when both fields have L,L′≥1L, L' \ge 1.
  7. [M20] Prove that the store formula gives a magnitude below b5b^5.
  8. [15] Which fields have ℓ=0\ell = 0? What does a store into one of them do?
  9. [M25] Let dd be a word, and let ss be the load of (L:R)(L:R) from dd, as a word. Show that storing ss into (L:R)(L:R) of dd gives dd back, when L≥1L \ge 1. Does the same hold when L=0L = 0?
  10. [20] The word ww has the bytes b4,b5b_4, b_5 as the address. Write the formula that extracts the address as a half word and the formula that tests that ww fits a half word, with bb general.
  11. [M23] Count the fields of a MIX word, and count those that have ℓ=0\ell = 0 and those that contain the sign.

Lean

The fields are TAOCP.MIX.Field, and the definitions of load and store are TAOCP.MIX.Word.load and TAOCP.MIX.Word.store in TAOCP/MIX/Field.lean. The theorems Word.mag_load, Word.sign_load, Word.value_load, Word.mag_store, Word.sign_store, Word.fieldMag_store, Word.load_store, Word.store_high, Word.store_low, Word.load_whole and Word.store_whole are proved in the project, all in the namespace TAOCP.MIX. The extraction by division and remainder is the definition of Word.fieldMag, so the theorem on the digits of the positions has no separate statement. The step from the word to the fields uses TAOCP.MIX.Word.mag_lt, the bound of section 1.

Notes and history

The field specification is a feature of MIX that no machine of today has, but it models the extraction of a bit field by a mask and a shift. TAOCP Section 1.3.1 introduces the field (L:R)(L:R) and the code 8L+R8L + R, and the later sections use it on every instruction. MMIX has no field, and chapter 4 shows the instructions that replace it: shifts and the masks.

A machine keeps a number in a fixed number of places. The place has a finite set of digits, and the number of places is the width of the word. Every statement about what a program computes depends on this fact: a sum can leave the range of the word, and a product can lose its high part. This chapter gives the exact rules, so that a later chapter can prove a program correct on the machine and not on the integers of mathematics.

Section 1 defines the representation of a natural number in radix bb, proves that the representation exists and is unique, and gives the algorithms of conversion. Section 2 adds the sign. It compares sign and magnitude, ones complement and twos complement, proves the rule of the overflow, and gives the value of a word of MIX. Section 3 cuts a word into bytes and fields, with the rules of load and store that MIX uses for its field specification (L:R)(L:R).

The chapter is the base of the case studies of chapters 3 and 4, and of TAOCP Section 1.3.1. It uses only P1: induction, the division with remainder and the floor function. The Lean parts name the declarations of the project in TAOCP/MIX/Word.lean and TAOCP/MIX/Field.lean, and the lemmas of Mathlib on digits.

The source of this page. Something is wrong? Report it.