Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

43
Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 1 See Stallings Chapter 9 Computer Arithmetic

Transcript of Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Page 1: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 1

See Stallings Chapter 9

Computer Arithmetic

Page 2: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 2

Arithmetic & Logic Unit (ALU)

ALU does the calculations

“Everything else in the computer is there to service this unit” (!)

Handles integers

May handle floating point (real) numbers

• there may be a separate floating point unit (FPU) (“math co-processor”)

• or an on-chip separate FPU (486DX +)

• on Pentium (earlier slides) multiple ALUs and FPUs

Page 3: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 3

ALU Inputs and Outputs

requested arithmetic operation

operands (in) operands (out)

Status e.g overflow?

Page 4: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 4

Integer Representation Positive numbers stored in binary

• e.g. 41=00101001

Only have 0 & 1 to represent everything

• No minus sign!

• No period!

• Exponent?

Two approaches to integers• Sign-Magnitude

• Twos complement

unsigned integers?

“counting numbers”?

Is zero positive?

(NO!)

Page 5: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 5

Sign-Magnitude Approach

Left most bit is sign bit

• 0 means positive

• 1 means negative

+18 = 00010010

-18 = 10010010

Problems!!

• Need to consider both sign and magnitude in arithmetic

• Two representations of zero (+0 and -0)

• Leftmost bit is most significant bit (msb)• Rightmost bit is least significant bit (lsb)

Page 6: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 6

Two’s Complement

+3 = 00000011

+2 = 00000010

+1 = 00000001

+0 = 00000000 00000000

-1 = 11111111 11111111

-2 = 11111110

-3 = 11111101

subtract 1

subtract 1 ???? add 1

Page 7: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 7

Aside re Binary ArithmeticYou should already know that

02+12 = 12

012+ 012 = 102 (or 12+12 = 02 carry 1)

112 + 12 = 002 carry 1

12 – 12 = 02 (no borrow), 02 – 12 = 12 borrow 1

00..002 – 12 = 11..112 borrow 1

000112 = 0•24 + 0•23 + 0•22 + 1•21 + 1•20 = bi 2ii=0

K-1

K bit binary number, e.g. K = 5

b2

Page 8: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 8

Two’s Complement Benefits

One representation of zero

Arithmetic works easily (see later)

Negating is fairly easy:

• 310 = 000000112

• bitwise complement gives 11111100

• Add 1 11111101 = –310

one’s complementtwo’s complement

Page 9: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 9

Two’s Complement Benefits

What about negating a negative number ?

• – 310 = 111111012

• bitwise complement gives 00000010

• Add 1 00000011 = 310

GOOD! – ( – 3 ) = 3

Page 10: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 10

Two’s Complement Integers

In n bits, can store integers from

–2n-1 to + 2n-1 – 1

neg pos

Page 11: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 11

Range of Numbers

8 bit 2s complement

• +127 = 01111111 = 27 -1

• -128 = 10000000 = -27

16 bit 2s complement

• +32767 = 01111111 11111111 = 215 - 1

• -32768 = 10000000 00000000 = -215

N bit 2s complement

• 011111111..11111111 = 2N-1 - 1

• 100000000..00000000 = -2N-1

Largest positive

Smallest (?) negative

Page 12: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 12

Negation Special Case 1

0 = 00000000

Bitwise not 11111111

Add 1 +1

Result 1 00000000

if this bit is ignored:

– 0 = 0 OK

hmmm . . .

Page 13: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 13

Carry vs. Overflow So . . . what about the ignored bit on the last slide?

CARRY: is an artifact of performing addition

• always happens: for binary values, carry = 0 or 1

• exists independently of computers!

OVERFLOW: an exception condition that results from using computers to perform operations

• caused by restricting values to fixed number of bits

• occurs when result exceeds range for the number of bits

• important limitation of using computers!e.g. Y2K bug!

Page 14: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 14

Overflow is Implementation Dependent!

recall example: negation of 0

binary addition is performed – regardless of interpretation

11111111

+1

1 00000000

carryfixed number

of bits torepresent

values

for this additionthe answer is:

0 with carry = 1

Page 15: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 15

Unsigned Interpretation Overflow

consider values as unsigned integers

11111111

+1

1 00000000

but . . . 255 + 1 = 256 ??

answer = 0 ???

OVERFLOW occurred!

WHY? cannot represent 256 as an 8-bit unsigned integer!

25510

Page 16: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 16

Signed Interpretation No Overflow

consider values as signed integers

11111111

+1

1 00000000

–1 + 1 = 0

answer is correct!

OVERFLOW did not occur (even though carry =1!)

WHY? can represent 0 as an 8-bit signed integer!

– 110

Page 17: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 17

Negation Special Case 2

– 128 = 10000000

bitwise not 01111111

Add 1 to lsb +1

Result 10000000

Whoops!

– (– 128) = – 128

Need to monitor msb (sign bit)

It should change during negation?

Problem! OVERFLOW!

what about negating zero?

Page 18: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 18

Conversion Between Lengths, e.g. 8 16

Positive number: add leading zeros

• +18 = 00010010

• +18 = 00000000 00010010

Negative numbers: add leading ones

• -18 = 11101110

• -18 = 11111111 11101110

i.e. pack with msb (sign bit) called “sign extension”

Page 19: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 19

Addition and Subtraction

a – b = ?

Normal binary addition

Monitor sign bit of result for overflow

Take twos complement of b and add to a

• i.e. a – b = a + (– b)

So we only need addition and 2’s complement circuits

Page 20: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 20

Hardware for Addition and Subtraction

A – B = ?2’s

Page 21: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 21

Multiplication

Complicated

1. Work out partial product for each digit

2. Take care with place value (column)

3. Add partial products

Aside: Multiply by 2? (shift?)

Page 22: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 22

Multiplication Example

1011 Multiplicand (1110)

x 1101 Multiplier (1310)

1011 Partial products

0000 Note: if multiplier bit is 1

1011 copy multiplicand (place value)

1011 otherwise zero

10001111 Product (14310)

Note: need double length result – could easily overflow single word

Page 23: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 23

Unsigned Binary Multiplication

Page 24: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 24

Execution of 4-Bit ExampleM x Q = A,Q

1 add

0 no add

1 add

1 add

Page 25: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 25

Flowchart for Unsigned Binary Multiplication

number of bits

Page 26: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 26

Multiplying Negative Numbers

Multiplying negative numbers by this method does not work!

Solution 1

• Convert to positive if required

• Multiply as above

• If signs were different, negate answer

Solution 2

• Booth’s algorithm – not in scope of course

Page 27: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 27

Division

More complex than multiplication

Negative numbers are really bad!

Based on long division

Aside: Divide by 2? (shift?)

Page 28: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 28

001111

Division of Unsigned Binary Integers

1011

00001101

100100111011001110

1011

1011100

Quotient

Dividend

Remainder

PartialRemainders

Divisor

Page 29: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 29

Flowchart for Unsigned Binary Division

differences with multiply?

shift left

subtract?

no “C”

add

Quotient

Remainder

Divisor

Subtract

Shift left

/ Dividend

Page 30: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 30

Real Numbers

Numbers with fractions

Could be done in pure binary

• 1001.1010 = 23 + 20 + 2-1 + 2-3 = 9.625

Where is the binary point?

Fixed?

• Very limited representation ability

Moving?

• How do you show where it is?

fixed point

floating point

Page 31: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 31

Normalization

Floating Point (FP) numbers are usually normalized

• i.e. exponent is adjusted so that leading bit (msb) of significand is always 1

Since msb is always 1 there is no need to store it

Recall scientific notation where numbers are normalized to give a single digit before the decimal point

e.g. 3.123 x 103

Page 32: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 32

Floating Point (Scientific Notation)

+/ – 1.significand x 2exponent

Floating point misnomer: Point is actually fixed after first digit of the significand: 1.xxxxxx

• xxxxxx is stored signficand

Exponent indicates place value (point position)

Sig

n bi

t

(Biased)exponent

(stored) significand

Page 33: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 33

Floating Point Examples

Typo here!What's wrong?

Page 34: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 34

Signs for Floating Point

Significand: explicit sign bit (msb)

• rest of bits are magnitude (sort of)

Exponent is in excess or biased notation

• 8 bit exponent field

• binary value range 0-255

• Excess (bias) 127 means

– Subtract 127 to get correct value

– Range -127 to +128

for k bit exponent:bias = 2k–1 –1

Page 35: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 35

FP Ranges

Range: The range of numbers that can be represented

• For a 32 bit number

• 8 bit exponent

• +/- 2127 +/- 1.5 x 1038

Accuracy: The effect of changing lsb of significand

• 23 bit significand 2-23 1.2 x 10-7

• About 6 decimal places

Page 36: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 36

Expressible Numbers

22

Page 37: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 37

IEEE 754

Standard for floating point storage

32 and 64 bit standards

8 and 11 bit exponent respectively

Extended formats (both significand and exponent) for intermediate results

special cases: (table 9.4, page 318)

• 0 (how to normalize?)

• denormalized (for arithmetic)

(how to quantify?)

• Not a number (NaN exception)

How?use exponent = all 0’s zero, denormalized use exponent = all 1’sinfinity, NAN reduces range! 2 –126 to 2127

Page 38: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 38

IEEE 754 Formats

Page 39: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 39

FP Arithmetic +/-

Check for zeros

Align significands (adjusting exponents)

denormalize!

Add or subtract significands

Normalize result

Page 40: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 40

FP Addition & Subtraction Flowchart

Page 41: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 41

FP Arithmetic x /

Check for zero

Add/subtract exponents

Multiply/divide significands (watch sign)

Normalize

Round

All intermediate results should be in double length storage

Page 42: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 42

Floating Point Multiplication

1.xxxxx x 2bias+a

1.yyyyy x 2bias+b

x

Page 43: Oct. 18, 2007SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.

Oct. 18, 2007 SYSC 2001* - Fall 2007. SYSC2001-Ch9.ppt 43

Floating Point Division