Computer Arithmetic - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Computer Arithmetic

Description:

A byte is 8 bits. A word is 4 bytes (a doubleword is 8 bytes and a half-word is 2 bytes) ... first 23 bits of product, round up if the discarded bits exceed ' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 12
Provided by: rober52
Category:

less

Transcript and Presenter's Notes

Title: Computer Arithmetic


1
Computer Arithmetic
  • Readings
  • Appendix A (A.1 A.7) of Hennessy and
    Pattersons Computer Architecture A
    Quantitative Approach

2
Computer Arithmetic
  • Number storage (like memory and registers) is
    generally accomplished with bits, which are
    ON-OFF switches that represent binary digits
  • A byte is 8 bits
  • A word is 4 bytes (a doubleword is 8 bytes and a
    half-word is 2 bytes)
  • The bits represent 1s and 0s in the binary
    (base-2) number system

3
Integer Representation
  • Unsigned integers are represented as sequences of
    bits (e.g. 10010101 149 base 10)
  • Signed integers reserve the most significant bit
    as the sign bit -- if it is 1 it is negative
  • Three ways to represent negatives
  • Sign bit only
  • Ones complement (toggle all the bits)
  • Twos complement (toggle all the bits and add one
    to the number)
  • We prefer twos complement because it behaves
    nicely, especially with respect to zero (ones
    complement has two representations of zero, but
    not twos complement)

4
Integer Addition/Subtraction
  • String together a chain of full adders (one for
    each bit in the integers), linking the carry-out
    of one adder to the carry-in of the next
  • For addition, feed a 0 to the first adders
    carry-in
  • In a twos complement system, to compute a-b,
    simply add a to the ones complement of b and use
    a 1 as the carry-in digit
  • Note that with twos complement, the result of
    addition or subtraction is correct for the stored
    bits regardless of the carry-out from the highest
    order bit
  • Overflow occurs when the carry-out is different
    from the carry-in to the last adder

5
Integer Multiplication (unsigned)
  • Requires extra register P that is initially zero
  • Assume operands are in registers A and B
  • Repeat the following for each bit
  • If the least significant digit of A is 1, then
    add B to P otherwise, add 0 to P
  • Shift A one bit right, discarding the bit that
    falls off the end
  • Shift P one bit right, moving the bit that falls
    off the end to the high-order position of A
  • Product is in P and A, where A has the low-order
    bits and P the high-order bits

6
Integer Division (unsigned)
  • Again, assume that A and B contain the operands
    and P is an extra zero register
  • Repeat the following for each bit
  • Shift P one bit left, discarding anything that
    falls off the left end
  • Shift A one bit left, moving the bit that falls
    off the end to the low-order position in P
  • Subtract B from P
  • Set the low-order bit of A to the complement of
    the sign bit of P
  • If P is negative, add B back to P
  • Register A has quotient, P has remainder

7
Signed Multiplication?
  • A technique called Booth recoding allows precise
    treatment of negative multipliers in hardware
    see handout for details
  • Since division is slow anyway, signed division is
    implemented by (initially) ignoring the sign bit,
    doing an unsigned divide, and then calculating
    new sign bit

8
Brief Introduction to Floating Point Arithmetic
  • FP standard is IEEE 754
  • A single-precision (32-bit) number x is
    represented with a sign bit, a 23-bit fractional
    part f and an 8-bit biased exponent e such
    that x 1.f x 2e-127
  • Question Why would anyone want to write a biased
    exponent instead of twos complement?
  • Some numbers do not have exact floating point
    representations because of the base-2 exponent
  • x0.5 f 0 and e 126 (in binary)
  • What about x 0.3? 0.9?

9
Special FP Numbers
  • Infinity e 255, f 0 (sign bit applies)
  • NaN e 255, f gt 0
  • Result for things like sqrt(-1), 0/0, 0
    infinity
  • Zero e 0, f 0
  • Trick Question If zero is represented in this
    way with e0, then what is the representation of
    1 in IEEE 754?
  • Denormal numbers are used to represent very small
    quantities (i.e. smaller than 2-127)
  • A denormal number is indicated by e 0, f gt 0
  • Decimal equivalent is 0.f x 2-127

10
FP Multiplication
  • Simplest operation
  • Use an integer multiply on the fractional parts
    to obtain the new fractional part (take the first
    23 bits of product, round up if the discarded
    bits exceed 1000)
  • The XOR of the sign bits is the new sign
  • Add the biased exponents together and subtract
    127 to get the new exponent
  • Overflow occurs when the exponent addition
    overflows however, underflow does not always
    occur when the exponent addition underflows (the
    result may be a denormalized number)
  • Some machines use software to trap a denormalized
    result rather than complicate hardware

11
Other FP Operations
  • See the reading for details if you are interested
    I do not expect you to know them in great
    detail
  • Some highlights
  • In addition/subtraction, precision is maintained
    by shifting the operand with the smaller exponent
    to the right until its exponent is the same as
    the larger one
  • Rounding is similar to the case in multiplication
  • In division, the general algorithm to calculate
    a/b is to calculate 1/b using Newtons method,
    and then multiply by a to get a/b
Write a Comment
User Comments (0)
About PowerShow.com