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
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 be an integer, the radix. A row of digits with for each represents the number . The digit is the least significant digit and is the most significant digit. The row has width .
Lean: Mathlib Nat.ofDigits, which takes the digits with the least significant first (proved in Mathlib).
Example P7.1.2. In radix the row represents . In radix the row represents .
Example P7.1.3. In radix the row represents . A digit of this radix is a number from to , 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 in radix represents a number with . Each integer in this range has exactly one row of width 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 , so the number is at most , by the sum of a geometric series. It is at least .
Uniqueness and existence together, by induction on . For the only row is the empty row, and the only number in the range is . Let and let . Write with (division with remainder). Then , so . Any row for has the form with . Since , the uniqueness of the division gives and . The row has width and represents , and by the induction hypothesis there is exactly one such row. So there is exactly one row for .
Corollary P7.1.5 (number of digits). A positive integer needs exactly digits, when its most significant digit must not be .
Proof. The width is the least integer with , that is, . The least with is .
Algorithm P7.1.6 (conversion by division). Input: and the radix . Output: the digits of .
- Set .
- While : set , then , then .
- The digits are the row. For the row is empty, and the number is written with one digit .
The invariant of the loop is: the original number equals . It holds at the start with . One step replaces by with , and , so it holds again. The loop stops because falls strictly while it is positive. At the end and the invariant says that the original number equals .
Algorithm P7.1.7 (Horner evaluation). The value of a row is computed by , then for each digit from the most significant to the least significant. After digits, is the number that the first digits represent. This is the form that a machine uses: it needs multiplications and 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 . A row in radix of width represents the same number as the row of width in radix whose digits are the groups of consecutive digits of the first row.
Proof. Put the groups for . Each is between and , so it is a digit of radix . Then .
Examples
Conversion by division. Take and . The steps are , , , . The remainders are from the first step to the last. The row is , with the last remainder most significant. The table shows the invariant: .
| step | before | after | |||
|---|---|---|---|---|---|
| 1 | 2024 | 289 | 1 | 1 | |
| 2 | 289 | 41 | 2 | 2 | |
| 3 | 41 | 5 | 6 | 3 | |
| 4 | 5 | 0 | 5 | 4 |
Grouping. The number in radix is , width . To use radix , pad the row on the left to width : . The groups of three digits are , , , which are , , . So in radix , and .
Range of a MIX word. With and the magnitudes are to . With they are to . An index register has two bytes, so its magnitude is at most with , and with . The number in radix is , three digits: it does not fit in two bytes.
Number of digits. The number has binary digits, and has .
Exercises
- [10] Write in radix , radix and radix .
- [10] What is the least width in radix for the number ? Give its digits.
- [M15] Show that the sum of the digits of in radix is congruent to modulo .
- [M20] Prove that a row of width whose most significant digit is not represents a number that is at least .
- [M20] Show that the conversion by division of takes divisions for .
- [M22] Prove that Horner evaluation gives the value of the row, by an invariant for the loop.
- [15] Convert the radix row to radix by grouping, then to radix .
- [M25] Let . Find the number of rows of width , and the number of these rows with a nonzero most significant digit.
- [M23] Prove that the conversion of a fraction to radix by repeated multiplication is exact in radix after steps if and only if is an integer.
- [20] The row is stored with 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 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 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 places holds 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 and a row of digits that represents a magnitude with . Its value is for and for . The values and are two words with the same value .
Definition P7.1.10 (twos complement). Let . A row of bits represents the number with . Its twos complement value is when and when .
Definition P7.1.11 (ones complement). With the same row, the ones complement value is when and when .
Lean: Mathlib has Int.toNat, Int.natAbs, and the type Fin (2 ^ n) with the arithmetic modulo ; 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 digits the values are the integers from to . In ones complement with bits they are the integers from to , and the value has two rows. In twos complement with bits they are the integers from to , and each has one row.
Proof. The first claim is the definition. For the twos complement, the rows with give , and the rows with give from to . These values are distinct, so each has one row. For the ones complement, gives and gives from to . The row of all zeros and the row of all ones both have value .
Theorem P7.1.13 (twos complement is arithmetic modulo ). Let and be integers in the twos complement range, with rows and . The row has the twos complement value when is in the range. When is out of the range, the value of this row is , the one that falls in the range.
Proof. The row of an integer in the range satisfies , since or . So . The value of a row is the unique integer in the range that is congruent to it modulo . If is in the range, it is this integer. The sum is between and , so if it is out of the range, adding or subtracting brings it into the range.
Definition P7.1.14 (overflow). The sum of two words overflows when the integer is not a value of the word.
Theorem P7.1.15 (the sign rule of the overflow). In twos complement, the sum of and overflows if and only if and 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 and . Then , so there is no overflow, and the rule says the same because the signs differ. Let . The sum is between and . It overflows exactly when . Then the row of the result is with , so its top bit is , and the signs differ. Without overflow the row is with top bit . The case is the same: the sum is between and , it overflows exactly when it is below , and then the result is at least , with the top bit .
Theorem P7.1.16 (negation). In twos complement, the row of is the row of with each bit inverted, plus one, modulo .
Proof. Inverting each bit of gives . Adding gives . The one case where is out of the range is , and the rule maps this row to itself.
Definition P7.1.17 (a MIX word). A MIX word has a sign and five bytes of radix (with or in the book). Its magnitude is and it is below . Its value is the magnitude with the sign. The addition of an integer to a word with sign has the result of magnitude , with the sign of , or the sign when , and the overflow flag set when .
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 has , so its value is . In ones complement the same row has the value . The row of is . The inversion of is , and , as the negation theorem says.
Overflow, 4 bits. The range is to . The table has the sum of the values, the row of the result and its value.
| value of the result row | overflow | |||
|---|---|---|---|---|
| 5 | 3 | 8 | yes | |
| 5 | 4 | 9 | yes | |
| 7 | yes | |||
| no | ||||
| 7 | 1 | 8 | yes | |
| 7 | yes | |||
| 3 | no |
In each row with overflow the two operands have the same sign and the result has the other sign. In the row the sum is exactly , which is in the range.
An 8 bit sum. The sum is out of the range to . The row of the result is , with the value . The overflow rule agrees: both operands are positive and the result is negative.
MIX overflow. With take the word with sign and all five bytes , so its value is . Adding gives . The magnitude of the result is , the sign is , and the flag is set. The result is the word with overflow. In the same radix, the word plus the integer gives the sum , which has no sign, so the result keeps the old sign and the flag is clear.
Exercises
- [10] What are the twos complement and the ones complement values of the 8 bit row ?
- [10] Write as an 8 bit twos complement row.
- [M15] Show that in twos complement with bits the negation of is itself, and that this is the only such case apart from .
- [M20] Prove that in ones complement the row of is the row of with each bit inverted.
- [M20] Show that the ones complement value of is congruent to modulo , for .
- [15] List the pairs of 3 bit twos complement values with out of range, for .
- [M22] Prove that in twos complement the overflow of a sum holds if and only if the carry into the top bit differs from the carry out of the top bit.
- [M25] Let . 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 and .
- [20] How many different words does MIX have with , and how many different values?
- [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 (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 , 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 to from the left. Position is the sign, and positions to are the bytes . A field is a pair with . It names the positions . The first byte position is , the length is bytes, and the shift is , the number of bytes to the right of the field.
Example P7.1.19. The field has , , . The field has , , , and it contains the sign. The field has , , : it is the sign alone, with no byte.
Example P7.1.20. The code of a field in an instruction is . So has the code , has , has and has . The code is unique because .
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 be a word with magnitude . The field magnitude of is . The field sign is the sign of if 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 equals the number that the bytes represent in radix . For it is .
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 . Dividing by and taking the floor removes the terms with , that is , because their sum is less than . This is the bound of the theorem on the range of a word, for the bytes . The terms that stay have the weights for , so . Taking the remainder modulo keeps the terms with , that is , and the terms with have a weight that is a multiple of . What stays is , which is the number of the row .
Definition P7.1.23 (load). The load of the field of a word 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 into the field of the word is the word whose magnitude is with the sign of when and the sign of otherwise. Here and are the magnitudes. The low bytes of replace the field. The other bytes and, when , the sign of , 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 from the result of the store of into of has magnitude and sign equal to the sign of when , and otherwise.
Lean: TAOCP.MIX.Word.load_store (proved in the project).
Proof. Let , and . The new magnitude is . Here and . Dividing by gives , since is a multiple of and . Then is a multiple of , so the remainder modulo is , because . The sign follows from the definition of the field sign, applied to the sign of the result.
Theorem P7.1.26 (store keeps the rest). The store changes no byte outside the field. Precisely, the new magnitude and have the same quotient by and the same remainder modulo .
Lean: TAOCP.MIX.Word.store_high and TAOCP.MIX.Word.store_low (proved in the project).
Proof. The new magnitude is with , since and give . So the quotient by is , which is the quotient of . The remainder modulo is , since is a multiple of .
Corollary P7.1.27 (whole word). The load of is the word itself, and the store into gives the source word.
Lean: TAOCP.MIX.Word.load_whole and TAOCP.MIX.Word.store_whole.
Proof. For , , and the sign is the sign of the word. The field magnitude is since . In the store formula, the first term is since , and the remaining terms are and .
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 and it is below . A word fits a half word when its bytes are zero.
Lean: TAOCP.MIX.Half, Half.mag, Half.mag_lt, Word.fitsHalf, Word.toHalf, Half.toWord.
Examples
Load. Take and the word with magnitude . The field has and . The field magnitude is , and the bytes give . The field has the sign, so its value is . The field has the value . The field has the bytes and the value . The field has the plus sign and the value .
Store. Let and . Store into : , . The two low bytes of are . They go to the positions and . The result is , and the sign of stays. Store into : , . The low bytes go to the positions and , and the sign of is taken, so the result is . Store into : the result is .
The formula. For the first store, and . So . The two low bytes of are , so , and . The low part is . The sum is . Its bytes are .
Half word. With a half word holds a magnitude up to . The word fits, with magnitude . The word does not fit, since .
Exercises
- [10] Give , and for the fields , , and .
- [10] With , find the value of the field of the word .
- [M15] Show that two different fields have two different codes .
- [15] Store the word into the field of . What is the result?
- [M20] Show that the store of into the field of with never changes the sign of .
- [M22] Prove that storing into the field and then storing into a disjoint field gives the same word in either order, when both fields have .
- [M20] Prove that the store formula gives a magnitude below .
- [15] Which fields have ? What does a store into one of them do?
- [M25] Let be a word, and let be the load of from , as a word. Show that storing into of gives back, when . Does the same hold when ?
- [20] The word has the bytes as the address. Write the formula that extracts the address as a half word and the formula that tests that fits a half word, with general.
- [M23] Count the fields of a MIX word, and count those that have 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 and the code , 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 , 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 .
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.