# Lecture 10 - Addiing and Multiplying Integers

## Practical Details

1. Assignment 1, giving back.
2. Ignore verilog
3. One assignment 1 without a name
4. 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

• Calculate -1 + 1 in two's complement.
• Addition and subtraction just work, except
• 0111...111 + 0000...001 =
• Wrap-around is still here

#### Negation

1. Take an arbitrary two's complement number, y,
2. Make a second two's complement number, x, by inverting all its bits
• The result is necessarily -1.
4. y + x = -1 => -y = x + 1

#### Sign extension

Put an n-bit number in an n+m-bit slot

1. Positive:
• obvious what to do
• fill from the left with zeros
2. 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
3. Put them together
• fill from the left with the sign bit

This is called sign extension

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
```

#### One-bit ALU

• AND
• OR
• PLUS

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

1. Multiply the right most digit by the multiplicand
• result is first step toward product
2. Repeat until no more digits
1. multiply next digit by multiplicant
3. shift result left by amount in shifter

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

1. If LSB of multiplier is 1 then
2. Shift multiplier 1 bit right
3. 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

1. If LSB of product is 1 then
• add multiplicand to high word of product
2. Shift product 1 bit right.
` `