CS251 - Computer Organization and Design - Spring 2008
Lecture 10 - Addiing and Multiplying Integers
Practical Details
- Assignment 1, giving back.
- Ignore verilog
- One assignment 1 without a name
- No class Friday
- assignments in drop box before class time
- watch for assignment 3
Integers
Two's complement
Interpret the MSB as -2^31 instead of 2^31. Then,
- 10000...000 is -2^31, the smallest number
- 011111...111 is 2^32 - 1, the biggest number
- 1111...111 is -1, 0000...000 is zero, and 0000...001 is +1
Exercise for the reader
- Calculate -1 + 1 in two's complement.
- Addition and subtraction just work, except
- 0111...111 + 0000...001 =
- Wrap-around is still here
Negation
- Take an arbitrary two's complement number, y,
- Make a second two's complement number, x, by inverting all its bits
- Add them together
- The result is necessarily -1.
- y + x = -1 => -y = x + 1
Sign extension
Put an n-bit number in an n+m-bit slot
- Positive:
- obvious what to do
- fill from the left with zeros
- Negative
- -1 in 4-bit is 1111; -1 in 8-bit is 11111111
- -2 in 4-bit is one less, 1110; -2 in 8-bit is one less,
11111110
- and so on.
- fill from the left with ones
- Put them together
- fill from the left with the sign bit
This is called sign extension
Addition
Truth table for one bit addition
A B | S CO
=========
0 0 | 0 0
0 1 | 1 0
1 0 | 1 0
1 1 | 0 1
Extend for carry in
A B CI | S CO
=============
0 0 0 | 0 0
0 0 1 | 1 0
0 1 0 | 1 0
0 1 1 | 0 1
1 0 0 | 1 0
1 0 1 | 0 1
1 1 0 | 0 1
1 1 1 | 1 1
CO = (A XOR B)CI + AB
S = (A XOR B)(NOT CI) + (NOT(A XOR B))CI = (A XOR B) XOR CI
Ripple Adder
One-bit ALU
Use a multiplexor addressed by operation to get the right result
Four-bit ALU
- all logic combinations of 4x4
- addition and other operations with carry
Multiplication
Long multiplication, the algorithm you know and love from grade school
- Multiply the right most digit by the multiplicand
- result is first step toward product
- Repeat until no more digits
- multiply next digit by multiplicant
- add one to shifter
- shift result left by amount in shifter
- add to product
This is hard because multiplying base ten is hard
- What is 7 times 8?
- Was I carrying five or six?
But we can all multiply by 1 and 0, no problem, so try this algorithm
product is zero
Repeat 32 times
- If LSB of multiplier is 1 then
- add multiplicand to product
- Shift multiplier 1 bit right
- Shift multiplicand 1 bit left
Version 2
Shift product register instead of multiplicand register
Version 3
High word of product is zero; low word of product is multiplier
Repeat 32 times
- If LSB of product is 1 then
- add multiplicand to high word of product
- Shift product 1 bit right.
Return to: