问题 5.9 综合了计算机组成原理、数字逻辑和离散数学中的关键概念,旨在帮助学生理解二进制算术运算的硬件实现、逻辑门与算术运算的关系,以及如何使用数学方法来验证数字系统的正确性。它强调了从规范到实现再到验证的完整过程。
思想
- 函数抽象: num ( α n ) \operatorname{num}(\alpha_{n}) num(αn):将二进制字符串映射到非负整数
- 递归: num ( α n + 1 ) = a n + 1 2 n + 1 + num ( α n ) \operatorname{num}(\alpha_{n + 1})=a_{n + 1}2^{n + 1}+\operatorname{num}(\alpha_{n}) num(αn+1)=an+12n+1+num(αn)
- 用求和位和进位来表示两个二进制数的加法:
思路
- 加法器的抽象规范–>加法器的电路实现–>数学方法验证实现是否符合规范
拓展问题
- 问题5.9都考察了哪些知识点?讲到了哪些重要的结论?
- 如何将问题5.9的结论推广到十进制?
Problem 5.9
For any binary string α \alpha α, let num ( α ) \operatorname{num}(\alpha) num(α) be the nonnegative integer it represents in binary notation (possibly with leading zeroes). For example, num ( 10 ) = 2 \operatorname{num}(10)=2 num(10)=2, and num ( 0101 ) = 5 \operatorname{num}(0101)=5 num(0101)=5.
An n + 1 n + 1 n+1-bit adder adds two n + 1 n + 1 n+1-bit binary numbers. More precisely, an n + 1 n + 1 n+1-bit adder takes two length n + 1 n + 1 n+1 binary strings
α n : : = a n . . . a 1 a 0 , β n : : = b n . . . b 1 b 0 , \begin{align*} \alpha_{n}&::=a_{n}...a_{1}a_{0},\\ \beta_{n}&::=b_{n}...b_{1}b_{0}, \end{align*} αnβn::=an...a1a0,::=bn...b1b0,
and a binary digit C 0 C_{0} C0 as inputs, and produces a length- ( n + 1 ) (n + 1) (n+1) binary string
σ n : : = s n . . . s 1 s 0 , \sigma_{n}::=s_{n}...s_{1}s_{0}, σn::=sn...s1s0,
and a binary digit c n + 1 c_{n + 1} cn+1 as outputs, and satisfies the specification:
num ( α n ) + num ( β n ) + c 0 = 2 n + 1 c n + 1 + num ( σ n ) . (5.9) \operatorname{num}(\alpha_{n})+\operatorname{num}(\beta_{n})+c_{0}=2^{n + 1}c_{n + 1}+\operatorname{num}(\sigma_{n}). \tag{5.9} num(αn)+num(βn)+c0=2n+1cn+1+num(σn).(5.9)
There is a straightforward way to implement an n + 1 n + 1 n+1-bit adder as a digital circuit: an n + 1 n + 1 n+1-bit ripple-carry circuit
has 1 + 2 ( n + 1 ) 1 + 2(n + 1) 1+2(n+1) binary inputs
a n , . . . , a 1 , a 0 , b n , . . . , b 1 , b 0 , c 0 , a_{n},...,a_{1},a_{0},b_{n},...,b_{1},b_{0},c_{0}, an,...,a1,a0,bn,...,b1,b0,c0,
and n + 2 n + 2 n+2 binary outputs,
c n + 1 , s n , . . . , s 1 , s 0 . c_{n + 1},s_{n},...,s_{1},s_{0}. cn+1,sn,...,s1,s0.
As in Problem 3.6, the ripple-carry circuit is specified by the following formulas:
s i : : = a i ⊕ b i ⊕ c i c i + 1 : : = ( a i ∧ b i ) ∨ ( a i ∧ c i ) ∨ ( b i ∧ c i ) \begin{align*} s_{i}&::=a_{i}\oplus b_{i}\oplus c_{i} \tag{5.10}\\ c_{i + 1}&::=(a_{i} \land b_{i}) \lor (a_{i} \land c_{i}) \lor (b_{i} \land c_{i}) \tag{5.11} \end{align*} sici+1::=ai⊕bi⊕ci::=(ai∧bi)∨(ai∧ci)∨(bi∧ci)(5.10)(5.11)
for 0 ≤ i ≤ n 0\leq i\leq n 0≤i≤n, where we follow the convention that 1 1 1 corresponds to T and 0 0 0 corresponds to F.
(a) Verify that definitions (5.10) and (5.11) imply that
a n + b n + c n = 2 c n + 1 + s n . (5.12) a_{n}+b_{n}+c_{n}=2c_{n + 1}+s_{n}. \tag{5.12} an+bn+cn=2cn+1+sn.(5.12)
for all n ∈ N n \in \mathbb{N} n∈N.
证明:对任意的 n ∈ N n\in\mathbb{N} n∈N,
- 左边 = a n + b n + c n \text{左边}=a_n+b_n+c_n 左边=an+bn+cn。
- 可用 1 1 1 位的行波进位加法器来实现。它的输入为 a n a_n an, b n b_n bn, c n c_n cn,输出为 c n + 1 c_{n+1} cn+1, s n s_n sn。
- 根据 (5.10) 和 (5.11) 有, s n = a n ⊕ b n ⊕ c n s_{n}=a_{n}\oplus b_{n}\oplus c_{n} sn=an⊕bn⊕cn, c n + 1 = ( a n ∧ b n ) ∨ ( b n ∧ c n ) ∨ ( c n ∧ a n ) c_{n+1}=(a_{n}\land b_{n})\lor(b_{n}\land c_{n})\lor(c_{n}\land a_{n}) cn+1=(an∧bn)∨(bn∧cn)∨(cn∧an)。
- 右边 = 2 c n + 1 + s n = 2 [ ( a n ∧ b n ) ∨ ( b n ∧ c n ) ∨ ( c n ∧ a n ) ] + a n ⊕ b n ⊕ c n \text{右边}=2c_{n+1}+s_n=2[(a_{n}\land b_{n})\lor(b_{n}\land c_{n})\lor(c_{n}\land a_{n})]+a_{n}\oplus b_{n}\oplus c_{n} 右边=2cn+1+sn=2[(an∧bn)∨(bn∧cn)∨(cn∧an)]+an⊕bn⊕cn。
下面使用真值表来验证。
a n a_{n} an | b n b_{n} bn | c n c_{n} cn | a n + b n + c n a_{n} + b_{n} + c_{n} an+bn+cn | s n = a n ⊕ b n ⊕ c n s_{n}=a_{n}\oplus b_{n}\oplus c_{n} sn=an⊕bn⊕cn | a n ∧ b n a_{n}\land b_{n} an∧bn | b n ∧ c n b_{n}\land c_{n} bn∧cn | c n ∧ a n c_{n}\land a_{n} cn∧an | c n + 1 = ( a n ∧ b n ) ∨ ( b n ∧ c n ) ∨ ( c n ∧ a n ) c_{n+1} = (a_{n} \land b_{n}) \lor (b_{n}\land c_{n})\lor (c_{n}\land a_{n}) cn+1=(an∧bn)∨(bn∧cn)∨(cn∧an) | 2 c n + 1 + s n 2c_{n+1} + s_{n} 2cn+1+sn |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
1 | 1 | 0 | 2 | 0 | 1 | 0 | 0 | 1 | 2 |
1 | 0 | 1 | 2 | 0 | 0 | 0 | 1 | 1 | 2 |
0 | 1 | 1 | 2 | 0 | 0 | 1 | 0 | 1 | 2 |
1 | 1 | 1 | 3 | 1 | 1 | 1 | 1 | 1 | 3 |
(b) Prove by induction on n n n that an n + 1 n + 1 n+1-bit ripple-carry circuit really is an n + 1 n + 1 n+1-bit adder, that is, its outputs satisfy (5.9).
Hint: You may assume that, by definition of binary representation of integers,
num ( α n + 1 ) = a n + 1 2 n + 1 + num ( α n ) . (5.13) \operatorname{num}(\alpha_{n + 1})=a_{n + 1}2^{n + 1}+\operatorname{num}(\alpha_{n}). \tag{5.13} num(αn+1)=an+12n+1+num(αn).(5.13)
证明:使用强归纳法。
归纳假设 P ( n ) P(n) P(n) 为公式 (5.9)。
基础情形: n = 0 n=0 n=0
- 左边 = num ( α 0 ) + num ( β 0 ) + c 0 \text{左边}=\operatorname{num}(\alpha_{0})+\operatorname{num}(\beta_{0})+c_{0} 左边=num(α0)+num(β0)+c0。
- 右边 = 2 c 1 + num ( σ 0 ) \text{右边}=2c_1+\operatorname{num}(\sigma_{0}) 右边=2c1+num(σ0)。
- 根据 n + 1 n+1 n+1位加法器的规范可知: α 0 = a 0 \alpha_0=a_0 α0=a0, β 0 = b 0 \beta_0=b_0 β0=b0, σ 0 = s 0 \sigma_0=s_0 σ0=s0。因为只有 1 1 1 位,所以 num ( α 0 ) = a 0 \operatorname{num}(\alpha_{0})=a_0 num(α0)=a0, num ( β 0 ) = b 0 \operatorname{num}(\beta_{0})=b_0 num(β0)=b0, num ( σ 0 ) = s 0 \operatorname{num}(\sigma_{0})=s_0 num(σ0)=s0。
- 左边 = num ( α 0 ) + num ( β 0 ) + c 0 = a 0 + b 0 + c 0 \text{左边}=\operatorname{num}(\alpha_{0})+\operatorname{num}(\beta_{0})+c_{0}=a_0+b_0+c_0 左边=num(α0)+num(β0)+c0=a0+b0+c0。
- 右边 = 2 c 1 + num ( σ 0 ) = 2 c 1 + s 0 \text{右边}=2c_1+\operatorname{num}(\sigma_{0})=2c_1+s_0 右边=2c1+num(σ0)=2c1+s0。
- 根据公式(5.12)可知, 左边 = 右边 \text{左边}=\text{右边} 左边=右边。
归纳步骤:
- 假设 P ( k ) P(k) P(k) 对所有 k ≤ n k\leq n k≤n 都成立。
- 当 k = n + 1 k=n+1 k=n+1 时, 左边 = num ( α n + 1 ) + num ( β n + 1 ) + c 0 \text{左边}=\operatorname{num}(\alpha_{n+1})+\operatorname{num}(\beta_{n+1})+c_{0} 左边=num(αn+1)+num(βn+1)+c0。
- 根据公式(5.13)有:
左边 = a n + 1 2 n + 1 + num ( α n ) + b n + 1 2 n + 1 + num ( β n ) + c 0 = ( a n + 1 2 n + 1 + b n + 1 2 n + 1 ) + ( num ( α n ) + num ( β n ) + c 0 ) = 2 n + 1 ( 2 c n + 2 + s n + 1 − c n + 1 ) + ( 2 n + 1 c n + 1 + num ( σ n ) ) = 2 n + 2 c n + 2 + 2 n + 1 s n + 1 − 2 n + 1 c n + 1 + 2 n + 1 c n + 1 + s n = 2 n + 2 c n + 2 + ( 2 n + 1 s n + 1 + num ( σ n ) ) = 2 n + 2 c n + 2 + num ( σ n + 1 ) = 右边 . \begin{align*} \text{左边}&=a_{n+1}2^{n+1}+\operatorname{num}(\alpha_{n})+b_{n+1}2^{n+1}+\operatorname{num}(\beta_{n})+c_0\\ &=(a_{n+1}2^{n+1}+b_{n+1}2^{n+1})+(\operatorname{num}(\alpha_{n})+\operatorname{num}(\beta_{n})+c_0)\\ &=2^{n+1}(2c_{n+2}+s_{n+1}-c_{n+1})+(2^{n+1}c_{n+1}+\operatorname{num}(\sigma_{n}))\\ &=2^{n+2}c_{n+2}+2^{n+1}s_{n+1}-2^{n+1}c_{n+1}+2^{n+1}c_{n+1}+s_n\\ &=2^{n+2}c_{n+2}+(2^{n+1}s_{n+1}+\operatorname{num}(\sigma_{n}))\\ &=2^{n+2}c_{n+2}+\operatorname{num}(\sigma_{n+1})\\ &=\text{右边}. \end{align*} 左边=an+12n+1+num(αn)+bn+12n+1+num(βn)+c0=(an+12n+1+bn+12n+1)+(num(αn)+num(βn)+c0)=2n+1(2cn+2+sn+1−cn+1)+(2n+1cn+1+num(σn))=2n+2cn+2+2n+1sn+1−2n+1cn+1+2n+1cn+1+sn=2n+2cn+2+(2n+1sn+1+num(σn))=2n+2cn+2+num(σn+1)=右边.
综上所述, P ( n ) P(n) P(n) 对任意的 n ∈ N n\in\mathbb{N} n∈N 都成立。