Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt2 Arithmetic & Logic Unit (ALU) ALU does the calculations “Everything else in the computer is there to service this unit” (!) Handles integers May handle floating point (real) numbers there may be a separate floating point unit (FPU) (“math co-processor”) or an on-chip separate FPU (486DX +) on Pentium (earlier slides) multiple ALUs and FPUs
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt3 ALU Inputs and Outputs requested arithmetic operation operands (in)operands (out) Status e.g overflow?
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt4 Integer Representation Positive numbers stored in binary e.g. 41= Only have 0 & 1 to represent everything No minus sign! No period! Exponent? Two approaches to integers Sign-Magnitude Twos complement unsigned integers? “counting numbers”? Is zero positive? (NO!)
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt5 Sign-Magnitude Approach Left most bit is sign bit 0 means positive 1 means negative +18 = -18 = Problems!! Need to consider both sign and magnitude in arithmetic Two representations of zero (+0 and -0) Leftmost bit is most significant bit (msb) Rightmost bit is least significant bit (lsb)
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt6 Two’s Complement +3 = +2 = +1 = +0 = -1 = -2 = -3 = subtract 1 subtract 1 ???? add 1
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt7 Aside re Binary Arithmetic You should already know that = 1 2 = 10 2 (or = 0 2 carry 1) = 00 2 carry 1 1 2 – 1 2 = 0 2 (no borrow), 0 2 – 1 2 = 1 2 borrow 1 – 1 2 = borrow 1 = = b i 2 i i=0 K-1 K bit binary number, e.g. K = 5 b2b2
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt8 Two’s Complement Benefits One representation of zero Arithmetic works easily (see later) Negating is fairly easy: 3 10 = bitwise complement gives Add = –3 10 one’s complement two’s complement
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt9 Two’s Complement Benefits What about negating a negative number ? – 3 10 = bitwise complement gives Add = 3 10 GOOD! – ( – 3 ) = 3
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt10 Two’s Complement Integers In n bits, can store integers from –2 n-1 to + 2 n-1 – 1 negpos
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt11 Range of Numbers 8 bit 2s complement +127 = = = = -2 7 16 bit 2s complement = = = = N bit 2s complement = 2 N = -2 N-1 Largest positive Smallest (?) negative
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt12 Negation Special Case 1 0 = Bitwise not Add 1 +1 Result if this bit is ignored: – 0 = 0 OK hmmm...
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt13 Carry vs. Overflow So... what about the ignored bit on the last slide? CARRY: is an artifact of performing addition always happens: for binary values, carry = 0 or 1 exists independently of computers! OVERFLOW: an exception condition that results from using computers to perform operations caused by restricting values to fixed number of bits occurs when result exceeds range for the number of bits important limitation of using computers! e.g. Y2K bug!
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt14 Overflow is Implementation Dependent! recall example: negation of 0 binary addition is performed – regardless of interpretation carry fixed number of bits to represent values for this addition the answer is: 0 with carry = 1
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt15 Unsigned Interpretation Overflow consider values as unsigned integers but = 256 ?? answer = 0 ??? OVERFLOW occurred! WHY? cannot represent 256 as an 8-bit unsigned integer!
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt16 Signed Interpretation No Overflow consider values as signed integers –1 + 1 = 0 answer is correct! OVERFLOW did not occur (even though carry =1!) WHY? can represent 0 as an 8-bit signed integer! – 1 10
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt17 Negation Special Case 2 – 128 = bitwise not Add 1 to lsb +1 Result Whoops! – (– 128) = – 128 Need to monitor msb (sign bit) It should change during negation? Problem! OVERFLOW! what about negating zero?
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt18 Conversion Between Lengths, e.g. 8 16 Positive number: add leading zeros +18 = = Negative numbers: add leading ones -18 = = i.e. pack with msb (sign bit) called “sign extension”
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt19 Addition and Subtraction a – b = ? Normal binary addition Monitor sign bit of result for overflow Take twos complement of b and add to a i.e. a – b = a + (– b) So we only need addition and 2’s complement circuits
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt20 Hardware for Addition and Subtraction A – B = ? 2’s
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt21 Multiplication Complicated 1.Work out partial product for each digit 2.Take care with place value (column) 3.Add partial products Aside: Multiply by 2? (shift?)
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt22 Multiplication Example 1011 Multiplicand (11 10 ) x 1101 Multiplier (13 10 ) 1011 Partial products 0000 Note: if multiplier bit is copy multiplicand (place value) 1011 otherwise zero Product ( ) Note: need double length result – could easily overflow single word
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt23 Unsigned Binary Multiplication
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt24 Execution of 4-Bit Example M x Q = A,Q 1 add 0 no add 1 add
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt25 Flowchart for Unsigned Binary Multiplication number of bits
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt26 Multiplying Negative Numbers Multiplying negative numbers by this method does not work! Solution 1 Convert to positive if required Multiply as above If signs were different, negate answer Solution 2 Booth’s algorithm – not in scope of course
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt27 Division More complex than multiplication Negative numbers are really bad! Based on long division Aside: Divide by 2? (shift?)
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt Division of Unsigned Binary Integers Quotient Dividend Remainder Partial Remainders Divisor
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt29 Flowchart for Unsigned Binary Division differences with multiply? shift left subtract? no “C” add Quotient Remainder Divisor Subtract Shift left / Dividend
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt30 Real Numbers Numbers with fractions Could be done in pure binary = = Where is the binary point? Fixed? Very limited representation ability Moving? How do you show where it is? fixed point floating point
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt31 Normalization Floating Point (FP) numbers are usually normalized i.e. exponent is adjusted so that leading bit (msb) of significand is always 1 Since msb is always 1 there is no need to store it Recall scientific notation where numbers are normalized to give a single digit before the decimal point e.g x 10 3
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt32 Floating Point (Scientific Notation) +/ – 1.significand x 2 exponent Floating point misnomer: Point is actually fixed after first digit of the significand: 1.xxxxxx xxxxxx is stored signficand Exponent indicates place value (point position) Sign bit (Biased) exponent (stored) significand
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt33 Floating Point Examples Typo here! What's wrong?
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt34 Signs for Floating Point Significand: explicit sign bit (msb) rest of bits are magnitude (sort of) Exponent is in excess or biased notation 8 bit exponent field binary value range Excess (bias) 127 means –Subtract 127 to get correct value –Range -127 to +128 for k bit exponent: bias = 2 k–1 –1
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt35 FP Ranges Range: The range of numbers that can be represented For a 32 bit number 8 bit exponent +/ +/- 1.5 x Accuracy: The effect of changing lsb of significand 23 bit significand 1.2 x About 6 decimal places
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt36 Expressible Numbers 2 2
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt37 IEEE 754 Standard for floating point storage 32 and 64 bit standards 8 and 11 bit exponent respectively Extended formats (both significand and exponent) for intermediate results special cases: (table 9.4, page 318) 0 (how to normalize?) denormalized (for arithmetic) (how to quantify?) Not a number (NaN exception) How? use exponent = all 0’s zero, denormalized use exponent = all 1’s infinity, NAN reduces range! 2 –126 to 2 127
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt38 IEEE 754 Formats
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt39 FP Arithmetic +/- Check for zeros Align significands (adjusting exponents) denormalize! Add or subtract significands Normalize result
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt40 FP Addition & Subtraction Flowchart
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt41 FP Arithmetic x / Check for zero Add/subtract exponents Multiply/divide significands (watch sign) Normalize Round All intermediate results should be in double length storage
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt42 Floating Point Multiplication 1.xxxxx x 2 bias+a 1.yyyyy x 2 bias+b x
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt43 Floating Point Division