16
Computer Organization Integers and Arithmetic Operations Terrence Mak

Computer Organization

Embed Size (px)

DESCRIPTION

Computer Organization. Integers and Arithmetic Operations Terrence Mak. Counting to 9, oh no, 1 please. Binary numbers (0, 1) are commonly used in computers as they are easily represented as on/off electrical signals Other related radix systems are Octal & Hexadecimal - PowerPoint PPT Presentation

Citation preview

Computer Organization

Integers and Arithmetic Operations

Terrence Mak

Counting to 9, oh no, 1 please• Binary numbers (0, 1) are commonly used in

computers as they are easily represented as on/off electrical signals

– Other related radix systems are Octal & Hexadecimal

• Different number (representation) systems are used in computers

• Let's assume n bits (binary digits) are used in the following discussions

Only 0 and 1 in a Sequence• Numbers are represented as binary vectors

B = bn-1 bn-2 … b1 b0

• MSB = Most Significant Bit = bn-1

i.e. leftmost digit in a binary vector

• LSB = Least Significant Bit = b0

i.e. rightmost digit in a binary vector

Representing Non-negative Numbers

• Unsigned (non-negative) numbers are in range 0 to 2n – 1

• Represented by

Value(B) = bn-12n-1 + … + b121 + b020

if B is a binary vector representing an unsigned integer

Representing Signed Numbers• In written decimal system, a signed number is

"usually" represented by a "dash" or "plus" sign and followed by the magnitude

e.g. –73, –215, +349

• In binary system, we have several choices:– Sign-and-magnitude

– 1’s complement

– 2’s complement

Representing Signed Numbers• Sign-and-magnitude

– MSB determines sign, remaining unsigned bits represent magnitude

– MSB: 0 means "+", 1 means "–"

• 1’s complement– MSB determines sign– To change sign from unsigned to negative, invert all the bits

• 2’s complement (commonly used)– MSB determines sign– To change sign from unsigned to negative, invert all the bits

and then add 1– This is equivalent to subtracting the positive number from 2n

Signed Number Systems

0000000011111111

00000000

1111

1111

1100110000110011

1010101001010101

1+

1-

2+3+4+5+6+7+

2-3-4-5-6-7-

8-0+0-

1+2+3+4+5+6+7+

0+7-6-5-4-3-2-1-0-

1+2+3+4+5+6+7+

0+

7-6-5-4-3-2-1-

b3 b2b1b0Sign and

magnitude 1' s complement 2' s complement

B (n = 4) V alues represented

2’s Complement

N 2-

N 1-0

1

2

(a) Circle representation of integers mod N

00000001

0010

0011

0100

0101

0110

01111000

1001

1010

1011

1100

1101

1110

1111

1+1-2+

3+

4+

5+

6+7+

2-

3-

4-

5-

6-7- 8-

0

(b) Mod 16 system for 2's-complement numbers

• 2's complement numbers actually make sense since they follow normal modulo arithmetic except when they overflow

• Range is –2(n–1) to +2(n–1) – 1e.g. –8 to +7 for n = 4

for n = 4, N = 2n = 24 = 16

Overflowcut-off

1-bit Addition

Carry-out

1

1

+

011

0

1+

0

0

0

+

1

0

1

+

n-bit Addition/ Subtraction• X + Y

– Use 1-bit addition propagating carry to the most significant bit

• X – Y X + (– Y)– Add X to the 2’s complement of Y

4-bit Addition/ Subtractionof numbers represented in 2’s Complement

1 0 1 11 1 1 0

1 0 0 1

1 1 0 11 0 0 1

0 0 1 00 1 0 0

1 1 1 0

0 1 0 01 0 1 0

0 1 1 11 1 0 1

0 1 0 0

1 1 0 10 1 1 1

0 1 0 0

0 0 1 01 1 0 0

1 1 1 0

0 1 0 1

0 0 1 00 0 1 1

5-

2+( )3+( )

5+( )

2+( )4+( )

2-

7-

3- 7-

6- 2-

4+( )

3-

4+( )

7+( )

4+( )

2-

+

+

+

+

+

+

-

-

(a)

(c)

(b)

(d)

(e)

(f)

4-bit Addition/ Subtractionof numbers represented in 2’s Complement

0 1 1 00 0 1 1

1 0 0 11 0 1 1

1 0 0 10 0 0 1

0 0 1 01 1 0 1

0 1 1 01 1 0 1

0 0 1 1

1 0 0 10 1 0 1

1 1 1 0

1 0 0 11 1 1 1

1 0 0 0

0 0 1 00 0 1 1

0 1 0 1

6+( )3+( )

1+( )

7- 5-

7-

2+( )3-

3+( )

2-

8-

5+( )

+

+

+

+-

-

-

-

(g)

(h)

(i)

(j)

Sign Extension

• Given a 4-bit 2’s complement number• Make it into an 8-bit 2's complement number

– Positive number• add 0’s to LHS• e.g. 0111 00000111

– Negative number• add 1’s to LHS• e.g. 1010 11111010

– c.f. circle representation, i.e. extend the MSB

Overflow

• In 2’s complement arithmetic– Addition of opposite sign numbers never overflow

– If the numbers are the same sign and the result is the opposite sign, overflow has occurred

• e.g. 0111 + 0100 = 1011 = –5!

• positive + positive = negative?!

• In unsigned arithmetic– Carry out signals an overflow

Exercise• Using 4-bit 2’s complement number system,

– What is the binary for -2?– Calculate 2+3– Calculate -2-3– Calculate 5+5

• Using 5-bit number system– What is the largest 2’s complement number?– What is the smallest 2’s complement number?– What is the largest unsigned number?

• Convert 56 to unsigned binary• What is the decimal value of 10110101 in 2’s

complement?• What is the decimal value of unsigned number

10110101?

Characters - 8-bit ASCII representation