William Stallings Computer Organization and Architecture ...lokeshchouhan.com/gallery/arithmetic...

67
William Stallings Computer Organization and Architecture 7 th Edition Chapter 9 Computer Arithmetic

Transcript of William Stallings Computer Organization and Architecture ...lokeshchouhan.com/gallery/arithmetic...

William Stallings

Computer Organization

and Architecture

7th

Edition

Chapter 9

Computer Arithmetic

Arithmetic & Logic Unit

• Does the calculations

• Everything else in the computer is there to service this unit

• Handles integers

• May handle floating point (real) numbers

• May be separate FPU (maths co-processor)

• May be on chip separate FPU (486DX +)

ALU Inputs and Outputs

Integer Representation

• Only have 0 & 1 to represent everything

• Positive numbers stored in binary

—e.g. 41=00101001

• No minus sign

• No period

• Sign-Magnitude

• Two’s compliment

Sign-Magnitude

• 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)

Two’s Compliment

• +3 = 00000011

• +2 = 00000010

• +1 = 00000001

• +0 = 00000000

• -1 = 11111111

• -2 = 11111110

• -3 = 11111101

Benefits

• One representation of zero

• Arithmetic works easily (see later)

• Negating is fairly easy

—3 = 00000011

—Boolean complement gives 11111100

—Add 1 to LSB 11111101

Geometric Depiction of Twos

Complement Integers

Negation Special Case 1

• 0 = 00000000

• Bitwise not 11111111

• Add 1 to LSB +1

• Result 1 00000000

• Overflow is ignored, so:

• - 0 = 0

Negation Special Case 2

• -128 = 10000000

• bitwise not 01111111

• Add 1 to LSB +1

• Result 10000000

• So:

• -(-128) = -128 X

• Monitor MSB (sign bit)

• It should change during negation

Range of Numbers

• 8 bit 2s compliment

—+127 = 01111111 = 27 -1

— -128 = 10000000 = -27

• 16 bit 2s compliment

—+32767 = 011111111 11111111 = 215 - 1

— -32768 = 100000000 00000000 = -215

Conversion Between Lengths

• Positive number pack with leading zeros

• +18 = 00010010

• +18 = 00000000 00010010

• Negative numbers pack with leading ones

• -18 = 10010010

• -18 = 11111111 10010010

• i.e. pack with MSB (sign bit)

Addition and Subtraction

• Normal binary addition

• Monitor sign bit for overflow

• Take twos compliment of substahend and add to minuend

—i.e. a - b = a + (-b)

• So we only need addition and complement circuits

Hardware for Addition and Subtraction

Multiplication

• Complex

• Work out partial product for each digit

• Take care with place value (column)

• Add partial products

Multiplication Example

• 1011 Multiplicand (11 dec)

• x 1101 Multiplier (13 dec)

• 1011 Partial products

• 0000 Note: if multiplier bit is 1 copy

• 1011 multiplicand (place value)

• 1011 otherwise zero

• 10001111 Product (143 dec)

• Note: need double length result

Unsigned Binary Multiplication

Execution of Example

Flowchart for Unsigned Binary

Multiplication

Multiplying Negative Numbers

• This does not work!

• Solution 1

—Convert to positive if required

—Multiply as above

—If signs were different, negate answer

• Solution 2

—Booth’s algorithm

Booth’s Algorithm

Step 1 for each pass

Use the LSB (least significant bit) and the previous LSB to determine the arithmetic action.

If it is the FIRST pass, use 0 as the previous LSB.

Possible arithmetic actions:

00 no arithmetic operation

01 add multiplicand to left half of product

10 subtract multiplicand from left half of product

11 no arithmetic operation

Example of Booth’s Algorithm

Example

For our example, multiply (-5) x 2

The numerically larger operand (5) would require

3 bits to represent in binary (101). So we must

use AT LEAST 4 bits to represent the operands, to

allow for the sign bit.

Let's use 5-bit 2's complement:

-5 is 11011 (multiplier) Q

2 is 00010 (multiplicand) M

Beginning Product

The multiplier Q is:

11011

Add 5 leading zeros to the multiplier to

get the beginning product:

A 00000 M 11011

Step 1 for each pass

Use the LSB (least significant bit) and the previous LSB to determine the arithmetic action.

If it is the FIRST pass, use 0 as the previous LSB.

Possible arithmetic actions:

00 no arithmetic operation

01 add multiplicand to left half of product

10 subtract multiplicand from left half of product

11 no arithmetic operation

Step 2 for each pass

Perform an arithmetic right shift(ASR) on the entire product.

NOTE: For X-bit operands, Booth's algorithm requires X passes.

Example

Let's continue with our example of multiplying

(-5) x 2

Remember:

-5 is 11011 (multiplier) Q

2 is 00010 (multiplicand) M

And we added 5 leading zeros to the

multiplier to get the beginning product:

A 00000 Q 11011

Example continued

Initial Product and previous LSB

00000 11011 0

(Note: Since this is the first pass, we use 0 for the

previous LSB)

Pass 1, Step 1: Examine the last 2 bits

00000 11011 0

The last two bits are 10, so we need to:

subtract the multiplicand from left half of product

Example: Pass 1 continued

Pass 1, Step 1: Arithmetic action

(1) 00000 (left half of product) A

-00010 (mulitplicand) M

11110 (uses a phantom borrow)

Place result into left half of product

A 11110 Q 11011 Q-1 0

Example: Pass 1 continued

Pass 1, Step 2: ASR (arithmetic shift right)

Before ASR

11110 11011 0

After ASR

11111 01101 1

(left-most bit was 1, so a 1 was shifted in on the left)

Pass 1 is complete.

Example: Pass 2

Current Product and previous LSB

11111 01101 1

Pass 2, Step 1: Examine the last 2 bits

11111 01101 1

The last two bits are 11, so we do NOT need to perform

an arithmetic action --

just proceed to step 2.

Example: Pass 2 continued

Pass 2, Step 2: ASR (arithmetic shift right)

Before ASR

11111 01101 1

After ASR

11111 10110 1

(left-most bit was 1, so a 1 was shifted in on the left)

Pass 2 is complete.

Example: Pass 3

Current Product and previous LSB

11111 10110 1

Pass 3, Step 1: Examine the last 2 bits

11111 10110 1

The last two bits are 01, so we need to:

add the multiplicand to the left half of the product

Example: Pass 3 continued

Pass 3, Step 1: Arithmetic action

(1) 11111 (left half of product)

+00010 (mulitplicand)

00001 (drop the leftmost carry)

Place result into left half of product

00001 10110 1

Example: Pass 3 continued

Pass 3, Step 2: ASR (arithmetic shift right)

Before ASR

00001 10110 1

After ASR

00000 11011 0

(left-most bit was 0, so a 0 was shifted in on the left)

Pass 3 is complete.

Example: Pass 4

Current Product and previous LSB

00000 11011 0

Pass 4, Step 1: Examine the last 2 bits

00000 11011 0

The last two bits are 10, so we need to:

subtract the multiplicand from the left half of the product

Example: Pass 4 continued

Pass 4, Step 1: Arithmetic action

(1) 00000 (left half of product)

-00010 (mulitplicand)

11110 (uses a phantom borrow)

Place result into left half of product

11110 11011 0

Example: Pass 4 continued

Pass 4, Step 2: ASR (arithmetic shift right)

Before ASR

11110 11011 0

After ASR

11111 01101 1

(left-most bit was 1, so a 1 was shifted in on the left)

Pass 4 is complete.

Example: Pass 5

Current Product and previous LSB

11111 01101 1

Pass 5, Step 1: Examine the last 2 bits

11111 01101 1

The last two bits are 11, so we do NOT need to perform

an arithmetic action --

just proceed to step 2.

Example: Pass 5 continued

Pass 5, Step 2: ASR (arithmetic shift right)

Before ASR

11111 01101 1

After ASR

11111 10110 1

(left-most bit was 1, so a 1 was shifted in on the left)

Pass 5 is complete.

Final Product

We have completed 5 passes on the 5-bit operands, so we are done.

Dropping the previous LSB, the resulting final product is:

11111 10110

Division

• More complex than multiplication

• Negative numbers are really bad!

• Based on long division

001111

Division of Unsigned Binary Integers

1011

00001101

10010011

1011

0011101011

1011

100

Quotient

Dividend

Remainder

Partial

Remainders

Divisor

Flowchart for Unsigned Binary Division

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

• Moving?

—How do you show where it is?

Floating Point

• +/- .significand x 2exponent

• Point is actually fixed between sign bit and body of mantissa

• Exponent indicates place value (point position)

Floating Point Concept• Floating point is a standard way for representing “real” numbers ... From the analog

world

– Real numbers have an integer and fractional part

• Floating point representation is a standard (canonical) form of “scientific notation”

N x 10E ... N is a decimal fraction = “mantissa”

E is the exponent

10 is the base

– We take advantage of the fact that the position of the decimal point in N can be shifted (“floated”) if we make corresponding adjustments to the exponent, E, in scientific notation.

• Standard floating point representation in a computer is of the following form: 1.zzzzz... x 2yyyy . This is a binary fraction - the base is 2 not 10The exponent yyyy is in binary, but in documentation is represented as decimal for clarity.

yyyy is adjusted to a value which will result in a one digit integral part of the mantissa. The fractional part of the mantissa, zzzzz…, is called the “significand” in the text.

Floating Point Concept Example

The floating point number is now given as:

(-1)S x (1+significand) x 2E

Where the bits of the significand represent a fraction between 0 and 1,

and E specifies the value in the exponent field.

If we number the bits of the significand from left to right as: s1, s2, s3, …

Then the floating point “value” is:

(-1)S x [1+ (s1 x 2-1) + (s2 x 2-2) + (s3 x 2-3)+(s4 x 2-4)+ … ] x 2E

Example:

Let S=0, E=3, significand = 01000101

Fractional part = 0x2-1 + 1x2-2 + 0x2-3 + 0x2-4 + 0x2-5 + 1x2-6 + 0x2-7+1x2-8

= 1/4 + 1/64 + 1/256+ … = 0.26953125… ==> 0.27

Value in decimal is (-1)0 x 1. 26953125… x 23 = 8x1.27 = 10.16

NOTE: This does not take the “bias” additive constant for exponent into account - see

later for this “feature”

Floating Point Representation• (-1)S x 1.Z x 2E (omitting exponent bias – see later)

– S is the sign of the entire number

– ... Sign magnitude representation used

– E is the exponent, 8 bits - signed 2's complement

– Z is the significand , 23 bitsOnly the fractional part of the mantissa is represented

because the integer part in binary is always 1

– Exponent can range from -126 -1, and 0 127 Giving an overall range of about 2.0 x 10-38 thru 2.0 x 103

– Note that some bit combinations of the exponent are not allowed, namely those for –127 = 10000001, and –128 = 10000000this would allow the “biased” exponent to have the positive range: 1 though 254 as desired (see later)

– This representation is used for the float type in C language

51

Floating Point

• Still use a fixed number of bits

—Sign bit S, exponent E, significand F

—Value: (-1)S x F x 2E

• IEEE 754 standard

Size Exponent Significand Range

Single precision 32b 8b 23b 2x10+/-38

Double precision 64b 11b 52b 2x10+/-308

S E F

52

Floating Point Exponent

• Exponent specified in biased or excessnotation

• Why?—To simplify sorting

—Sign bit is MSB to ease sorting

—2’s complement exponent:– Large numbers have positive exponent

– Small numbers have negative exponent

—Sorting does not follow naturally

53

Excess or Biased Exponent

Exponent 2’s Compl Excess-127

-127 1000 0001 0000 0000

-126 1000 0010 0000 0001

… … …

+127 0111 1111 1111 1110

• Value: (-1)S x F x 2(E-bias)

—SP: bias is 127

—DP: bias is 1023

Floating Point Examples

Signs for Floating Point

• Mantissa is stored in 2s compliment

• Exponent is in excess or biased notation

—e.g. Excess (bias) 128 means

—8 bit exponent field

—Pure value range 0-255

—Subtract 128 to get correct value

—Range -128 to +127

Normalization

• FP numbers are usually normalized

• i.e. exponent is adjusted so that leading bit (MSB) of mantissa is 1

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

• (c.f. Scientific notation where numbers are normalized to give a single digit before the decimal point

• e.g. 3.123 x 103)

FP Ranges

• For a 32 bit number

—8 bit exponent

—+/- 2256 1.5 x 1077

• Accuracy

—The effect of changing lsb of mantissa

—23 bit mantissa 2-23 1.2 x 10-7

—About 6 decimal places

Expressible Numbers

Density of Floating Point Numbers

IEEE 754

• Standard for floating point storage

• 32 and 64 bit standards

• 8 and 11 bit exponent respectively

• Extended formats (both mantissa and exponent) for intermediate results

IEEE 754 Formats

FP Arithmetic +/-

• Check for zeros

• Align significands (adjusting exponents)

• Add or subtract significands

• Normalize result

FP Addition & Subtraction Flowchart

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

Floating Point Multiplication

Floating Point Division

Required Reading

• Stallings Chapter 9

• IEEE 754 on IEEE Web site