• No results found

w12_arithematic.pdf

N/A
N/A
Protected

Academic year: 2020

Share "w12_arithematic.pdf"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

W

ILLIAM

S

TALLINGS

C

OMPUTER

O

RGANIZATION

AND

A

RCHITECTURE

8

TH

E

DITION Chapter 9

Computer Arithmetic

A

RITHMETIC

& L

OGIC

U

NIT

 Does arithmetic and logical operations

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

 Based on digital logic services that can store binary digits

 Handles integers

 May handle floating point (real) numbers

 May be separate FPU (maths co-processor)

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(2)

ALU I

NPUTS AND

O

UTPUTS

3

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

I

NTEGER

R

EPRESENTATION

 Only have 0 & 1 to represent everything

 Numbers stored in binary

 e.g. 41=00101001  No minus sign

 No period

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(3)

S

IGN

-M

AGNITUDE

R

EPRESENTATION

 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

+010 = 00000000

-010 = 10000000 5

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

T

WO

S

C

OMPLIMENT

R

EPRESENTATION

 +3 = 00000011

 +2 = 00000010

 +1 = 00000001

 +0 = 00000000

 -1 = 11111111

 -2 = 11111110

 -3 = 11111101

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(4)

B

ENEFITS

 One representation of zero

 Arithmetic works easily (see later)

 Negating is fairly easy

 3 = 00000011

 Boolean complement gives 11111100  Add 1 to LSB 11111101

7

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

C

ONVERTING

B

ETWEEN

D

IFFERENT

B

IT

L

ENGTH+18 = 00010010 (sign magnitude, 8 bits)

+18 = 0000000000010010 (sign magnitude, 16 bits) -18 = 10010010 (sign magnitude, 8 bits) -18 = 1000000000010010 (sign magnitude, 16 bits)

+18 = 00010010 (Two’s Complement, 8 bits)

+18 = 0000000000010010 (Two’s Complement, 16 bits)

-18 = 11101110 (Two’s Complement, 8 bits )

-32.658 = 1000000000010010 (Two’s Complement, 16 bits)

-18 = 11101110 (Two’s Complement, 8 bits)

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(5)

G

EOMETRIC

D

EPICTION OF

T

WOS

C

OMPLEMENT

I

NTEGERS

9

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

N

EGATION

 +18 = 00010010 (Two’s Complement)

 Bitwise not 11101101

 Add 1 to LSB +1

 Result 11101110 = -18

 -18 = 11101110 (Two’s Complement)

 Bitwise not 00010001

 Add 1 to LSB +1

Result 00010010 = +18

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(6)

N

EGATION

S

PECIAL

C

ASE

1

 0 = 00000000

 Bitwise not 11111111

 Add 1 to LSB +1

 Result 1 00000000

 Overflow is ignored, so:

 - 0 = 0

11

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

N

EGATION

S

PECIAL

C

ASE

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

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(7)

R

ANGE OF

N

UMBERS

 8 bit 2s compliment

 +127 = 01111111 = 27 -1  -128 = 10000000 = -27  16 bit 2s compliment

 +32767 = 011111111 11111111 = 215 - 1  -32768 = 100000000 00000000 = -215

13

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

A

DDITIONAND

S

UBTRACTION

 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

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(8)

A

DDITION

15

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

S

UBTRACTION

0000 = 2 +1001 = -7 1011 = -5

(a) M = 2 = 0010 S = 7 = 0111 -S = 1001

0101 = 5 + 1110 = -2 0011 = 3

(b) M = 5 = 0101 S = 2 = 0010 - S = = 1110

1011 = -5 + 1110 = -2

1001 = -7

(b) M = -5 = 1011 S = 2 = 0010 - S = = 1110

0101 = 5 + 0010 = 2 0111 = 3

(b) M = 5 = 0101 S = -2 = 1110 - S = = 0010

0111 = 7 + 1001 = 7 0000 = OF

(b) M = 7 = 0111 S = -7 = 1001 - S = = 0111

1010 = -6 + 1100 = -4

0110 = OF

(b) M = -6 = 1010 S = 4 = 0100 - S = = 1100

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(9)

H

ARDWARE FOR

A

DDITIONAND

S

UBTRACTION

17

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

M

ULTIPLICATION

 Complex

 Work out partial product for each digit

 Take care with place value (column)

 Add partial products

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(10)

M

ULTIPLICATION

E

XAMPLE

 1011 Multiplicand (11)

 x 1101 Multiplier (13)

 1011 Partial products

 0000 Note: if multiplier bit is 1 copy

 1011 multiplicand (place value)

 1011 otherwise zero

 10001111 (143)

 Note: need double length result

19

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

U

NSIGNED

B

INARY

M

ULTIPLICATION

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(11)

E

XECUTION OF

E

XAMPLE

21

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

F

LOWCHART FOR

U

NSIGNED

B

INARY

M

ULTIPLICATION

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(12)

T

WOS

’ C

OMPLEMENT

R

EPRESENTATION

If numbers are unsigned

1 0 0 1 ( 9 ) + 0 0 1 1 ( 3 ) 1 1 0 0 ( 12 )

If numbers are two’s complement 1 0 0 1 ( -7 ) + 0 0 1 1 ( 3 )

1 1 0 0 ( -4 ) 23

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

N

OT WORK FOR MULTIPLICATION

If numbers are unsigned

1 0 0 1 ( 9 ) x 1 1 0 1 ( 13 ) 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 1 0 1 (117)

If numbers are represented in two’s complement 1 0 0 1 ( -7 ) x 1 1 0 1 ( -3 ) 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(13)

M

ULTIPLYING

N

EGATIVE

N

UMBERS

 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

25

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

B

OOTH

S

A

LGORITHM

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(14)

E

XAMPLE OF

B

OOTH

S

A

LGORITHM

27

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

D

IVISION

 More complex than multiplication

 Negative numbers are really bad!

 Based on long division

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(15)

001111

D

IVISION OF

U

NSIGNED

B

INARY

I

NTEGERS

1011

00001101 10010011

1011 001110

1011

1011 100

Quotient

Dividend

Remainder Partial

Remainders Divisor

29

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

F

LOWCHART FOR

U

NSIGNED

B

INARY

D

IVISION

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(16)

B

INARY

D

IVISION

1 - Load the divisor into the M register and the dividend into the A, Q registers. The dividend must be expressed as a 2n-bit two’s complement number. This, for example , the 4-bit 0111 becomes 00000111, and 1001 becomes 11111001.

2 - shift A, Q left 1 bit position.

3 - if M and A have the same signs, perform A = A - M otherwise A = A + M

4 – the preceding operation is successful if the sign of A is the same before and after the operation.

a. if the operation is successful or A = 0 then set Q0 = 1 b. if the operation is unsuccessful and A is not equal to 0, then set Q0 = 0 and restore the previous value of A

31

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

B

INARY

D

IVISION

5 – Repeat steps 2 through 4 as many times as there are bit positions in Q.

6 – the remainder is in A. if the signs of the divisor and dividend were the same, then the quotient is in Q, otherwise, the correct quotient is the two’s complement of Q.

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(17)

R

EAL

N

UMBERS

 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?

33

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

F

LOATING

P

OINT

 +/- .significand x 2exponent

 Misnomer

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

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(18)

F

LOATING

P

OINT

E

XAMPLES

35

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

S

IGNSFOR

F

LOATING

P

OINT

 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

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(19)

N

ORMALIZATION

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

37

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

FP R

ANGES

 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

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(20)

E

XPRESSIBLE

N

UMBERS

39

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

D

ENSITY OF

F

LOATING

P

OINT

N

UMBERS

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(21)

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

41

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

IEEE 754 F

ORMATS

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(22)

FP A

RITHMETIC

+/-

 Check for zeros

 Align significands (adjusting exponents)

 Add or subtract significands

 Normalize result

43

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

FP A

DDITION

& S

UBTRACTION

F

LOWCHART

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(23)

FP A

RITHMETIC

X

/

 Check for zero

 Add/subtract exponents

 Multiply/divide significands (watch sign)

 Normalize

 Round

 All intermediate results should be in double length storage

45

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

F

LOATING

P

OINT

M

ULTIPLICATION

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

(24)

F

LOATING

P

OINT

D

IVISION

47

P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

vi

R

EQUIRED

R

EADING

 Stallings Chapter 9

 IEEE 754 on IEEE Web site P

re

pa

re

d

b

y

E

n

gi

n

ee

r

S

M

A

sim

A

li

R

iz

References

Related documents

planned delivery and installation of any materials, to allow Owner ample time for scrutiny. No claims for extension of time shall be entertained because of any delay in the work

This is known as fixed-point representation where the position of decimal point is fixed and number of bits before and after decimal point are also predefined..

Goal: Perform operations with numbers written in scientific notation.. Operations with

A skilful teacher should use techniques that will disclose behavioural traits that are considered important to the development of broad and wholesome

A number is in scientific notation when it’s written as a decimal with one digit to the left of the decimal point times some power of 10.... EXAMPLE 3 Writing Numbers in

The most discriminative background patches change for the three tasks, which is due to different training images and to the discriminative training: For example, the first

The Offeror providing the lowest 3 Year Cost to the Consumer for all Call Types and Fees on Page 3 4 of Attachment M will receive a total score of 55.. All other Offerors will

Of particular interest was the potential of the method to complement data-derived knowledge, by eliciting information on the following themes: (a) the frequency and spatial location