william stallings computer organization and architecture 6 th edition

54
William Stallings Computer Organization and Architecture 6 th Edition Chapter 9 Computer Arithmetic

Upload: dexter-koch

Post on 30-Dec-2015

31 views

Category:

Documents


3 download

DESCRIPTION

William Stallings Computer Organization and Architecture 6 th Edition. Chapter 9 Computer Arithmetic. Arithmetic & Logic Unit. Performs arithmetic and logical operations on data. Everything else in the computer is there to service this unit Handles integers - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: William Stallings  Computer Organization  and Architecture 6 th  Edition

William Stallings Computer Organization and Architecture6th Edition

Chapter 9Computer Arithmetic

Page 2: William Stallings  Computer Organization  and Architecture 6 th  Edition

Arithmetic & Logic Unit

• Performs arithmetic and logical operations on data.

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

Page 3: William Stallings  Computer Organization  and Architecture 6 th  Edition

ALU Inputs and Outputs

• Data are presented to the ALU in registers, and the results of an operation are stored in registers.

• The ALU may also set flags as the result of an operation.

• The control unit provides signals that control the operation of the ALU and the movement of the data into and out of the ALU.

Page 4: William Stallings  Computer Organization  and Architecture 6 th  Edition

Integer Representation

• Only have 0 & 1 to represent everything• Positive numbers stored in binary

—e.g. 41=00101001

• No minus sign• No period• Negative number representation

—Sign-Magnitude—Two’s complement

Page 5: William Stallings  Computer Organization  and Architecture 6 th  Edition

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)

Page 6: William Stallings  Computer Organization  and Architecture 6 th  Edition

Two’s Complement• Two’s complement

—Take the Boolean complement of each bit of the integer. i.e., set each 1 to 0 and each 0 to 1.

—Treating the result as an unsigned binary integer, add 1.—Example:

– 3 = 00000011– Boolean complement gives 11111100– Add 1 to LSB 11111101

—+3 = 00000011—+2 = 00000010—+1 = 00000001—+0 = 00000000— -1 = 11111111—-2 = 11111110—-3 = 11111101

Page 7: William Stallings  Computer Organization  and Architecture 6 th  Edition

Benefits

• One representation of zero• Arithmetic works easily (see later)• Negating is fairly easy

Page 8: William Stallings  Computer Organization  and Architecture 6 th  Edition

Geometric Depiction of Twos Complement Integers

若減去 k ,則往逆時針方向移動 k 個位置

若加上 k ,則往順時針方向移動 k 個位置

Page 9: William Stallings  Computer Organization  and Architecture 6 th  Edition

Negation Special Case 1

• 0 = 00000000• Bitwise not 11111111• Add 1 to LSB +1• Result 1 00000000• Overflow is ignored, so:• - 0 = 0 • Overflow

—When the result is larger than can be held in the word size being used.

Page 10: William Stallings  Computer Organization  and Architecture 6 th  Edition

Negation Special Case 2

• -128 = -10000000• bitwise not 01111111• Add 1 to LSB +1• Result 10000000• So:• -(-128) = -128 X

Page 11: William Stallings  Computer Organization  and Architecture 6 th  Edition

Range of Numbers

• 8 bit 2s complement—+127 = 01111111 = 27 -1— -128 = 10000000 = -27

• 16 bit 2s complement—+32767 = 011111111 11111111 = 215 - 1— -32768 = 100000000 00000000 = -215

• N bits 2s complement—+(2n-1-1)—-(2n-1)

Page 12: William Stallings  Computer Organization  and Architecture 6 th  Edition

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)—Sign extension

Page 13: William Stallings  Computer Organization  and Architecture 6 th  Edition

Addition and Subtraction

• Normal binary addition• Monitor sign bit for overflow

• Take twos compliment of subtrahend and add to minuend—i.e. a - b = a + (-b)

• So we only need addition and complement circuits

Page 14: William Stallings  Computer Organization  and Architecture 6 th  Edition

Addition of Numbers in Twos Complement Representation

Page 15: William Stallings  Computer Organization  and Architecture 6 th  Edition

Subtraction of Numbers in Twos Complement Representation (M-S)

Page 16: William Stallings  Computer Organization  and Architecture 6 th  Edition

Subtraction of Numbers in Twos Complement Representation (M-S)

Page 17: William Stallings  Computer Organization  and Architecture 6 th  Edition

Overflow

• Overflow—The result of addition is larger than can be held in the

word size.

• 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

• Effects of Overflow—An exception (interrupt) occurs

– Control jumps to predefined address for exception

Page 18: William Stallings  Computer Organization  and Architecture 6 th  Edition

Hardware for Addition and Subtraction

Page 19: William Stallings  Computer Organization  and Architecture 6 th  Edition

Multiplication

• Complex• Work out partial product for each digit• Take care with place value (column)• Add partial products

Page 20: William Stallings  Computer Organization  and Architecture 6 th  Edition

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

Page 21: William Stallings  Computer Organization  and Architecture 6 th  Edition

Unsigned Binary Multiplication

Page 22: William Stallings  Computer Organization  and Architecture 6 th  Edition

Execution of Example

Page 23: William Stallings  Computer Organization  and Architecture 6 th  Edition

Flowchart for Unsigned Binary Multiplication

Page 24: William Stallings  Computer Organization  and Architecture 6 th  Edition

Multiplying Negative Numbers

• Solution 1—Convert to positive if required—Multiply as above—If signs were different, negate answer

• Solution 2—Booth’s algorithm

Page 25: William Stallings  Computer Organization  and Architecture 6 th  Edition

Comparison of Multiplication of Unsigned and Twos Complement Integer

部分積以 2n bit 表示 可解決被乘數為負值的問題,但若乘數為負值時,仍無解

Page 26: William Stallings  Computer Organization  and Architecture 6 th  Edition

Booth’s Algorithm

Page 27: William Stallings  Computer Organization  and Architecture 6 th  Edition

Example of Booth’s Algorithm

Page 28: William Stallings  Computer Organization  and Architecture 6 th  Edition

Examples Using Booth’s Algorithm

Page 29: William Stallings  Computer Organization  and Architecture 6 th  Edition

Examples Using Booth’s Algorithm (cont.)

Page 30: William Stallings  Computer Organization  and Architecture 6 th  Edition

Division

• More complex than multiplication• Negative numbers are really bad!• Based on long division

Page 31: William Stallings  Computer Organization  and Architecture 6 th  Edition

Division of Unsigned Binary Integers

Quotient

Dividend

Remainder

PartialRemainders

Divisor

Page 32: William Stallings  Computer Organization  and Architecture 6 th  Edition

Flowchart for Unsigned Binary Division

Page 33: William Stallings  Computer Organization  and Architecture 6 th  Edition

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-point notation

– Very limited+ Very large numbers cannot be represented, nor can very small

fractions.

—Floating-point notation– Scientific notation

– Sign: plus or minus– Significand S– Exponent E– Base B

EBS

Page 34: William Stallings  Computer Organization  and Architecture 6 th  Edition

Floating Point

• +/- .significand x 2exponent

• Point is actually fixed between sign bit and body of mantissa—There is one bit to the left of the radix point.

• Exponent value is biased representation— A fixed value is subtracted from the field to get the true exponent value— The bias equals (2k-1-1)

Sig

n bi

t

BiasedExponent

Significand or Mantissa

Page 35: William Stallings  Computer Organization  and Architecture 6 th  Edition

Floating Point Examples

Page 36: William Stallings  Computer Organization  and Architecture 6 th  Edition

Signs for Floating Point

• Exponent is in excess or biased notation—e.g. Excess (bias) 127 means—8 bit exponent field—Pure value range 0-255—Subtract 127 to get correct value—Range -127 to +128

• Significand also called the mantissa

Page 37: William Stallings  Computer Organization  and Architecture 6 th  Edition

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—e.g. 3.123 x 103

—The normalized nonzero number is one in the form

—See Fig. 9.18b– The sign is stored in the first bit of the word– The first bit of the true significand is always 1 and need

not be stored in the significand field.– The value 127 is added to the true exponent to be

stored in the exponent field.– The base is 2.

Ebbbb 2....1

Page 38: William Stallings  Computer Organization  and Architecture 6 th  Edition

FP Ranges• For a 32 bit number

—Using 2s complement integer representation

—Using 8 bit exponent, 23 bit significand – Negative number –(2-2-23)*2128~-2-127

– Positive number: 2-127~(2-2-23)*2128

—Five regions on the number line are not included in these ranges:

– Negative overflow– Negative underflow– Zero– Positive underflow– Positive overflow

• Accuracy—The effect of changing lsb of mantissa—23 bit mantissa 2-23 1.2 x 10-7

—About 6 decimal places

12~2 3131

Page 39: William Stallings  Computer Organization  and Architecture 6 th  Edition

Expressible Numbers

Page 40: William Stallings  Computer Organization  and Architecture 6 th  Edition

IEEE 754

• Standard for floating point storage—32 bit single precision —64 bit double precision

• 8 and 11 bit exponent respectively• 23 and 52 bit significand respectively• The exponent is biased,

—the rang of exponents is -126~+127 , -1022~+1023

—exponent 與 significand 均為零時, 由 sign 決定為 positive or negative zero.

—Exponent 均為 1 , significand 為零時 , 由 sign 決定為正負無窮大

Page 41: William Stallings  Computer Organization  and Architecture 6 th  Edition

IEEE 754 Formats

Page 42: William Stallings  Computer Organization  and Architecture 6 th  Edition

IEEE 754 format parameter

Page 43: William Stallings  Computer Organization  and Architecture 6 th  Edition

Interpretation of IEEE 754 FP numbers

Page 44: William Stallings  Computer Organization  and Architecture 6 th  Edition

Example 1• (a) Convert the decimal number -1.5 to a 32-

bit floating-point number with IEEE 754 format.(b) Convert a 32-bits IEEE 754 format floating-point number 43A0C000 to the decimal number.

• Solution:• (a) 1.5=1.12=1.1*20

S=1, C=E+127=127=01111111,F=1000 0000 0000 0000 0000 000(b)43A0C000=0100 0011 1010 0000 1100 0000 0000 00002S=0, C=10000111=135=E+127, =>E=8,F= 0100 0001 1000 0000 00001.01000001102*28=101000001.12= 321.5

Page 45: William Stallings  Computer Organization  and Architecture 6 th  Edition

Example 2

• Convert the decimal number -1.5 to a 32-bit floating-point number with IBM S/390 format.

• Solution:—Base of 16, 7 bit exponent, 24 bit significand

—-1.5=-1.8(16)=-0.18*161

—S=1, C=E+64=1+64=65=100 0001F=0001 1000 0000 0000 0000 0000

—The IBM S/390 format is1 100 0001 0001 1000 0000 0000 0000 0000

Page 46: William Stallings  Computer Organization  and Architecture 6 th  Edition

Floating-point Arithmetic

• A FP operation may produce one of these conditions:—Exponent overflow—Exponent underflow—Significand underflow—Significand overfolw

Page 47: William Stallings  Computer Organization  and Architecture 6 th  Edition

FP numbers and arithmetic operations

Page 48: William Stallings  Computer Organization  and Architecture 6 th  Edition

FP Arithmetic +/-

• Check for zeros• Align significands (adjusting exponents)• Add or subtract significands• Normalize result

Page 49: William Stallings  Computer Organization  and Architecture 6 th  Edition

FP Addition & Subtraction Flowchart

Page 50: William Stallings  Computer Organization  and Architecture 6 th  Edition

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 51: William Stallings  Computer Organization  and Architecture 6 th  Edition

Floating Point Multiplication

Page 52: William Stallings  Computer Organization  and Architecture 6 th  Edition

Floating Point Division

Page 53: William Stallings  Computer Organization  and Architecture 6 th  Edition

Precision Considerations

• Guard bit—Used to pad out the right end of the significand

with 0s.

• Rounding—When the result is put back into the floating-

point format, the extra bits must be disposed of.—IEEE standard lists four alternative approaches:

– Round to nearest+使用最接近結果的表示值 ( 四捨五入 )

– Round toward +∞– Round toward –∞– Round toward 0

+單純的將多餘的位元截斷。

Page 54: William Stallings  Computer Organization  and Architecture 6 th  Edition

IEEE standard for binary FP arithmetic

• Infinity—The limiting case of real arithmetic.

• Quiet and Signaling NaNs—Signaling NaN afford values for uninitialized

variables and arithmetic-like enhancement.—Quiet NaN propagates through almost every

arithmetic operation without signaling an exception.

• Denormalized Numbers—To handle cases of exponent underflow.