Chapter 3: Arithmetic for Computers - Kent State...

25
Chapter 3: Arithmetic for Computers

Transcript of Chapter 3: Arithmetic for Computers - Kent State...

Page 1: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Chapter 3:

Arithmetic for Computers

Page 2: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 2

Objectives

� Signed and Unsigned Numbers

� Addition and Subtraction

� Multiplication and Division

� Floating Point

Page 3: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 3

The Binary Numbering System

� A computer’s internal storage techniques are

different from the way humans represent

information in daily lives

Humans

� Decimal numbering system to rep real numbers

� Base-10

� Each position is a power of 10

3052 = 3 x 103 + 0 x 102 + 5 x 101 + 2 x 100

Page 4: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 4

� Information inside a digital computer is stored as

a collection of “binary data”

� Binary numbering system

� Base-2

� Built from ones and zeros

� Each position is a power of 2

1101 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20

� Digits 0,1 are called bits (binary digits)

Binary Representation of Numbers

Page 5: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 5

Binary Representation of Numbers

� 6-Digit Binary Number (111001)� 111001 = 1 x 25 + 1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20

= 32 + 16 + 8 + 0 + 0 + 1

= 57

� 5-Digit Binary Number (10111)� 10111 = 1 x 24 +0 x 23 + 1 x 22 + 1 x 21 + 1 x 20

= 16 + 0 + 4 + 2 +1

= 23

Page 6: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 6

Binary Representation of Numbers

� Computers use finite number of Bits for

Integer Storage

Size (“word”) Max Unsigned

Number Allowed

� 16 1x215 + 1x214 + …+1x21 +1x20

� MIPS-32 1x231 +1x230 + …+1x21 + 1x20

Otherwise Arithmetic Overflow

Page 7: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 7

32132132132100007654765476547654................................................................................31313131 30 29 2830 29 2830 29 2830 29 28

10110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0 0 00 0 0 00 0 0 00 0 0 0

Number Representation

MIPS wordExample: 11ten = 1 x 23 +0 x 22 + 1 x 21 + 1 x 20

= 1011two

Least-significant bitMost-significant bit

Page 8: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 8

Signed Number Representation

Sign/Magnitude Notation

Signed Number Examples: -49, +3, -8

Most significant Bit Stores sign

� 0 � +ve number

� 1 � -ve number

� Remaining Bits represent Magnitude of number

� -49

� +3

00010011001100110011000000000000000000000000000000000000000000000000000000000000000000000000000000001111 0 0 00 0 00 0 00 0 0

00110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0 0 00 0 00 0 00 0 0

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1 1 1 0 0 00 0 00 0 00 0 0

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0 0 0 0 0 00 0 00 0 00 0 0

Find the decimal value for the 32-bit sign/magnitude notation:

Page 9: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 9

Signed Number Representation

Two’s Complement Notation

� Leading 0s mean +ve

� Leading 1s mean -ve

00010000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000001111 0 0 00 0 00 0 00 0 0

1 x –231 + 0 X230 + …0x26 + 1x25 + 1x24 + 0x23 + 0x22+0x21 +1x20

= -2,147,483,648 + 32 +16 +1

= -2,147,483,609

Compare with sign/magnitude representation for -49

Page 10: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 10

cf: Sign Magnitude/ Two’s Complement Notations

Up Close

Sign Magnitude Two's Complement

000 = +0 000 = +0

001 = +1 001 = +1

010 = +2 010 = +2

011 = +3 011 = +3

100 = -0 100 = -4

101 = -1 101 = -3

110 = -2 110 = -2

111 = -3 111 = -1

Page 11: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 11

MIPS

� 32 bit signed numbers:

Two’s Complement Representation Value

0000 0000 0000 0000 0000 0000 0000 0000 = 0

0000 0000 0000 0000 0000 0000 0000 0001 = + 1

0000 0000 0000 0000 0000 0000 0000 0010 = + 2

...

0111 1111 1111 1111 1111 1111 1111 1110 = + 2,147,483,646

0111 1111 1111 1111 1111 1111 1111 1111 = + 2,147,483,647

1000 0000 0000 0000 0000 0000 0000 0000 = – 2,147,483,648

1000 0000 0000 0000 0000 0000 0000 0001 = – 2,147,483,647

1000 0000 0000 0000 0000 0000 0000 0010 = – 2,147,483,646

...

1111 1111 1111 1111 1111 1111 1111 1101 = – 3

1111 1111 1111 1111 1111 1111 1111 1110 = – 2

1111 1111 1111 1111 1111 1111 1111 1111 = – 1

Page 12: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 12

Two’s Complement Operation

� To Negate a Two's complement number:

� First invert all bits then

� Add 1 to the inverted bits

� To Convert n bit numbers into numbers with more than n bits:

� MIPS 16 bit immediate gets converted to 32 bits for

arithmetic

� copy the most significant bit (the sign bit) into the LHS half of

the word

0010 -> 0000 0010

1010 -> 1111 1010

Page 13: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 13

� Addition (carries 1s)

0000 0000 0000 0000 0000 0000 0000 0011 = + 3

0000 0000 0000 0000 0000 0000 0000 0010 = + 2

� Subtraction: use addition of negative numbers

0000 0000 0000 0000 0000 0000 0000 0011 = + 3

1111 1111 1111 1111 1111 1111 1111 1110 = - 2

� Overflow (if result too large to fit in the finite computer word of the result register)

� e.g., adding two n-bit numbers does not yield an n-bit number

0111

0001

1000

Addition and Subtraction

0000 0000 0000 0000 0000 0000 0000 0101 = + 5

0000 0000 0000 0000 0000 0000 0000 0001 = + 1

Page 14: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 14

Overflow

� No overflow when adding a positive and a negative number

� No overflow when signs are the same for subtraction

� Overflow occurs when the value affects the sign:

� overflow when adding two positives yields a negative

� or, adding two negatives gives a positive

� or, subtract a negative from a positive and get a negative

� or, subtract a positive from a negative and get a positive

Page 15: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 15

Multiplication

� Recall:1000ten

X 1001ten

10000000

000010001001000ten

Observations

� More storage required to store the product� Place copy of multiplicand in proper location if multiplier is a 1

� Place 0 in proper location if multiplier is 0� Product of n-bit Multiplicand and m-Multiplier is (n + m)-bit long� Number of steps (move digits to LHS) is n -1; where n rep the number of digits (1,0)

Let's examine 2 versions of multiplication algorithm for binary numbers

Multiplicand

Multiplier

Product

Page 16: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 16

MultiplicationVersion 1

Multiplicand

Shift left

64 bits

64-bit ALU

Product

Write

64 bits

Control test

Multiplier

Shift right

32 bits

32nd repetition?

1a. Add multiplicand to product and

place the result in Product register

Multiplier0 = 01. Test

Multiplier0

Start

Multiplier0 = 1

2. Shift the Multiplicand register left 1 bit

3. Shift the Multiplier register right 1 bit

No: < 32 repetitions

Yes: 32 repetitions

Done

DatapathControl

Page 17: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 17

MultiplicationRefined Version

Multiplicand

32 bits

32-bit ALU

ProductWrite

64 bits

Control

test

Shift right

32nd repetition?

Product0 = 01. Test

Product0

Start

Product0 = 1

3. Shift the Product register right 1 bit

No: < 32 repetitions

Yes: 32 repetitions

Done

Add multiplicand to bits 32

thru’ 63 in product register

and place the result in bits

32 thru’ 63 of product

register

Page 18: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 18

MultiplicationNegative Numbers

� Convert Multiplicand and Multiplier to Positive

Numbers

� Run the Multiplication algorithm for 31

iterations (ignoring the sign bit)

� Negate product only if original signs for

Multiplicand and Multiplier are different

Page 19: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 19

Floating Point (Overview)Binary Representation

� Floats Provide representation for:

� Decimal numbers, e.g., 3.1416

� Fractions, very small numbers, e.g., .000000001

� First Convert number to scientific notation:

+MxB+E M ~ mantissa; B ~ base (2) of exponent

E ~ exponent

Page 20: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 20

Floats

� Binary Representation:

� Example 5.75

� 5 has binary value 101

� .75 = ½ + ¼ = 2-1 + 2-2

= 0.11 (binary value)

� 5.75 = 101.11 x 20 (scientific notation)

� Normalize number:

� 5.75 = .10111 x 23 (i.e., 1/2 + 1/8 + 1/16 + 1/32)

Page 21: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 21

Floating PointMIPS Sign Magnitude Representation

32132132132100007654765476547654................................…………........22222222......... . . .. . . .. . . .. . . .31313131 30 29 2830 29 2830 29 2830 29 28

00000000000000000000000000000000000000000000000000001100110011001100111110110110110100010001000100010000 0000 0 00 00 00 0

Least-significant bitMost-significant bit

+5.75 = +.10111 x 2+3

General Sign Magnitude Representation(-1)sxFx2E

Min value of numbers is 2x10 -38

Max value of numbers is 2x1038

Show that:

How do you increase the precision?

Page 22: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 22

� Exponent is too large � Overflow

� Exponent too small � Underflow

� MIPS solution:

� Double Precision representation

� Combine two MIPS word

MIPS Representation

Overflow/Underflow/Double Precision

32132132132100007654765476547654................................…………....................................31 30 29 2831 30 29 2831 30 29 2831 30 29 28

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0 00 0 00 0 00 0 0

32103210321032107654765476547654................................20202020 …………....................................31313131 30 29 2830 29 2830 29 2830 29 28

00000000000000000000000000000000000011001100110011001111111101111000100010001000100000000000000000000 0000 0000

+5.75 = +.10111 x 2+3

Page 23: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 23

Observations so far:

� We can increase the precision by making leading 1 bit of the normalized number implicit

� Logically 24 bits (instead of 23) for the fractional part

� In our representation, the exponent of 2-1

� Looks like a large binary number

� On the other hand the exponent 2+1

� looks like a smaller binary number

� Let’s make the most negative exponent 0000000 and most positive 1111111

� Hence introduce a transformation (Bias)

� Single Precision (7 bits) � subtract 127 from exponent why 7 bits?

� Double Precision (10 bits) � subtract 1023 from exponent why 10 bits?

Floating PointIEEE 754 Representation

(-1)sx(1 + F)x2(E- Bias)IEEE 754 Binary Representation:

Page 24: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 24

� Show the IEEE 754 Single Precision Binary Representation of -0.75

� - 0.75 = -0.112x2-1

� Hence, normalized notation: -1.12x2-1 ------(a)

� cf (a) with generalized form:

� Then (a) becomes: (-1)1 x (1 + .10000…000) x 2(126 – 127)

Floating PointIEEE 754 Binary Representation

(-1)sx(1 + F)x2(E- Bias)

32132132132100007654765476547654................................…………........22222222......... . . .. . . .. . . .. . . .31313131 30 29 2830 29 2830 29 2830 29 28

00000000000000000000000000000000000000000000000000000000000000000000000010010010010011111111111111111111 0000 1 11 11 11 1

Page 25: Chapter 3: Arithmetic for Computers - Kent State …personal.kent.edu/~asamba/cs35101/CS-35101Chap03.pdfChapter 3: Arithmetic for Computers Computer Architecture CS 35101-002 2 Objectives

Computer Architecture CS 35101-002 25

� Show the IEEE 754 Double Precision Binary Representation of -0.75

� - 0.75 = -0.112x2-1

� Hence, normalized notation: -1.12x2-1 ------(a)

� cf (a) with generalized form:

� Then (a) becomes: (-1)1 x (1 + .10000…000) x 2(1022 – 1023)

Floating PointIEEE 754 Binary Representation

(-1)sx(1 + F)x2(E- Bias)

32103210321032107654765476547654................................…………....23 . 21 2023 . 21 2023 . 21 2023 . 21 2027 26 25 27 26 25 27 26 25 27 26 25 ....31313131 30303030 29 2829 2829 2829 28

0000000000000000000000000000000000000000000000000000000000000000000011 1 011 1 011 1 011 1 01 1111 1111 1111 1111111 0000 1 11 11 11 1

0000000000000000000000000000000000000000000000000000000000000000000000 0 000 0 000 0 000 0 00 0000 0000 0000 0000 0 0 00 0 0 00 0 0 00 0 0 0