CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z...

38
CMPUT 229 - Computer Orga nization and Architecture I 1 2. Number Systems Decimal Binary Addition Headecimal Two’s complement ASCII characters
  • date post

    18-Dec-2015
  • Category

    Documents

  • view

    226
  • download

    2

Transcript of CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z...

Page 1: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

1

2. Number Systems

Decimal Binary Addition HeadecimalTwo’s complement ASCII characters

Page 2: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

2

Positional Number System

329 923

329

923

9 2 3

9 23

329 923

Page 3: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

3

Decimal numbers

329 923

3102 + 2101 + 9100 9102 + 2101 + 3100

3100 + 210 + 91 9100 + 210 + 31

300 + 20 + 9 900 + 20 + 3

923329

Page 4: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

4

Positional Number System

32913 92313

3132 + 2131 + 9130 9132 + 2131 + 3130

3169 + 213 + 91 9169 + 213 + 3150710 + 2610 + 910 152110 + 2610 + 310

The same positional system works with different basis:

15501054210

Page 5: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

5

Binary System

1102 92316

122 + 121 + 020 9162 + 2161 + 3160

14 + 12 + 01 9256 + 216 + 31410 + 210 + 010 230410 + 3210 + 310

In computers we are mostly interested on bases 2, 8, and 16.

233910610

Page 6: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

6

Signed Integers

Problem: given 2k distinct patterns of bits, each pattern with k bits, assign integers to the patterns in such a way that:The numbers are spread in an interval around

zero without gaps.Roughly half of the patterns represent positive

numbers, and half represent negative numbers.When using standard binary addition, given an

integer n, the following property should hold: pattern(n+1) = pattern(n) + pattern(1)

Page 7: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

Signed MagnitudeValue Represented Pattern

Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 1001 1010 1011 1100 1101 1110 1111

In a signed magnitude representation weuse the first bit of the pattern to indicateif it is a positive or a negative number.

Page 8: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

Signed MagnitudeValue Represented Pattern

Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7

What do we do with the pattern 1000?

Page 9: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

Signed MagnitudeValue Represented Pattern

Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7

Having two patterns to represent0 is wasteful.

The signed magnitude representationhas the advantage that it is easy toread the value from the pattern.

But does it have the binary arithmetic property?

For instance, what is the resultof pattern(-1) + pattern(1)?

1001 pattern(-1)+ 0001 pattern(1) ??

PattPatel pp. 20

Page 10: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

Signed MagnitudeHaving two patterns to represent0 is wasteful.

The signed magnitude representationhas the advantage that it is easy toread the value from the pattern.

But does it have the arithmetic property?

For instance, what is the resultof pattern(-1) + pattern(1)?

1001 pattern(-1)+ 0001 pattern(1) 1010 = ??

Value Represented Pattern Sign Magnitude

0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7

PattPatel pp. 20

Page 11: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

Signed MagnitudeValue Represented Pattern

Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7

Having two patterns to represent0 is wasteful.

The signed magnitude representationhas the advantage that it is easy toread the value from the pattern.

But does it have the arithmetic property?

For instance, what is the resultof pattern(-1) + pattern(1)?

1001 pattern(-1)+ 0001 pattern(1) 1010 = pattern(-2)

PattPatel pp. 20

Page 12: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

1’s ComplementValue Represented Pattern

Sign Magnitude 1’s complement 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 -0 -7 1001 -1 -6 1010 -2 -5 1011 -3 -4 1100 -4 -3 1101 -5 -2 1110 -6 -1 1111 -7 -0

A negative number is representedby “flipping” all the bits of a positivenumber.

We still have two patterns for 0.

It is still easy to read a value froma given pattern.

How about the arithmetic property?

Suggestion: try the folllowing-1 + 1 = ??-0 + 1 = ?? 0 + 1 = ??

PattPatel pp. 20

Page 13: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

13

2’s Complement

a representation for negative numbers The leftmost bit is used to indicate +/-

Positive number starts with 0, negative 1

A negative number is obtained by Convert the corresponding positive

decimal number to a binary toggle all bits ( all 10 and all 0 1) Add 1 to the the binary obtained in the

previous step

Page 14: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

2’s ComplementValue Represented Pattern

Sign Magnitude 1’s complement 2’s complement 0000 0 0 0 0001 1 1 1 0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 -0 -7 -8 1001 -1 -6 -7 1010 -2 -5 -6 1011 -3 -4 -5 1100 -4 -3 -4 1101 -5 -2 -3 1110 -6 -1 -2 1111 -7 -0 -1

A single pattern for 0.

1111 pattern(-1)+ 0001 pattern(1) 0000 = pattern(0)

It holds the arithmetic property.

But the reading of a negative pattern is not trivial.

PattPatel pp. 20

Page 15: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

15

Binary to Decimal Conversion

Problem: Given an 8-bit 2’s complement binary number:

a7 a6 a5 a4 a3 a2 a1 a0 find its corresponding decimal value.

PattPatel pp. 23

Because the binary representation has8 bits, the decimal value must be in the [-27; +(27-1)] =[-128;+127] interval.

Page 16: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

16

if (negative = true)then

Binary to Decimal Conversion

a7 a6 a5 a4 a3 a2 a1 a0

Solution: negative false if (a7 = 1) then negative true flip all bits; compute magnitude using:

00

11

22

33

44

55

66 2222222 aaaaaaaV

VV

VV

1

PattPatel pp. 24

Page 17: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

17

Binary to Decimal Conversion (Examples)

Convert the 2’s complement integer 11000111 to itsdecimal integer value.

1. a7 is 1, thus we make a note that this is a negative number and invert all the bits, obtaining: 00111000

2. We compute the magnitude:

56000816320

10204081161321640

20202021212120 0123456

V

3. Now we remember that it was a negative number, thus:57 ;57156 VV

PattPatel pp. 24

Page 18: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

18

Decimal to Binary Convertion

We will start with an example. What is the binaryrepresentation of 10510?

00

11

22

33

44

55

66 2222222105 aaaaaaa

Our problem is to find the values of each ai

Because 105 is odd, we know that a0 = 1

Thus we can subtract 1 from both sides to obtain:

11

22

33

44

55

66 222222104 aaaaaa

PattPatel pp. 24

Page 19: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

19

Decimal to Binary Convertion (cont.)

11

22

33

44

55

66 222222104 aaaaaa

Now we can divide both sides by 2

01

12

23

34

45

56 22222252 aaaaaa

Because 52 is even, we know that a1 = 0

12

23

34

45

56 2222252 aaaaa

02

13

24

35

46 2222226 aaaaa a2 = 0

13

24

35

46 222226 aaaa

03

14

25

36 222213 aaaa a3 = 1

14

25

36 22212 aaa

PattPatel pp. 24

Page 20: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

20

Decimal to Binary Convertion (cont.)

14

25

36 22212 aaa

04

15

26 2226 aaa a4 = 0

15

26 226 aa

05

16 223 aa a5 = 1

16 22 a

06 21 a a6 = 1

Thus we got:a1 = 0a4 = 0a5 = 1a6 = 1 a2 = 0a3 = 1 a0 = 1

10510 = 011010012

PattPatel pp. 25

Page 21: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

21

Decimal to Binary Conversion (Another Method)

We can also use repeated long division:

105/2 = 52 remainder 1

52/2 = 26 remainder 0

26/2 = 13 remainder 0

13/2 = 6 remainder 1

6/2 = 3 remainder 0

3/2 = 1 remainder 1

1/2 = 0 remainder 1

Page 22: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

22

Decimal to Binary Conversion (Another Method)

We can also use repeated long division:

105/2 = 52 remainder 1

52/2 = 26 remainder 0

26/2 = 13 remainder 0

13/2 = 6 remainder 1

6/2 = 3 remainder 0

3/2 = 1 remainder 1

1/2 = 0 remainder 1

rightmost digit

10510 = 011010012

Page 23: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

23

Decimal to Binary Conversion (Negative Numbers)

What is the binary representation of -10510 in 8 bits?

We know from the previous slide that:+10510 =011010012

To obtain the binary representation of a negativenumber we must flip all the bits of the positive

representation and add 1:

10010110+ 00000001

10010111

Thus:-10510 =100101112

PattPatel pp. 22-23

Page 24: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

24

Hexadecimal Numbers (base 16)

Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F

If the number 0xFACE representsa 2’s complement binary number,

what is its decimal value?

By convention, the characters 0xare printed in front of an hexadecimal

number to indicate base 16.

First we need to look up the binaryrepresentation of F, which is 1111.

Therefore 0xFACE is a negative number, and we have to flip all the bits.

PattPatel pp. 26-27

Page 25: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

25

Hexadecimal Numbers (base 16)

Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F

It is best to write down the binaryrepresentation of the number first:

0xFACE = 1111 1010 1100 1110

Now we flip all the bits and add 1: 0000 0101 0011 0001 + 0000 0000 0000 0001 0000 0101 0011 0010 = 0x0532

Then we convert 0x0532 from base 16 to base 10:

0x0532 = 0163 + 5162 + 3161 + 2160

= 0 + 5256 + 316 + 21

= 1280 + 48 + 2

= 133010

0xFACE = -133010

PattPatel pp. 26-27

Page 26: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

26

Binary Arithmetic

Decimal

19+ 3

22

Binary

010011+ 000011

010110

Decimal

14- 9

5

Binary

001110+ 110111

000101

910 = 0010012

-910 = 1101112

PattPatel pp. 25

Page 27: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

27

Overflow

What happens if we try to add +9 with +11 in a 5-bit 2-complementrepresentation?

Decimal

9+ 11

20

Binary

01001+ 01011

10100 = -12 ?

The result is too large to represent in 5 digits, i.e. it is larger than 01111 = +1510.

When the result is too large for the representationwe say that the result has OVERFLOWed the

capacity of the representation?

PattPatel pp. 27

Page 28: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

28

Overflow Detection

What happens if we try to add +9 with +11 in a 5-bit 2-complementrepresentation?

Decimal

9+ 11

20

Binary

01001+ 01011

10100 = -12 ?

We can easily detect the overflow by detecting that theaddition of two positive numbers resulted in a negative result.

PattPatel pp. 28

Page 29: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

29

Overflow (another example)

Could overflow happen when we add two negative numbers?

Decimal

- 12+ -6-18

Binary

10100+ 11010

01110 = +14 ?

Again we can detect overflow by detecting that weadded two negative numbers and got a positive result.

Could we get overflow when adding a positive anda negative number?

PattPatel pp. 28

Page 30: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

30

Sign-extension

What is the 8-bit representation of +510?

0000 0101

What is the 16-bit representation of +510?

0000 0000 0000 0101

What is the 8-bit representation of -510?

1111 1011

What is the 8-bit representation of -510?

1111 1111 1111 1011

PattPatel pp. 27

Page 31: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

31

Sign-extension

What is the 8-bit representation of +510?

0000 0101

What is the 16-bit representation of +510?

0000 0000 0000 0101

What is the 8-bit representation of -510?

1111 1011

What is the 8-bit representation of -510?

1111 1111 1111 1011

To sign-extend anumber to a largerrepresentation, allwe have to do is toreplicate the sign bituntil we obtain the

new length.

PattPatel pp. 27

Page 32: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

32

Storing characters

ASCII (American Standard Code for Information Interchange) One byte

UTF-8 (8-bit Unicode Transformation Format) One to four bytes Coincides with ASCII Be able to represent any languages

Page 33: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

33

Endianess

0x100010000x100010010x100010020x100010030x100010040x100010050x100010060x10001007

0x000x000x000x130xFF0xFF0xFF0x97

Address Value

In the previous example, there are two ways to store +1910 atthe address 0x10001000 and -10510 at the address 0x10001004:

0x100010000x100010010x100010020x100010030x100010040x100010050x100010060x10001007

0x130x000x000x000x970xFF0xFF0xFF

Address Value

What is the difference?

Page 34: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

34

Little-End and Big-End

+1910 = 0000 0000 0000 0000 0000 0000 0001 0011

+1910 = 0x0000 0013

Little end of +1910 Big end of +1910

(binary)

(hexadecimal)

-10510 = 1111 1111 1111 1111 1111 1111 1001 0111

+1910 = 0xFFFF FF97

Little end of +1910 Big end of +1910

(binary)

(hexadecimal)

Page 35: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

35

Endianess

0x100010000x100010010x100010020x100010030x100010040x100010050x100010060x10001007

0x000x000x000x130xFF0xFF0xFF0x97

Address Value

The question is: which end of the integer do we store first in memory?

0x100010000x100010010x100010020x100010030x100010040x100010050x100010060x10001007

0x130x000x000x000x970xFF0xFF0xFF

Address Value

Big end of +1910 Little end of +1910

Little Endian Byte OrderBig Endian Byte Order

DECstations and Intel 80x86 are little-endians.

Sun SPARC and Macintosh are big-endians.

Page 36: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

36

Floating Number

How to represent a very large number?

How about a number of the form 2.5006 x 10

Floating number IEEE standard

-8

Page 37: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

37

IEEE 754 Floating Number

S exponent fraction (precision)

1 8 23

N = -1 x 1. Fraction x 2 S exponent – 127

Page 38: CMPUT 229 - Computer Organization and Architecture I1 2. Number Systems z Decimal z Binary z Addition z Headecimal zTwo’s complement z ASCII characters.

CMPUT 229 - Computer Organization and Architecture I

38

Example

-65

8