signed numbers goals
play

Signed numbers Goals unsigned numbers - non-negative integers - PowerPoint PPT Presentation

Signed numbers Goals unsigned numbers - non-negative integers signed numbers - positive/negative numbers represent negative numbers Chapter 10: Signed Addition twos complement representation Many ways to represent signed numbers add &


  1. Signed numbers Goals unsigned numbers - non-negative integers signed numbers - positive/negative numbers represent negative numbers Chapter 10: Signed Addition two’s complement representation Many ways to represent signed numbers add & subtract two’s complement numbers Computer Structure - Spring 2004 identify overflow and negative result � Dr. Guy Even c Tel-Aviv Univ. – p.1 – p.2 – p.3 Representation of signed numbers Two’s complement - examples Two’s complement - story The number represented in sign-magnitude We denote the number represented in two’s The most common method for representing signed representation by A [ n − 1 : 0] ∈ { 0 , 1 } n and S ∈ { 0 , 1 } is complement representation by A [ n − 1 : 0] as follows: numbers is two’s complement. Why? adding, subtracting, and multiplying signed ( − 1) S · � A [ n − 1 : 0] � . = − 2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � . △ [ A [ n − 1 : 0]] numbers represented in two’s complement representation is almost as easy as performing these The number represented in one’s complement Examples: representation by A [ n − 1 : 0] ∈ { 0 , 1 } n is computations on unsigned (binary) numbers. [0 n ] = 0 . We will discuss addition & subtraction. [0 · x [ n − 2 : 0]] = � x [ n − 2 : 0] � . − (2 n − 1 − 1) · A [ n − 1] + � A [ n − 2 : 0] � . [1 · x [ n − 2 : 0]] = − 2 n − 1 + � x [ n − 2 : 0] � < 0 . DEF: Suppose that the string A represents the value x . The number represented in two’s complement ⇒ MSB indicates the sign. representation by A [ n − 1 : 0] ∈ { 0 , 1 } n is Negation means computing the string B that represents − x . [1 n ] = − 1 . � 1 · 0 n − 1 � − 2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � . = − 2 n − 1 . Question: Suggest circuit for negation with respect to sign- magnitude representation and one’s complement represen- tation. – p.4 – p.5 – p.6

  2. Two’s complement - notation Two’s complement - negation A circuit for negating a two’s complement number Claim: Claim: − [ A [ n − 1 : 0]] = [ INV ( A [ n − 1 : 0])] + 1 . − [ A [ n − 1 : 0]] = [ INV ( A [ n − 1 : 0])] + 1 . T n - the set of signed numbers that are representable in two’s complement representation using n -bit binary strings. Proof: Note that INV ( A [ i ]) = 1 − A [ i ] . Hence, A [ n − 1 : 0] n [ INV ( A [ n − 1 : 0])] = − 2 n − 1 · INV ( A [ n − 1]) + � INV ( A [ n − 2 : 0]) � Claim: inv ( n ) � − 2 n − 1 , − 2 n − 1 + 1 , . . . , 2 n − 1 − 1 � n − 2 △ = T n . � = − 2 n − 1 · (1 − A [ n − 1]) + (1 − A [ i ]) · 2 i n A [ n − 1 : 0] i =0 Question: Prove the claim. n − 2 n − 2 Remark: T n is not closed under negation: − 2 n − 1 ∈ T n but � � = − 2 n − 1 + + 2 n − 1 · A [ n − 1] − inc ( n ) 2 i A [ i ] · 2 i 2 n − 1 �∈ T n . i =0 i =0 n C [ n ] � �� � � �� � B [ n − 1 : 0] = − 1 = − [ A [ n − 1:0]] = − 1 − [ A [ n − 1 : 0]] . Question: [ B [ n − 1 : 0]] ? = − [ A [ n − 1 : 0]] QED. – p.7 – p.8 – p.9 A circuit for negating a two’s complement number - cont. A circuit for negating a two’s complement number - cont. A circuit for negating a two’s complement number - cont. The increment circuit computes: Counter example: A [ n − 1 : 0] n A [ n − 1 : 0] = 1 · 0 n − 1 . � A [ n − 1 : 0] � + 1 . A [ n − 1 : 0] inv ( n ) A [ n − 1 : 0] However, we should compute n A [ n − 1 : 0] = 0 · 1 n − 1 . n n � � A [ n − 1 : 0] + 1 . inv ( n ) A [ n − 1 : 0] inv ( n ) Increment yields C [ n ] = 0 and We know that n B [ n − 1 : 0] = 1 · 0 n − 1 = A [ n − 1 : 0] . inc ( n ) n A [ n − 1 : 0] A [ n − 1 : 0] � C [ n ] · B [ n − 1 : 0] � = � A [ n − 1 : 0] � + 1 . n C [ n ] inc ( n ) � � � � B [ n − 1 : 0] inc ( n ) Suppose we are “lucky” and C [ n ] = 0 . � � = ⇒ B � = − A . We will prove a theorem that will help us formulate and prove n C [ n ] n � B [ n − 1 : 0] � = � A [ n − 1 : 0] � + 1 . Reason? binary increment is not a two’s C [ n ] the correctness of the negation circuit. B [ n − 1 : 0] B [ n − 1 : 0] complement increment. Why should this imply that � � � Had to err: − �∈ T n . A � � [ B [ n − 1 : 0]] = A [ n − 1 : 0] + 1? – p.10 – p.11 – p.12

  3. � � Two’s complement - mod 2 n property Claim: mod ( � � � A � , 2 n ) = mod ( , 2 n ) A Two’s complement - sign extension Claim: For every A [ n − 1 : 0] ∈ { 0 , 1 } n Proof: Claim: If A [ n ] = A [ n − 1] , then � � mod ( � � A � , 2 n ) = mod ( � , 2 n ) . A A � , 2 n ) = mod (2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � , 2 n ) mod ( � � [ A [ n : 0]] = [ A [ n − 1 : 0]] . = mod ((2 n − 1 − 2 n ) · A [ n − 1] + � A [ n − 2 : 0] � , 2 n ) Note that Proof: = mod ( − 2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � , 2 n ) A � ∈ [0 , 2 n − 1] � � [ A [ n : 0]] = − 2 n · A [ n ] + � A [ n − 1 : 0] � � � � � ∈ [ − 2 n − 1 , 2 n − 1 − 1] . � , 2 n ) . � = − 2 n · A [ n ] + 2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � = mod ( A A = − 2 n · A [ n − 1] + 2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � ✷ Remark: Alternative definition of two’s comple- = − 2 n − 1 · A [ n − 1] + � A [ n − 2 : 0] � ment representation based on Claim. Namely, rep- = [ A [ n − 1 : 0]] . x ∈ [ − 2 n − 1 , 2 n − 1 − 1] by x ′ ∈ [0 , 2 n − 1] , resent where QED mod ( x, 2 n ) = mod ( x ′ , 2 n ) . – p.13 – p.14 – p.15 Two’s complement - sign extension Theorem - proof Theorem: signed addition �− → binary addition Binary addition: assume that functionality of FA n − 1 in RCA ( n ) = ⇒ Claim: If A [ n ] = A [ n − 1] , then � C [ n ] · S [ n − 1 : 0] � = � A [ n − 1 : 0] � + � B [ n − 1 : 0] � + C [0] . A [ n − 1] + B [ n − 1] + C [ n − 1] = 2 C [ n ] + S [ n − 1] [ A [ n : 0]] = [ A [ n − 1 : 0]] . ⇒ A [ n − 1] + B [ n − 1] = 2 C [ n ] − C [ n − 1] + S [ n − 1] . C [ n − 1] - carry-bit in position [ n − 1] associated with this binary addition. Corollary: We now expand z as follows: [ A [ n − 1] ∗ · A [ n − 1 : 0]] = [ A [ n − 1 : 0]] . △ z = [ A [ n − 1 : 0]] + [ B [ n − 1 : 0]] + C [0] = [ A [ n − 1 : 0]] + [ B [ n − 1 : 0]] + C [0] . z = − 2 n − 1 · ( A [ n − 1] + B [ n − 1]) = ⇒ sign-extension - duplicating the most significant bit does not + � A [ n − 2 : 0] � + � B [ n − 2 : 0] � + C [0] z > 2 n − 1 − 1 affect the value represented in two’s complement represen- C [ n − 1] − C [ n ] = 1 = ⇒ = − 2 n − 1 · (2 C [ n ] − C [ n − 1] + S [ n − 1]) + � C [ n − 1] · S [ n − 2 : 0] � z < − 2 n − 1 tation. This is similar to padding zeros from the left in binary C [ n ] − C [ n − 1] = 1 = ⇒ = − 2 n − 1 · (2 C [ n ] − C [ n − 1] − C [ n − 1]) + [ S [ n − 1] · S [ n − 2 : 0]] representation. z ∈ T n ⇐ ⇒ C [ n ] = C [ n − 1] = − 2 n · ( C [ n ] − C [ n − 1]) + [ S [ n − 1 : 0]] . z ∈ T n = ⇒ z = [ S [ n − 1 : 0]] . – p.16 – p.17 – p.18

  4. Theorem - proof - cont Detecting Overflow Overflow z = − 2 n · ( C [ n ] − C [ n − 1]) + [ S [ n − 1 : 0]] . The signal C [ n − 1] may not be available if one uses a △ DEF: Let z = [ A [ n − 1 : 0]] + [ B [ n − 1 : 0]] + C [0] . The signal We distinguish between three cases: “black-box” binary-adder (e.g., a library component in OVF is defined as follows: 1. If C [ n ] − C [ n − 1] = 1 , then which C [ n − 1] is an internal signal). � 1 if z �∈ T n z = − 2 n + [ S [ n − 1 : 0]] In this case we detect overflow based on the following △ = OVF 0 otherwise . claim. ≤ − 2 n + 2 n − 1 − 1 = − 2 n − 1 − 1 . Claim: 2. If C [ n ] − C [ n − 1] = − 1 , then overflow - sum is either too large or too small. XOR ( C [ n − 1] , C [ n ]) = XOR 4 ( A [ n − 1] , B [ n − 1] , S [ n − 1] , C [ n ]) . z = 2 n + [ S [ n − 1 : 0]] better term - out-of-range - not the common term. ≥ 2 n − 2 n − 1 = 2 n − 1 . By Theorem Proof: Recall that OVF = XOR ( C [ n − 1] , C [ n ]) . 3. If C [ n ] = C [ n − 1] , then z = [ S [ n − 1 : 0]] , and obviously C [ n − 1] = XOR 3 ( A [ n − 1] , B [ n − 1] , S [ n − 1]) . z ∈ T n . ✷ QED – p.19 – p.20 – p.21 Determining the sign of the sum Claim: NEG = XOR 3 ( A [ n − 1] , B [ n − 1] , C [ n ]) . Determining the sign of the sum - cont. How do we determine the sign of the sum z ? DEF: The signal NEG is defined as follows: Proof: The proof is based on playing the following “mental game”: Obviously, if z ∈ T n , then the sign-bit S [ n − 1] indicates � 1 if z < 0 whether z is negative. “extend” the computation to n + 1 bits. △ = NEG if z ≥ 0 . 0 = ⇒ overflow does not occur in extended precision. What happens if overflow occurs? = ⇒ the sum bit in position n indicates correctly the sign Question: Provide an example in which the sign of z is not Theorem implies that: of the sum z . signaled correctly by S [ n − 1] .  express this sum bit using n -bit addition signals. S [ n − 1] if no overflow   NEG = 1 if C [ n ] − C [ n − 1] = 1 We would like to be able to know whether z is negative re-   0 if C [ n − 1] − C [ n ] = 1 . gardless of whether overflow occurs. An even simpler method... – p.22 – p.23 – p.24

Recommend


More recommend