BinaryArithmetic

92
ELEC 2200-002 Digital Logic Circuits Fall 2012 Binary Arithmetic (Chapter 1) Vishwani D. Agrawal James J. Danaher Professor Department of Electrical and Computer Engineering Auburn University, Auburn, AL 36849 http://www.eng.auburn.edu/~vagrawal [email protected] Fall 2012, Aug 20 . . Fall 2012, Aug 20 . . . ELEC2200-002 Lecture 2 ELEC2200-002 Lecture 2 1

description

BinaryArithmetic

Transcript of BinaryArithmetic

  • ELEC 2200-002Digital Logic CircuitsFall 2012Binary Arithmetic (Chapter 1)Vishwani D. AgrawalJames J. Danaher ProfessorDepartment of Electrical and Computer EngineeringAuburn University, Auburn, AL 36849http://www.eng.auburn.edu/[email protected]

    Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Why Binary Arithmetic?Hardware can only deal with binary digits, 0 and 1.Must represent all numbers, integers or floating point, positive or negative, by binary digits, called bits.Can devise electronic circuits to perform arithmetic operations: add, subtract, multiply and divide, on binary numbers.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Positive IntegersDecimal system: made of 10 digits, {0,1,2, . . . , 9} 41 = 4101 + 1100255 = 2102 + 5101 + 5100Binary system: made of two digits, {0,1}00101001= 027 + 026 + 125 + 024 +123 + 022 + 021 + 120= 32 + 8 +1 = 4111111111= 255, largest number with 8 binary digits, 28-1

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Base or RadixFor decimal system, 10 is called the base or radix.Decimal 41 is also written as 4110 or 41tenBase (radix) for binary system is 2.Thus,41ten= 1010012 or 101001twoAlso,111ten = 1101111two and 111two = 7tenWhat about negative numbers?

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Signed Magnitude What Not to DoUse fixed length binary representationUse left-most bit (called most significant bit or MSB) for sign:0 for positive1 for negativeExample: +18ten = 00010010two 18ten = 10010010two

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Difficulties with Signed MagnitudeSign and magnitude bits should be differently treated in arithmetic operations.Addition and subtraction require different logic circuits.Overflow is difficult to detect.Zero has two representations:+ 0ten = 00000000two 0ten = 10000000twoSigned-integers are not used in modern computers.

    ELEC2200-002 Lecture 2

  • Problems with Finite MathFinite size of representation:Digital circuit cannot be arbitrarily large.Overflow detection easy to determine when the number becomes too large.

    Represent negative numbers:Unique representation of 0.Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*-404 812162000000100 1000 11001000010100Infiniteuniverseof integers-4-bit numbers

    ELEC2200-002 Lecture 2

  • 4-bit UniverseFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Modulo-16(4-bit)universe16/084120100100011000000151111084120100100011000000-0111115-7770111-300010001Only 16 integers: 0 through 15, or 7 through 7

    ELEC2200-002 Lecture 2

  • One Way to Divide Universe1s Complement NumbersFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*084120100100011000000-0111115-770111-30001Negation rule: invert bits.

    Problem: 0 0

    Decimal magnitudeBinary numberPositiveNegative000001111100011110200101101300111100401001011501011010601101001701111000

    ELEC2200-002 Lecture 2

  • Another Way to Divide Universe2s Complement NumbersFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*084120100100011000000-1111115-870111-40001Negation rule: invert bits and add 1Subtract 1on this side

    Decimal magnitudeBinary numberPositiveNegative0000010001111120010111030011110140100110050101101160110101070111100181000

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Integers With Sign Two WaysUse fixed-length representation, but no explicit sign bit:1s complement: To form a negative number, complement each bit in the given number.2s complement: To form a negative number, start with the given number, subtract one, and then complement each bit, orfirst complement each bit, and then add 1.2s complement is the preferred representation.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*2s-Complement IntegersWhy not 1s-complement? Dont like two zeros.Negation rule:Subtract 1 and then invert bits, orInvert bits and add 1Some properties:Only one representation for 0Exactly as many positive numbers as negative numbersSlight asymmetry there is one negative number with no positive counterpart

    ELEC2200-002 Lecture 2

  • General Method for Binary Integers with SignSelect number (n) of bits in representation.Partition 2n integers into two sets:000 through 011 are 2n/2 positive integers.100 through 111 are 2n/2 negative integers.Negation rule transforms negative to positive, and vice-versa:Signed magnitude: invert MSB (most significant bit)1s complement: Subtract from 2n 1 or 11 (same as inverting all bits)2s complement: Subtract from 2n or 100 (same as 1s complement + 1) Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • Three Systems (n = 4)Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*00001000011111111010 = 2

    Signed magnitude0000100011111010 = 5

    1s complement integers 00101010101001112 26 5 000010001111100001010 = 6

    2s complement integers 101001116 6 0 00 7 8770 07 7 1

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Three RepresentationsSign-magnitude

    000 = +0001 = +1010 = +2011 = +3100 = - 0101 = - 1110 = - 2111 = - 32s complement

    000 = +0001 = +1010 = +2011 = +3100 = - 4101 = - 3110 = - 2111 = - 1

    (Preferred)1s complement

    000 = +0001 = +1010 = +2011 = +3100 = - 3101 = - 2110 = - 1111 = - 0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*2s Complement Numbers (n = 3)0+1+2+3-1-2-3- 4000001010011100101110111additionsubtractionPositive numbersNegative numbersOverflowNegation

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*2s Complement n-bit NumbersRange: 2n 1 through 2n 1 1 Unique zero: 00000000 . . . . . 0Negation rule: see slide 11 or 13.Expansion of bit length: stretch the left-most bit all the way, e.g., 11111101 is still 101 or 3. Also, 00000011 is same as 011 or 3.Most significant bit (MSB) indicates sign.Overflow rule: If two numbers with the same sign bit (both positive or both negative) are added, the overflow occurs if and only if the result has the opposite sign.Subtraction rule: for A B, add B and A.

    ELEC2200-002 Lecture 2

  • SummaryFor a given number (n) of digits we have a finite set of integers. For example, there are 103 = 1,000 decimal integers and 23 = 8 binary integers in 3-digit representations.We divide the finite set of integers [0, rn 1], where radix r = 10 or 2, into two equal parts representing positive and negative numbers.Positive and negative numbers of equal magnitudes are complements of each other: x + complement (x) = 0.Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • Summary: Defining ComplementDecimal integers:10s complement: x = Complement (x) = 10n x 9s complement: x = Complement (x) = 10n 1 x For 9s complement, subtract each digit from 9For 10s complement, add 1 to 9s complement Binary integers:2s complement: x = Complement (x) = 2n x 1s complement: x = Complement (x) = 2n 1 x For 1s complement, subtract each digit from 1For 2s complement, add 1 to 1s complement

    Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • Understanding ComplementComplement means something that completes:e.g., X + complement (X) = Whole.Complement also means opposite, e.g., complementary colors are placed opposite in the primary color chart.Complementary numbers are like electric charges. Positive and negative charges of equal magnitudes annihilate each other.Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • 2s-Complement NumbersFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*. . . -1 0 1 2 3 4 5 . . .000001 010 011 100101Infiniteuniverseof integers-0004995001000001999501FiniteUniverse of3-digitDecimalnumbers0000111001000001111101FiniteUniverseof 3-bitbinarynumbers

    ELEC2200-002 Lecture 2

  • Examples of ComplementsDecimal integers (r = 10, n = 3):10s complement: 50 = Compl (50) = 103 50 = 950; 50 + 950 = 1,000 = 0 (in 3 digit representation)9s complement: 50 = Compl (50) = 10n 1 50 = 949; 50 + 949 = 999 = 0 (in 9s complement rep.) Binary integers (r = 2, n = 4):2s complement: 5 = Complement (5) = 24 5 = 1110 or 1011; 5 + 11 = 16 = 0 (in 4-bit representation)1s complement: 5 = Complement (5) = 24 1 5 = 1010 or 1010; 5 + 10 = 15 = 0 (in 1s complement representation)

    Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*2s-Complement to Decimal Conversionbn-1 bn-2 . . . b1 b0 = 2n-1bn-1 + 2i bii=0n-2-128 64 32 16 8 4 2 1

    8-bit conversion box-128 64 32 16 8 4 2 1 1 1 1 1 1 1 0 1Example 128 + 64 + 32 + 16 + 8 + 4 + 1 = 128 + 125 = 3

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*For More on 2s-ComplementChapter 4 in D. E. Knuth, The Art of Computer Programming: Seminumerical Algorithms, Volume II, Second Edition, Addison-Wesley, 1981.A. alKhwarizmi, Hisab al-jabr wal-muqabala, 830.Read: A two part interview with D. E. Knuth, Communications of the ACM (CACM), vol. 51, no. 7, pp. 35-39 (July), and no. 8, pp. 31-35 (August), 2008.Donald E. Knuth (1938 - )Abu Abd-Allah ibn Musa alKhwarizmi (~780 850)

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*AdditionAdding bits: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = (1) 0Adding integers:carry0 0 0 . . . . . . 0 1 1 1 two = 7ten+ 0 0 0 . . . . . . 0 1 1 0 two = 6ten= 0 0 0 . . . . . . 1 (1)1 (1)0 (0)1 two = 13ten1 1 0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*SubtractionDirect subtraction

    Twos complement subtraction by adding0 0 0 . . . . . . 0 1 1 1 two = 7ten 0 0 0 . . . . . . 0 1 1 0 two = 6ten= 0 0 0 . . . . . . 0 0 0 1two = 1ten0 0 0 . . . . . . 0 1 1 1 two = 7ten+ 1 1 1 . . . . . . 1 0 1 0 two = 6ten= 0 0 0 . . . . . . 0 (1) 0 (1) 0 (0)1 two = 1ten1 1 1 . . . . . . 1 1 0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Overflow: An ErrorExamples: Addition of 3-bit integers (range - 4 to +3)

    -2-3 = -5 110 = -2 + 101 = -3 = 1011 = 3 (error)

    3+2 = 5 011 = 3 010 = 2 = 101 = -3 (error)

    Overflow rule: If two numbers with the same sign bit (both positive or both negative) are added, the overflow occurs if and only if the result has the opposite sign.0123-1-2-3- 4000001010011100101110111 +Overflowcrossing

    ELEC2200-002 Lecture 2

  • Overflow and Finite UniverseFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*. . .1111 0000 0001 0010 0011 0100 0101 . . .DecreaseIncreaseInfiniteuniverseof integersNo overflow-0000Forbidden fence1000000111111001FiniteUniverseof 4-bitbinaryintegers00100011010001010110011110101011110011011110IncreaseDecrease

    ELEC2200-002 Lecture 2

  • Adding Two BitsFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*SUMCARRY

    abs = a+bDecimalBinary00000011011010111210

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*HAHalf-Adder Adds two BitsAdding two bits:abhalf_sum carry_outh_s(a, b) c_o(a, b)00 0 001 1 010 1 011 0 1abhalf_sumcarry_outXORAND

    ELEC2200-002 Lecture 2

  • Adding Three BitsFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*SUMCARRY

    abcs=a+b+cDecimalBinary000000001101010101011210100101101210110210111311

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Full-Adder Adds Three BitsabXORANDXORANDORc_insumc_outFAh_s(a, b)c_o(a, b)h_s(h_s(a, b), c_in)c_o(h_s(a, b), c_in)HAHA

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*32-bit Ripple-Carry AdderFA0FA1FA2FA31 c0=0 a0 b0 a1 b1 a2 b2 a31 b31 sum0 sum1 sum2 sum31

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*How Fast is Ripple-Carry Adder?Longest delay path (critical path) runs from (a0, b0) to sum31.Suppose delay of full-adder is 100ps.Critical path delay = 3,200psClock rate cannot be higher than 1/(3,20010 12) Hz = 312MHz.Must use more efficient ways to handle carry.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Speeding Up the Adder16-bit ripple carry adder a0-a15 b0-b15 cin sum0-sum1516-bit ripple carry adder a16-a31 b16-b31 016-bit ripple carry adder a16-a31 b16-b31 1Multiplexer sum16-sum3101This is a carry-select adder

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Fast AddersIn general, any output of a 32-bit adder can be evaluated as a logic expression in terms of all 65 inputs.Number of levels of logic can be reduced to log2N for N-bit adder. Ripple-carry has N levels.More gates are needed, about log2N times that of ripple-carry design.Fastest design is known as carry lookahead adder.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*N-bit Adder Design OptionsReference: J. L. Hennessy and D. A. Patterson, Computer Architecture: A Quantitative Approach, Second Edition, San Francisco, California, 1990, page A-46.

    Type of adderTime complexity(delay)Space complexity(size)Ripple-carryO(N)O(N)Carry-lookaheadO(log2N)O(N log2N)Carry-skipO(N)O(N)Carry-selectO(N)O(N)

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Binary Multiplication (Unsigned) 1 0 0 0 two= 8tenmultiplicand 1 0 0 1 two= 9tenmultiplier____________

    1 0 0 0 0 0 0 0partial products 0 0 0 0 1 0 0 0____________ 1 0 0 1 0 0 0two= 72tenBasic algorithm: For n = 1, 32,only If nth bit of multiplier is 1,then add multiplicand 2 n 1to product

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*LSB of multiplier?Multiplication FlowchartInitialize product register to 0Partial product number, n = 1Left shift multiplicand register 1 bitRight shift multiplier register 1 bit n = ?n = n + 1DoneStartAdd multiplicand to product and place result in product register10n < 32n = 32

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Serial Multiplication 64-bit product register 64646464-bit ALU Test LSB32 timesshift right 32-bit multipliershift leftwrite3 operations per bit:shift rightshift leftaddNeed 64-bit ALUMultiplicand (expanded 64-bits)LSB = 0LSB= 1addshift

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Serial Multiplication (Improved)Multiplicand 64-bit product register 32323232-bit ALUTest LSB32 timesLSBshift right00000 . . . 00000 32-bit multiplierInitialized product registerwrite2 operations per bit:shift rightadd32-bit ALU11add

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example: 0010two 0011two0010two 0011two = 0110two, i.e., 2ten 3ten = 6ten

    IterationStepMultiplicandProduct0Initial values00100000 00111LSB =1 => Prod = Prod + Mcand 00100010 0011Right shift product00100001 00012LSB =1 => Prod = Prod + Mcand00100011 0001Right shift product00100001 10003LSB = 0 => no operation00100001 1000Right shift product00100000 11004LSB = 0 => no operation 00100000 1100Right shift product00100000 0110

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Multiplying with SignsConvert numbers to magnitudes.Multiply the two magnitudes through 32 iterations.Negate the result if the signs of the multiplicand and multiplier differed.Alternatively, the previous algorithm will work with some modifications. See B. Parhami, Computer Architecture, New York: Oxford University Press, 2005, pp. 199-200.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example 1: 1010two 0011two1010two 0011two = 101110two, i.e., 6ten 3ten = 18ten

    IterationStepMultiplicandProduct0Initial values1101000000 00111LSB = 1 => Prod = Prod + Mcand 1101011010 0011Right shift product1101011101 00012LSB = 1 => Prod = Prod + Mcand1101010111 0001Right shift product1101011011 10003LSB = 0 => no operation1101011011 1000Right shift product1101011101 11004LSB = 0 => no operation 1101011101 1100Right shift product1101011110 1110

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example 2: 1010two 1011two1010two 1011two = 011110two, i.e., 6ten ( 5ten) = 30ten*Last iteration with a negative multiplier in 2s complement.

    IterationStepMultiplicandProduct0Initial values1101000000 10111LSB =1 => Prod = Prod + Mcand 1101011010 1011Right shift product1101011101 01012LSB =1 => Prod = Prod + Mcand1101010111 0101Right shift product1101011011 10103LSB = 0 => no operation1101011011 1010Right shift product1101011101 11014LSB =1 => Prod = Prod Mcand*0011000011 1101Right shift product1101000001 1110

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Adding Partial Products y3y2y1y0multiplicand x3x2x1x0multiplier ________________________ x0y3x0y2x0y1x0y0four carryx1y3x1y2x1y1x1y0partial carryx2y3x2y2x2y1x2y0productscarry x3y3x3y2x3y1x3y0to be__________________________________________________ summed p7p6p5p4p3p2p1p0

    Requires three 4-bit additions. Slow.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Array Multiplier: Carry Forward y3y2y1y0multiplicand x3x2x1x0multiplier ________________________ x0y3x0y2x0y1x0y0four x1y3x1y2x1y1x1y0partial x2y3x2y2x2y1x2y0products x3y3x3y2x3y1x3y0to be__________________________________________________summed p7p6p5p4p3p2p1p0

    Note: Carry is added to the next partial product (carry-save addition).Adding the carry from the final stage needs an extra (ripple-carry stage. These additions are faster but we need four stages.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Basic Building BlocksTwo-input ANDFull-adderFull adder yi x0 p0i = x0yi 0th partial product sum bitto (k+1)th sum sum bit from (k-1)th sum yi xk carry bits from (k-1)th sum carry bits to (k+1)th sumSlide 24ith bit ofkth partialproduct

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Array Multiplier y3 y2 y1 y0x0x1x2x3FAxiyjppk ppk+1co000ci00000p7p6p5p4p3p2p1p0FAFAFAFACritical path0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Types of Array MultipliersBaugh-Wooley Algorithm: Signed product by twos complement addition or subtraction according to the MSBs.Booth multiplier algorithmTree multipliersReference: N. H. E. Weste and D. Harris, CMOS VLSI Design, A Circuits and Systems Perspective, Third Edition, Boston: Addison-Wesley, 2005.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Binary Division (Unsigned) 1 3 Quotient1 1 / 1 4 7Divisor / Dividend 1 1 3 7 Partial remainder 3 3 4 Remainder

    0 0 0 0 1 1 0 11 0 1 1 / 1 0 0 1 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 1 1 1 0 1 1 1 0 0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*4-bit Binary Division (Unsigned)Dividend: 6 = 0110Divisor: 4 = 0100 4 = 1100

    6 = 1, remainder 24 0 0 0 10 0 0 0 1 1 01 1 0 01 1 0 0negative quotient bit 00 1 0 0 restore remainder 0 0 0 0 1 1 0 1 1 0 0 1 1 0 1negative quotient bit 0 0 1 0 0 restore remainder 0 0 0 1 1 0 1 1 0 0 1 1 1 1negative quotient bit 0 0 1 0 0 restore remainder 0 0 1 1 0 1 1 0 0 0 0 1 0positive quotient bit 1Iteration 4 Iteration 3 Iteration 2 Iteration 1

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*32-bit Binary Division Flowchart $R = 0, $M = Divisor, $Q = Dividend, count = n Shift 1-bit left $R, $Q $R $R $M $R < 0?$Q0 = 1$Q0=0$R $R + $M count = count 1 count = 0?Done$Q = Quotient$R = RemainderStartYesYesNoNo$R and $M have one extra sign bit beyond 32 bits.Restore $R(remainder)$R (33 b) | $Q (32 b)

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*4-bit Example: 6/4 = 1, Remainder 2Remainder | Quotientcount43210

    Actionsn$R, $Q$M = DivisorInitialize40 0 0 0 0 | 0 1 1 00 0 1 0 0Shift left $R, $Q40 0 0 0 0 | 1 1 0 00 0 1 0 0Add $M (11100) to $R41 1 1 0 0 | 1 1 0 00 0 1 0 0Restore, add $M (00100) to $R30 0 0 0 0 | 1 1 0 00 0 1 0 0Shift left $R, $Q30 0 0 0 1 | 1 0 0 00 0 1 0 0Add $M (11100) to $R31 1 1 0 1 | 1 0 0 00 0 1 0 0Restore, add $M (00100) to $R20 0 0 0 1 | 1 0 0 00 0 1 0 0Shift left $R, $Q20 0 0 1 1 | 0 0 0 00 0 1 0 0Add $M (11100) to $R21 1 1 1 1 | 0 0 0 00 0 1 0 0Restore, add $M (00100) to $R10 0 0 1 1 | 0 0 0 00 0 1 0 0Shift left $R, $Q10 0 1 1 0 | 0 0 0 00 0 1 0 0Add $M (11100) to $R10 0 0 1 0 | 0 0 0 00 0 1 0 0Set LSB of $Q = 100 0 0 1 0 | 0 0 0 10 0 1 0 0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Initialize$R0Division33-bit $M (Divisor)33-bit $R (Remainder) 33333333-bit ALU32 timesStep 1: 1- bit left shift $R and $Q32-bit $Q (Dividend)Step 2:Subtract $R $R $MStep 3:If sign-bit ($R) = 0, set Q0 = 1If sign-bit ($R) = 1, set Q0 = 0 and restore $RV. C. Hamacher, Z. G. Vranesic and S. G. Zaky, Computer Organization, Fourth Edition,New York: McGraw-Hill, 1996.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example: 8/3 = 2, Remainder = 2Initialize$R = 0 0 0 0 0$Q = 1 0 0 0$M = 0 0 0 1 1

    Step 1, L-shift$R = 0 0 0 0 1$Q =0 0 0 0Step 2, Add $M =1 1 1 0 1$R =1 1 1 1 0Step 3, Set Q0$Q =0 0 0 0 Restore + $M =0 0 0 1 1 $R = 0 0 0 0 1

    Step 1, L-shift$R =0 0 0 1 0$Q =0 0 0 0$M =0 0 0 1 1Step 2, Add $M =1 1 1 0 1 $R =1 1 1 1 1Step 3, Set Q0$Q =0 0 0 0 Restore + $M =0 0 0 1 1 $R =0 0 0 1 0Iteration 2Iteration 1

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example: 8/3 = 2 (Remainder = 2) (Continued)$R =0 0 0 1 0$Q =0 0 0 0$M =0 0 0 1 1

    Step 1, L-shift$R = 0 0 1 0 0$Q =0 0 0 0$M =0 0 0 1 1Step 2, Add $M =1 1 1 0 1$R =0 0 0 0 1Step 3, Set Q0$Q =0 0 0 1

    Step 1, L-shift$R,Q =0 0 0 1 0$Q =0 0 1 0$M =0 0 0 1 1Step 2, Add $M =1 1 1 0 1 $R =1 1 1 1 1Step 3, Set Q0$Q =0 0 1 0 Final quotient Restore+ $M =0 0 0 1 1$R = 0 0 0 1 0Iteration 4Iteration 3Note Restore $R in Steps 1, 2 and 4. This method is known as the RESTORING DIVISION. An improved method, NON-RESTORINGDIVISION, is possible (see Hamacher, et al.)Remainder

    ELEC2200-002 Lecture 2

  • Non-Restoring DivisionAvoid unnecessary addition (restoration).How it works?Initially $R contains dividend 2 n for n-bit numbers. Example (n = 8):

    In some iteration after left shift, suppose $R = x and divisor is ySubtract divisor, $R = x y Restore: If $R is negative, add y, $R = xNext step: Left shift, $R = 2x+b, and subtract y, $R = 2x y + bFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*0010110100000000 00101101Dividend

    $R, $Q

    ELEC2200-002 Lecture 2

  • How It Works: Last two StepsSuppose we do not restore and go to next step:Left shift, $R = 2(x y) + b = 2x 2y + b, and add y, then $R = 2x 2y + y + b = 2x y + b (same result as with restoration)Non-restoring divisionInitialize and start iterations same as in restoring division by subtracting divisorIn any iteration after left shift and subtraction/additionIf $R is positive, subtract divisor (y) in next iterationIf $R is negative, add divisor (y) in next iterationAfter final iteration, if $R is negative then restore it by adding divisor (y)

    Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example: 8/3 = 2, Remainder = 2Non-Restoring DivisionInitialize$R = 0 0 0 0 0$Q = 1 0 0 0$M = 0 0 0 1 1

    Step 1, L-shift$R = 0 0 0 0 1$Q =0 0 0 0Step 2, Add $M =1 1 1 0 1$R =1 1 1 1 0$Q =0 0 0 0Step 3, Set Q0

    Step 1, L-shift$R =1 1 1 0 0$Q =0 0 0 0$M =0 0 0 1 1Step 2, Add + $M =0 0 0 1 1 $R =1 1 1 1 1$Q =0 0 0 0Step 3, Set Q0 Iteration 2Iteration 1Add + $M in next iteration

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example: 8/3 = 2 (Remainder = 2) Non-Restoring Division (Continued)$R =1 1 1 1 1$Q =0 0 0 0$M =0 0 0 1 1

    Step 1, L-shift$R = 1 1 1 1 0$Q =0 0 0 0$M =0 0 0 1 1Step 2, Add + $M =0 0 0 1 1$R =0 0 0 0 1 $Q =0 0 0 1Step 3, Set Q0

    Step 1, L-shift$R,Q =0 0 0 1 0$Q =0 0 1 0$M =0 0 0 1 1Step 2, Add $M =1 1 1 0 1 $R =1 1 1 1 1 $Q =0 0 1 0 Final quotient = 2Step 3, Set Q0 Restore+ $M =0 0 0 1 1$R = 0 0 0 1 0Iteration 4Iteration 3See, V. C. Hamacher, Z. G. Vranesic and Z. G. Zaky, Computer Organization, Fourth Edition, McGraw-Hill, 1996, Section 6.9, pp. 281-285.Remainder = 2

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Signed DivisionRemember the signs and divide magnitudes.Negate the quotient if the signs of divisor and dividend disagree.There is no other direct division method for signed division.

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Symbol RepresentationEarly versions (60s and 70s)Six-bit binary code (Control Data Corp., CDC)EBCDIC extended binary coded decimal interchange code (IBM)Presently used ASCII American standard code for information interchange 7 bit code specified by American National Standards Institute (ANSI), see Table 1.11 on page 63; an eighth MSB is often used as parity bit to construct a byte-code.Unicode 16 bit code and an extended 32 bit version

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*ASCIIEach byte pattern represents a character (symbol)Convenient to write in hexadecimal, e.g., with even parity,00000000 0ten00hexnull0100000165ten41hexA11100001225tenE1hexaTable 1.11 on page 63 gives the 7-bit ASCII code.C program string terminating with a null byte (odd parity):0100010101000011010001011000000069ten or 45hex 67ten or 43hex 69ten or 45hex128ten or 80hex E C E(null)

    ELEC2200-002 Lecture 2

  • Error Detection CodeErrors: Bits can flip due to noise in circuits and in communication.Extra bits used for error detection.Example: a parity bit in ASCII codeFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Even parity code for A01000001(even number of 1s)Odd parity code for A11000001(odd number of 1s)7-bit ASCII codeParity bitsSingle-bit error in 7-bit code of A, e.g., 1000101, will changesymbol to E or 1000000 to @. But error will be detected inthe 8-bit code because the error changes the specified parity.

    ELEC2200-002 Lecture 2

  • Richard W. HammingError-correcting codes (ECC).Also known forHamming distance (HD) = Number of bits two binary vectors differ inExample:HD(1101, 1010) = 3Hamming Medal, 1988Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*1915 -1998

    ELEC2200-002 Lecture 2

  • The Idea of Hamming CodeFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Code space contains 2N possible N-bit code words:1010A1110E1011B10008001021-bit error in AHD = 1HD = 1HD = 1HD = 1Error not correctable. Reason: No redundancy.Hammings idea: Increase HD between valid code words.N = 4CodeSymbol

    000000001100102001130100401015011060111710008100191010A1011B1100C1101D1110E1111F

    ELEC2200-002 Lecture 2

  • Hammings Distance 3 CodeFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*1010010 A1-bit error in Ashortest distancedecoding eliminateserrorHD = 2HD = 10010101 21000111 81011001 B1110100 EHD = 3HD = 3HD = 3HD = 40010010 ?HD = 3HD = 4HD = 40011110 3

    ELEC2200-002 Lecture 2

  • Minimum Distance-3 Hamming CodeFall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Original code: Symbol 0 with a single-bit error will be Interpreted as1, 2, 4 or 8.

    Reason: Hamming distance betweencodes is 1. A code with any bit error willmap onto another valid code.

    Remedy 1: Design codes with HD 2.Example: Parity code. Single bit errordetected but not correctable.

    Remedy 2: Design codes with HD 3.For single bit error correction, decodeas the valid code at HD = 1.

    For more error bit detection orcorrection, design code with HD 4.

    SymbolOriginal codeOdd-parity codeECC, HD 300000100000000000100010000100010112001000010001010130011100110011110401000010001001105010110101010110160110101100110011701110011101110008100001000100011191001110011001100A1010110101010010B1011010111011001C1100111001100001D1101011011101010E1110011101110100F1111111111111111

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Integers and Real NumbersIntegers: the universe is infinite but discreteNo fractionsNo numbers between consecutive integers, e.g., 5 and 6A countable (finite) number of items in a finite rangeReferred to as fixed-point numbersReal numbers the universe is infinite and continuousFractions represented by decimal notationRational numbers, e.g., 5/2 = 2.5Irrational numbers, e.g., 22/7 = 3.14159265 . . .Infinite numbers exist even in the smallest rangeReferred to as floating-point numbers

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Wide Range of NumbersA large number:976,000,000,000,000 = 9.76 1014A small number:0.0000000000000976 = 9.76 10 14

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Scientific NotationDecimal numbers0.513105, 5.13104 and 51.3103 are written in scientific notation.5.13104 is the normalized scientific notation.Binary numbersBase 2Binary point multiplication by 2 moves the point to the right.Normalized scientific notation, e.g., 1.0two2 1

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Floating Point NumbersGeneral format1.bbbbbtwo2eeee

    or(-1)S (1+F) 2EWhereS = sign, 0 for positive, 1 for negativeF = fraction (or mantissa) as a binary integer, 1+F is called significandE = exponent as a binary integer, positive or negative (twos complement)

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Binary to Decimal ConversionBinary(-1)S (1.b1b2b3b4) 2EDecimal(-1)S (1 + b12-1 + b22-2 + b32-3 + b42-4) 2EExample:-1.1100 2-2 (binary) = - (1 + 2-1 + 2-2) 2-2= - (1 + 0.5 + 0.25)/4

    = - 1.75/4= - 0.4375 (decimal)

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*William Morton (Velvel) Kahan1989 Turing Award Citation:

    For his fundamental contributions to numerical analysis. One of the foremost experts on floating-point computations. Kahan has dedicated himself to "making the world safe for numerical computations." Architect of the IEEE floating point standardb. 1933, CanadaProfessor of Computer Science, UC-Berkeley

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*NegativeOverflow

    PositiveOverflow

    Expressible numbersNumbers in 32-bit FormatsTwos complement integers

    Floating point numbers

    Ref: W. Stallings, Computer Organization and Architecture, Sixth Edition, Upper Saddle River, NJ: Prentice-Hall.-231231-10Expressible negativenumbers Expressible positivenumbers0-2-1272-127Positive underflowNegative underflow(2 2-23)2128- (2 2-23)2128Positive zeroNegative zero+

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*IEEE 754 Floating Point StandardBiased exponent: true exponent range[-126,127] is changed to [1, 254]:Biased exponent is an 8-bit positive binary integer.True exponent obtained by subtracting 127ten or 01111111twoFirst bit of significand is always 1: 1.bbbb . . . b 2E1 before the binary point is implicitly assumed.Significand field represents 23 bit fraction after the binary point.Significand range is [1, 2), to be exact [1, 2 2-23]

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Examples 1.1010001 210100 = 0 10010011 10100010000000000000000 = 1.6328125 220-1.1010001 210100 = 1 10010011 10100010000000000000000 = -1.6328125 2201.1010001 2-10100 = 0 01101011 10100010000000000000000 = 1.6328125 2-20-1.1010001 2-10100 = 1 01101011 10100010000000000000000 = -1.6328125 2-20Biased exponent (0-255), bias 127 (01111111) to be subtracted1.00.50.1250.00781251.6328125Sign bit8-bit biased exponent107 127= 2023-bit Fraction (F) of significand

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example: Conversion to DecimalSign bit is 1, number is negativeBiased exponent is 27+20 = 129The number is1 10000001 01000000000000000000000Sign bit S bits 23-30 bits 0-22 normalized EF(-1)S (1 + F) 2(exponent bias)= (-1)1 (1 + F) 2(129 127)= - 1 1.25 22= - 1.25 4= - 5.0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*NegativeOverflow

    PositiveOverflow

    IEEE 754 Floating Point FormatFloating point numbersExpressible negativenumbers Expressible positivenumbers0-2-1262-126Positive underflowNegative underflow(2 2-23)2127- (2 2-23)2127+ 1 1011001 01001100000000010001101Sign bit S bits 23-30 bits 0-22 normalized EFPositive integer 127 = E+0 0

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Positive Zero in IEEE 754+ 1.0 2127 Smallest positive number in single-precision IEEE 754 standard.Interpreted as positive zero.True exponent less than 126 is positive underflow; can be regarded as zero.0 00000000 00000000000000000000000Biased exponentFraction

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Negative Zero in IEEE 754 1.0 2127 Smallest negative number in single-precision IEEE 754 standard.Interpreted as negative zero.True exponent less than 126 is negative underflow; may be regarded as 0.1 00000000 00000000000000000000000Biased exponentFraction

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Positive Infinity in IEEE 754+ 1.0 2128 Largest positive number in single-precision IEEE 754 standard.Interpreted as + If true exponent = 128 and fraction 0, then the number is greater than . It is called not a number or NaN and may be interpreted as .0 11111111 00000000000000000000000Biased exponentFraction

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Negative Infinity in IEEE 7541.0 2128 Smallest negative number in single-precision IEEE 754 standard.Interpreted as - If true exponent = 128 and fraction 0, then the number is less than - . It is called not a number or NaN and may be interpreted as - .1 11111111 00000000000000000000000Biased exponentFraction

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Addition and Subtraction0.Zero check-Change the sign of subtrahend, i.e., convert to summation-If either operand is 0, the other is the result1.Significand alignment: right shift significand of smaller exponent until two exponents match.2.Addition: add significands and report error if overflow occurs. If significand = 0, return result as 0.3.Normalization-Shift significand bits to normalize.-report overflow or underflow if exponent goes out of range.4.Rounding

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example (4 Significant Fraction Bits)Subtraction: 0.5ten 0.4375tenStep 0:Floating point numbers to be added 1.000two 2 1 and 1.110two 2 2Step 1: Significand of lesser exponent is shifted right until exponents match 1.110two 2 2 0.111two 2 1Step 2: Add significands, 1.000two + ( 0.111two)Result is 0.001two 2 1 01000+11001 000012s complement addition, one bit added for sign

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*Example (Continued)Step 3: Normalize, 1.000two 2 4 No overflow/underflow since127 exponent 126Step 4: Rounding, no change since the sum fits in 4 bits.

    1.000two 2 4 = (1+0)/16 = 0.0625ten

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*FP Multiplication: Basic IdeaSeparate signAdd exponents (integer addition)Multiply significands (integer multiplication)Normalize, round, check overflow/underflowReplace sign

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*FP Multiplication: Step 0Multiply, X Y = ZX = 0?Y = 0?Z = 0ReturnSteps 1 - 5yesnoyesno

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*FP Multiplication IllustrationMultiply 0.5ten and 0.4375ten (answer = 0.21875ten) orMultiply 1.000two2 1 and 1.110two2 2Step 1: Add exponents1 + (2) = 3Step 2: Multiply significands 1.000 1.110 0000 1000 100010001110000Product is 1.110000

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*FP Mult. Illustration (Cont.)Step 3:Normalization: If necessary, shift significand right and increment exponent.Normalized product is 1.110000 2 3Check overflow/underflow: 127 exponent 126Step 4: Rounding: 1.110 2 3Step 5: Sign: Operands have opposite signs,Product is 1.110 2 3(Decimal value = (1+0.5+0.25)/8 = 0.21875ten)

    ELEC2200-002 Lecture 2

  • Fall 2012, Aug 20 . . .ELEC2200-002 Lecture 2*FP Division: Basic IdeaSeparate sign.Check for zeros and infinity.Subtract exponents.Divide significands.Normalize and detect overflow/underflow.Perform rounding.Replace sign.

    ELEC2200-002 Lecture 2

    *