Addition, Subtraction, and Multiplication

Tom Kelliher, CS 220

Oct. 15, 1997

Announcements

Assignment

Start reading Chapter 6.

Addition/Subtraction

Sign-Magnitude Addition

Addition algorithm for a plus b:

if a and b are of the same sign
{
   sign of sum = sign of a;
   magnitude of sum = magnitude of a + magnitude of b;
}
else if magnitude of a > magnitude of b
{
   sign of sum = sign of a;
   magnitude of sum = magnitude of a - magnitude of b;
}
else
{
   sign of sum = sign of b;
   magnitude of sum = magnitude of b - magnitude of a;
}
How do we handle subtraction?

Comparison slows this. Can we speed it up?

When can overflow occur? How do we detect it?

Two's-Complement

  1. Two's complement representation for n-bit numbers and the face of a clock with positions.

  2. An n-bit adder adds modulo .

  3. Addition examples for n=4:
    1. -4, 2.

    2. -2, 4.

    3. 3, 4.

    4. -3, -4.

    5. 4, 4.

  4. So, what's the addition algorithm?

  5. How do we subtract? Given a four-bit adder circuit, design an adder/subtractor.

  6. How do we detect overflow?

One's Complement

  1. The addition wheel.

  2. Adding one to skip the second zero.

  3. The wraparound carry in adder circuits.

Multiplication

  1. Paper and pencil multiplication: shift the multiplicand and scale by the appropriate multiplier bit.

  2. Unsigned binary example: in 3 bits.

  3. n-bit multiplicand, multiplier: 2n-bit result.

  4. Signed binary multiplication: sign-extend multiplicand multiplier to 2n bits.

  5. Examples in 3 bits: , .

Shifting the Multiplicand

Consequences:

  1. 2n-bit adder required.

  2. 2n bits of storage each required for multiplicand and partial product.

  3. n-bits of storage required for multiplier. (Why only n bits?)

  4. Next homework problem: Pseudo-code and SAL to multiply two 32-bit signed integers, producing a 64-bit result, by shifting the multiplicand.

Shifting the Partial Product

Observation: shifting multiplicand left is equivalent to shifting partial product right.

Consequences:

  1. Only an n-bit adder required.

  2. 2n bits of storage required for partial product.

  3. n bits of storage each required for multiplicand and multiplier.

Algorithm:

ms_sum = ls_sum = 0;

for (i = 0; i < 32; ++i)
{
   if (lsb of multiplier == 1)
      ms_sum += multiplicand;

   shift multiplier right one bit;
   shift ls_sum right one bit;

   if (lsb of ms_sum == 1)
      set msb of ls_sum;

   shift ms_sum right one bit;
}



Thomas P. Kelliher
Wed Oct 15 07:53:40 EDT 1997
Tom Kelliher