Number Systems and Arithmetic Jason Mars Thursday, January 24, 13 - - PowerPoint PPT Presentation

number systems and arithmetic
SMART_READER_LITE
LIVE PREVIEW

Number Systems and Arithmetic Jason Mars Thursday, January 24, 13 - - PowerPoint PPT Presentation

Number Systems and Arithmetic Jason Mars Thursday, January 24, 13 What do all those bits mean? bits (011011011100010 ....01) data instruction number text chars .............. R-format I-format ... integer floating point single precision


slide-1
SLIDE 1

Number Systems and Arithmetic

Jason Mars

Thursday, January 24, 13

slide-2
SLIDE 2

What do all those bits mean?

bits (011011011100010 ....01) instruction R-format I-format ... data number text chars .............. integer floating point signed unsigned single precision double precision ... ... ... ...

Thursday, January 24, 13

slide-3
SLIDE 3

Questions About Numbers

  • How do you represent
  • negative numbers?
  • fractions?
  • really large numbers?
  • really small numbers?
  • How do you
  • do arithmetic?
  • identify errors (e.g. overflow)?
  • What is an ALU and what does it look like?
  • ALU=arithmetic logic unit

Thursday, January 24, 13

slide-4
SLIDE 4

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

Thursday, January 24, 13

slide-5
SLIDE 5

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

1

Thursday, January 24, 13

slide-6
SLIDE 6

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

1

Thursday, January 24, 13

slide-7
SLIDE 7

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

1 1

Thursday, January 24, 13

slide-8
SLIDE 8

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

1 1

Thursday, January 24, 13

slide-9
SLIDE 9

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

1 1 1

Thursday, January 24, 13

slide-10
SLIDE 10

Introduction to Binary Numbers

  • Consider a 4 bit binary number
  • Examples of binary arithmetic

Binary Binary Decimal 0000 1 0001 2 0010 3 0011 Decimal 4 0100 5 0101 6 0110 7 0111 1 1 1 + 1 1 1 1 1 + 1 1

3 + 2 = 5 3 + 3 = 6

1 1 1 1

Thursday, January 24, 13

slide-11
SLIDE 11

Negative Numbers?

  • We would like a number system that provides
  • obvious representation of 0,1,2...
  • uses adder for addition
  • single value of 0
  • equal coverage of positive and negative numbers
  • easy detection of sign
  • easy negation

Thursday, January 24, 13

slide-12
SLIDE 12

Two’s Complement Representation

  • 2’s complement representation of negative numbers
  • Take the bitwise inverse and add 1
  • Biggest 4-bit Binary Number: 7

Smallest 4-bit Binary Number: -8

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

Thursday, January 24, 13

slide-13
SLIDE 13

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 +

Thursday, January 24, 13

slide-14
SLIDE 14

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1

Thursday, January 24, 13

slide-15
SLIDE 15

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1

Thursday, January 24, 13

slide-16
SLIDE 16

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1

Thursday, January 24, 13

slide-17
SLIDE 17

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1

Thursday, January 24, 13

slide-18
SLIDE 18

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1

Thursday, January 24, 13

slide-19
SLIDE 19

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1

Thursday, January 24, 13

slide-20
SLIDE 20

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1

Thursday, January 24, 13

slide-21
SLIDE 21

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1 1

Thursday, January 24, 13

slide-22
SLIDE 22

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1 1 1

Thursday, January 24, 13

slide-23
SLIDE 23

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1 1 1 1

Thursday, January 24, 13

slide-24
SLIDE 24

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1 1 1 1 1

Thursday, January 24, 13

slide-25
SLIDE 25

Two’s Complement Arithmetic (Subtraction)

2s Complement Binary 2s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 Decimal

  • 1
  • 2
  • 3

4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001

  • 4
  • 5
  • 6
  • 7

1000

  • 8

Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2

1 1 1 1 1 + 1 1 1 1 1 + 1 1 1 1 1 1 1 1

Thursday, January 24, 13

slide-26
SLIDE 26

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

Thursday, January 24, 13

slide-27
SLIDE 27

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

1

Thursday, January 24, 13

slide-28
SLIDE 28

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

11

Thursday, January 24, 13

slide-29
SLIDE 29

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

011

Thursday, January 24, 13

slide-30
SLIDE 30

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

011 1

Thursday, January 24, 13

slide-31
SLIDE 31

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

011 1

  • 5

Thursday, January 24, 13

slide-32
SLIDE 32

Some Things We Want To Know About Our Number System

  • Negation
  • Sign extension
  • +3 => 0011, 00000011, 0000000000000011
  • -3 => 1101, 11111101, 1111111111111101
  • Overflow detection

0101 5 + 0110 6

011 1

  • 5

Thursday, January 24, 13

slide-33
SLIDE 33

Overflow Detection

  • How do we detect overflow?
  • XOR Carry In and Carry Out of MSB

1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 7 3 1

  • 6
  • 4
  • 5

7 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 2 3 5

  • 4
  • 2
  • 6

1 1

Thursday, January 24, 13

slide-34
SLIDE 34

Overflow Detection

  • How do we detect overflow?
  • XOR Carry In and Carry Out of MSB

1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 7 3 1

  • 6
  • 4
  • 5

7 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 2 3 5

  • 4
  • 2
  • 6

1 1

OK

Thursday, January 24, 13

slide-35
SLIDE 35

Overflow Detection

  • How do we detect overflow?
  • XOR Carry In and Carry Out of MSB

1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 7 3 1

  • 6
  • 4
  • 5

7 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 2 3 5

  • 4
  • 2
  • 6

1 1

OK Not OK

Thursday, January 24, 13

slide-36
SLIDE 36

Arithmetic -- The Heart of Instruction Execution

32 32 32

  • peration

result a b

ALU

Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction

Thursday, January 24, 13

slide-37
SLIDE 37

Designing an Arithmetic Logic Unit

ALU N N N A B Result Overflow Zero 3 ALUop CarryOut ALU Control Lines (ALUop) Function 000 And 001 Or 010 Add 110 Subtract 111 Set-on-less-than

Thursday, January 24, 13

slide-38
SLIDE 38

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

Thursday, January 24, 13

slide-39
SLIDE 39

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

Thursday, January 24, 13

slide-40
SLIDE 40

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-41
SLIDE 41

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-42
SLIDE 42

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-43
SLIDE 43

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

Thursday, January 24, 13

slide-44
SLIDE 44

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-45
SLIDE 45

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-46
SLIDE 46

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-47
SLIDE 47

A One Bit ALU

  • This 1-bit ALU will perform AND, OR, and ADD

1 1 1 1 1 + 1 1 1

  • 4
  • 2
  • 6

1

1

Thursday, January 24, 13

slide-48
SLIDE 48

32 Bit ALU

1-bit ALU 32-bit ALU

Thursday, January 24, 13

slide-49
SLIDE 49

Relationship Between ISAs and ALUs

32 32 32

  • peration

result a b

ALU

Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction Thursday, January 24, 13

slide-50
SLIDE 50

Relationship Between ISAs and ALUs

  • After Decode
  • Implements All Arithmetic Execution
  • Inputs
  • Source Operands, Registers
  • Destination
  • Destination Operand

32 32 32

  • peration

result a b

ALU

Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction Thursday, January 24, 13

slide-51
SLIDE 51

Relationship Between ISAs and ALUs

  • After Decode
  • Implements All Arithmetic Execution
  • Inputs
  • Source Operands, Registers
  • Destination
  • Destination Operand
  • Will all come together with single cycle cpu, I promise ;-)

32 32 32

  • peration

result a b

ALU

Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction Thursday, January 24, 13

slide-52
SLIDE 52

The ALU’s Job - A Few Example Operations

Thursday, January 24, 13

slide-53
SLIDE 53

The ALU’s Job - A Few Example Operations

  • And, Or, Addition
  • add

Thursday, January 24, 13

slide-54
SLIDE 54

The ALU’s Job - A Few Example Operations

  • And, Or, Addition
  • add
  • Subtraction
  • sub

Thursday, January 24, 13

slide-55
SLIDE 55

The ALU’s Job - A Few Example Operations

  • And, Or, Addition
  • add
  • Subtraction
  • sub
  • Set-on-less-than
  • slt

Thursday, January 24, 13

slide-56
SLIDE 56

The ALU’s Job - A Few Example Operations

  • And, Or, Addition
  • add
  • Subtraction
  • sub
  • Set-on-less-than
  • slt
  • Branch-if-equal
  • beq

Thursday, January 24, 13

slide-57
SLIDE 57

Adding Subtraction

  • Keep in mind the following:
  • (A - B) is the same as: A + (-B)
  • 2’s Complement negate: Take the inverse of every bit and add 1
  • Bit-wise inverse of B is !B:
  • A - B = A + (-B) = A + (!B + 1) = A + !B + 1

Thursday, January 24, 13

slide-58
SLIDE 58

Adding Subtraction

  • Keep in mind the following:
  • (A - B) is the same as: A + (-B)
  • 2’s Complement negate: Take the inverse of every bit and add 1
  • Bit-wise inverse of B is !B:
  • A - B = A + (-B) = A + (!B + 1) = A + !B + 1

Thursday, January 24, 13

slide-59
SLIDE 59

Adding Subtraction

  • Keep in mind the following:
  • (A - B) is the same as: A + (-B)
  • 2’s Complement negate: Take the inverse of every bit and add 1
  • Bit-wise inverse of B is !B:
  • A - B = A + (-B) = A + (!B + 1) = A + !B + 1

Lets throw in NOR while we’re at it!

Thursday, January 24, 13

slide-60
SLIDE 60

Adding Subtraction

  • Keep in mind the following:
  • (A - B) is the same as: A + (-B)
  • 2’s Complement negate: Take the inverse of every bit and add 1
  • Bit-wise inverse of B is !B:
  • A - B = A + (-B) = A + (!B + 1) = A + !B + 1

Binvert a b CarryIn CarryOut Operation 1 2

  • Result

1 Ainvert 1

Lets throw in NOR while we’re at it!

Thursday, January 24, 13

slide-61
SLIDE 61

Adding Set-on-less-than

  • We are mostly there!
  • A < B = (A - B) < 0
  • If true, set LSB to 1, all others 0
  • Else, set all bits to 0

Thursday, January 24, 13

slide-62
SLIDE 62

Adding Set-on-less-than

  • We are mostly there!
  • A < B = (A - B) < 0
  • If true, set LSB to 1, all others 0
  • Else, set all bits to 0

Binvert a b CarryIn CarryOut Operation 1 2

  • Result

1 Ainvert 1 3 Less

Thursday, January 24, 13

slide-63
SLIDE 63

Adding Set-on-less-than

  • We are mostly there!
  • A < B = (A - B) < 0
  • If true, set LSB to 1, all others 0
  • Else, set all bits to 0

Binvert a b CarryIn CarryOut Operation 1 2

  • Result

1 Ainvert 1 3 Less

Binvert a b CarryIn Operation 1 2

  • Result

1 3 Less Overflow detection Set Overflow Ainvert 1

MSB

Thursday, January 24, 13

slide-64
SLIDE 64

Adding Set-on-less-than

  • We are mostly there!
  • A < B = (A - B) < 0
  • If true, set LSB to 1, all others 0
  • Else, set all bits to 0

1 1 1 1 1 + 1 1 1 1 1

What about overflow?

Binvert a b CarryIn CarryOut Operation 1 2

  • Result

1 Ainvert 1 3 Less

Binvert a b CarryIn Operation 1 2

  • Result

1 3 Less Overflow detection Set Overflow Ainvert 1

MSB

Thursday, January 24, 13

slide-65
SLIDE 65

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

Thursday, January 24, 13

slide-66
SLIDE 66

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->

Thursday, January 24, 13

slide-67
SLIDE 67

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->
  • -7 + (-6) = 3 -->

Thursday, January 24, 13

slide-68
SLIDE 68

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->
  • -7 + (-6) = 3 -->
  • MSB=0 -->

Thursday, January 24, 13

slide-69
SLIDE 69

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->
  • -7 + (-6) = 3 -->
  • MSB=0 -->
  • -7 > 6!!

Thursday, January 24, 13

slide-70
SLIDE 70

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 7 3 1

  • 6
  • 4
  • 5

7 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 2 3 5

  • 4
  • 2
  • 6

1 1 Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->
  • -7 + (-6) = 3 -->
  • MSB=0 -->
  • -7 > 6!!

Thursday, January 24, 13

slide-71
SLIDE 71

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 7 3 1

  • 6
  • 4
  • 5

7 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 2 3 5

  • 4
  • 2
  • 6

1 1

OK

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->
  • -7 + (-6) = 3 -->
  • MSB=0 -->
  • -7 > 6!!

Thursday, January 24, 13

slide-72
SLIDE 72

Overflow Detection

  • When is Overflow Possible?
  • Positive + Negative = Overflow Impossible
  • Negative + Positive = Overflow Impossible
  • Positive + Positive = Can Overflow
  • Negative + Negative = Can Overflow

1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 7 3 1

  • 6
  • 4
  • 5

7 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 2 3 5

  • 4
  • 2
  • 6

1 1

OK

Not OK

Decimal

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

1 2 3 4 5 6 7 Twos Complement Binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

  • A Problem for SLT ( -7 < 6 ) -->
  • -7 + (-6) = 3 -->
  • MSB=0 -->
  • -7 > 6!!

Thursday, January 24, 13

slide-73
SLIDE 73

Overflow Detection Logic

  • Carry into MSB ! = Carry out of MSB
  • For a N-bit ALU: Overflow = CarryIn[N - 1] XOR CarryOut[N - 1]

A0 B0 1-bit ALU Result0 CarryIn0 CarryOut0 A1 B1 1-bit ALU Result1 CarryIn1 CarryOut1 A2 B2 1-bit ALU Result2 CarryIn2 A3 B3 1-bit ALU CarryIn3 CarryOut3 Result3 CarryOut2 X Y X XOR Y 1 1 1 1 1 1

Thursday, January 24, 13

slide-74
SLIDE 74

Overflow Detection Logic

  • Carry into MSB ! = Carry out of MSB
  • For a N-bit ALU: Overflow = CarryIn[N - 1] XOR CarryOut[N - 1]

A0 B0 1-bit ALU Result0 CarryIn0 CarryOut0 A1 B1 1-bit ALU Result1 CarryIn1 CarryOut1 A2 B2 1-bit ALU Result2 CarryIn2 A3 B3 1-bit ALU CarryIn3 CarryOut3 Result3 CarryOut2 X Y X XOR Y 1 1 1 1 1 1 Overflow

Thursday, January 24, 13

slide-75
SLIDE 75

Adding Branch-if-equal

Thursday, January 24, 13

slide-76
SLIDE 76

Adding Branch-if-equal

  • Subtract to the rescue

again!

Thursday, January 24, 13

slide-77
SLIDE 77

Adding Branch-if-equal

  • Subtract to the rescue

again!

  • If (A - B = 0) --> A = B

Thursday, January 24, 13

slide-78
SLIDE 78

Adding Branch-if-equal

  • Subtract to the rescue

again!

  • If (A - B = 0) --> A = B
  • Zero Detection Logic is just
  • ne BIG NOR gate (for

equality test)

  • Any non-zero input to the

NOR gate will cause its

  • utput to be zero

Thursday, January 24, 13

slide-79
SLIDE 79

Adding Branch-if-equal

  • Subtract to the rescue

again!

  • If (A - B = 0) --> A = B
  • Zero Detection Logic is just
  • ne BIG NOR gate (for

equality test)

  • Any non-zero input to the

NOR gate will cause its

  • utput to be zero

. . . a0 Operation CarryIn ALU0 Less CarryOut b0 a1 CarryIn ALU1 Less CarryOut b1 Result0 Result1 a2 CarryIn ALU2 Less CarryOut b2 a31 CarryIn ALU31 Less b31 Result2 Result31 . . . . . . . . . Bnegate . . . Ainvert Overflow . . . Set CarryIn . . . . . . Zero Thursday, January 24, 13

slide-80
SLIDE 80

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-81
SLIDE 81

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

2

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-82
SLIDE 82

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

2 1 1 2

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-83
SLIDE 83

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

2 1 1 2

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-84
SLIDE 84

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

2 1 1 2 1

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-85
SLIDE 85

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

2 1 1 2 1 1 1 2

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-86
SLIDE 86

Full ALU

what signals accomplish: Binvert CIn Oper add? sub? and?

  • r?

beq? slt?

2 1 1 2 1 1 1 2 1 1 3

ALU a ALU operation b CarryOut Zero Result Overflow Thursday, January 24, 13

slide-87
SLIDE 87

The Disadvantage of Ripple Carry

  • The adder we just built is called a

“Ripple Carry Adder”

  • The carry bit may have to

propagate from LSB to MSB

  • Worst case delay for an N-bit

RC adder: 2N-gate delay A0 B0 1-bit ALU Result0 CarryOut0 A1 B1 1-bit ALU Result1 CarryIn1 CarryOut1 A2 B2 1-bit ALU Result2 CarryIn2 A3 B3 1-bit ALU Result3 CarryIn3 CarryOut3 CarryOut2 CarryIn0

a b CarryIn CarryOut

Thursday, January 24, 13

slide-88
SLIDE 88

The Disadvantage of Ripple Carry

  • The adder we just built is called a

“Ripple Carry Adder”

  • The carry bit may have to

propagate from LSB to MSB

  • Worst case delay for an N-bit

RC adder: 2N-gate delay A0 B0 1-bit ALU Result0 CarryOut0 A1 B1 1-bit ALU Result1 CarryIn1 CarryOut1 A2 B2 1-bit ALU Result2 CarryIn2 A3 B3 1-bit ALU Result3 CarryIn3 CarryOut3 CarryOut2 CarryIn0

a b CarryIn CarryOut

Faster Adders Possible: See C.6

Thursday, January 24, 13

slide-89
SLIDE 89

Teaser!! The ALU in Perspective

Thursday, January 24, 13

slide-90
SLIDE 90

Teaser!! The ALU in Perspective

Thursday, January 24, 13

slide-91
SLIDE 91

Teaser!! The ALU in Perspective Single Cycle CPU Next Time!!

Thursday, January 24, 13