Computer Organization - Data Representation on CPU

Preview:

DESCRIPTION

Nota

Citation preview

Chapter 2.0:

Data Representation on CPU

FP203 : Computer Organization

2.1 Number System (decimal, binary, octal, and

hexadecimal)

2.2 Arithmetic Operation in number system.

2.3 Convert Decimal, Binary, Octal and Hexadecimal

Numbers to different bases.

2.4 Coding system:Sign and magnitude,

1’s Complement and 2’s Complement

Binary Coded Decimal (BCD system)

ASCII and EBCDIC

Topic Cover

Examples

INTRODUCTION

Real World

Data

Computer

DataInput device

Dear Mom: Keyboard 10110010…

Digitalcamera

10110010…

• Many number system are in use in digital technology.

• Most common are:– Decimal, N10

– Binary, N2

– Octal, N8

– Hexadecimal, N16

2.1 Number System

• Arithmetic operation in number system consist of:– Addition– Subtraction– Multiplication– Division

2.2 ARITHMETIC OPERATION

Only cover this 2 topics

• Decimal system is composed of 10 numerals or symbol.

• Symbol: 0,1,2,3,4,5,6,7,8,9

• Example: 23410

Decimal number system

10 Symbol

103 102 101 100 . 10-1

= 1000 = 100 = 10 = 1 . = 0.1

Multiplier:

2746.210 This number is came from this calculation:

2746.210 = (2x1000) + (7x100) + (4x10) + (2x1) + (2x0.1)

= 2000 + 700 + 40 + 2 + 0.2

= 2746.2

Decimal number = Natural Number

Example:

2 7 4 2 . 2103 102 101 100 . 10-1

= 1000 = 100 = 10 = 1 . = 0.1

Arithmetic Operation

Decimal

+ -

Decimal AdditionExample:

a. 89310 + 32110 =

89310

+32110

121410

Try this : 73310 + 79910 = ?

b. 75710 + 24510 = 75710

+ 24510

100210

Decimal Subtraction

Example:

a. 5410 - 1710 =

5410

- 1710

3710

Try this : 533310 - 3710 = ?

b. 15710 - 8910 = 15710

- 8910

6810

• Octal system is composed of 8 numerals or symbol.

• Symbol: 0,1,2,3,4,5,6,7

• Example: 658

Octal number system

8 Symbol

83 82 81 80 . 8-1

= 512 = 64 = 8 = 1 . = 0.125

Multiplier:

107.158 This number can be convert to decimal value using this calculation:

107.158 = (1x64)+(0x8)+(7x1)+(1x0.1250)+(5x0.0156)

= 64 + 0 + 7 + 0.1250 + 0.078

= 71.20310

Example:

1 0 7 . 1 582 81 80 . 8-1 8-2

= 64 = 8 = 1 . = 0.1250 = 0.0156

Arithmetic Operation

Octal

+ -

Octal AdditionSekiranya setiap hasil perjumlahan yang melebihi atau sama dengan 8 mestilah ditolak dengan 8.

Example:

a. 1238 + 3218 =

1238

+3218

4448

Try this : 7338 + 748 = ?

b. 4578 + 2458 = 4578

+ 2458

7248

Octal Subtraction Sekiranya terdapat peminjam, nombor peminjam mestilah dijumlahkan dengan 8.

Example:

a. 5248 - 1678 =

5248

- 1678

3358

Try this : 15238 - 3648 = ?

b. 1678 - 248 = 1678

- 248

1438

• Binary system is composed of 2 numerals or symbol.

• Symbol: 0,1

• Example: 1012

Binary number system

2 Symbol

25 24 23 22 21 20

= 32 = 16 = 8 = 4 = 2 = 1

Multiplier:

10.1012 This number can be convert to decimal value using this calculation:

10.1012 = (1x2)+(0x1)+(1x0.5)+(0x0.25)+(1x0.125)

= 2 + 0 + 0.5 + 0 + 0.125

= 2.62510

Example:

1 0 . 1 0 121 20 . 2-1 2-2 2-3

= 2 = 1 . = 0.5000 = 0.2500 = 0.1250

Arithmetic Operation

Binary

+ -

Binary AdditionThe four basic rules for adding binary digits are as follows:

Example:

110112 + 100012 =

110112

+ 100012

1011002

Try this : 101112 + 1112 = ?

0 + 0 = 00 + 1 = 11 + 0 = 1

1 + 1 = 0 carry 1

Binary SubtractionThe four basic rules for subtracting binary digits are as follows:-

Example:

10012 -102 =

10012

- 102

1112

Try this : 1010112 – 11112 =?

0 - 0 = 0 0 - 1 = 1 borrow 1 1 - 0 = 1 1 - 1 = 0

Have previously looked at the subtraction operation. A quick review.

Just like subtraction in any other base

10110

-10010

00100

• And when a borrow is needed. Note that the borrow gives us 2 in the current bit position.

.

Binary Subtraction

Example

• When there is no borrow into the msb position, then the subtrahend in not larger than the minuend and the result is

positive and correct.

• If a borrow into the msb does occur, then the subtrahend is larger than the minuend.

In General

• Now do the operation 4 – 6

• Correct difference is -2 or -0010• Different because 2n was brought in and made the operation M-

N+2n

Consider

• Hexadecimal system is composed of 16 numerals or symbol.

• Symbol: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

• Example: 7A16

Hexadecimal number system

16 Symbol

163 162 161 160 . 16-1

= 4096 = 256 = 16 = 1 . = 0.0626

Multiplier:

10 11 12 13 14 15

B6F.7C16 This number can be convert to decimal value using this calculation:

B6F.7C16 = (11x256) + (6x16) + (15x1) + (7x0.0625) + (12x0.0039)

= 2816 + 96 + 15 + 0.4375 + 0.0468

= 2927.484310

Example:

B 6 F . 7 C162 161 160 . 16-1 16-2

= 256 = 16 = 1 . 0.0625 = 0.0039

Arithmetic Operation Hexadecimal

+ -

Hexadecimal AdditionSekiranya setiap hasil perjumlahan yang melebihi atau sama dengan 16 mestilah ditolak dengan 16.

Example:

a. 3316 + 4716 =

3316

+ 4716

7A16

Try this : DF16 + AB16 = ?

b. 20D316 + 12BC16 = 20D316

+ 12BC16

338F16

Hexadecimal SubtractionNilai yang kecil daripada 16 boleh dipinjam dari sebelah dengan nilai 16.

Example:

a. 4416 - 1716 =

4416

- 1716

2D16

Try this : DF16 - AB16 = ?

b. 20D316 - 12BC16 = 20D316

- 12BC16

0E1716

2.3: Convert Decimal, Binary, Octal and Hexadecimal Numbers to different bases

1111012 This number can be convert to decimal value using this calculation:

1111012 = (1x32)+(1x16)+(1x8)+(1x4)+(0x2)+(1x1)

= 32 + 16 + 8 + 4 + 0 + 1

= 6110

Try this: Convert 1100.10112 to decimal?

Convert 100.10112 to decimal?

Example:

1 1 1 1 0 125 24 23 22 21 20

= 32 = 16 =8 = 4 = 2 = 1

Convert Binary to Decimal (N2 – N10)

1111012

1111012 = [(1x4)+(1x2)+(1x1)] [(1x4)+(0x2)+(1x1)]

= [4 + 2 + 1][ 4 + 0 + 1]

= 758

Try this: Convert 110010112 to Octal?

Convert Binary to Octal adalah dengan membahagikan nombor Binary tersebut kepada 3 bit bermula dari sebelah kanan (LSB)

1 0 122 21 20

= 4 = 2 = 1

Convert Binary to Octal (N2 - N8)

1 1 122 21 20

= 4 = 2 = 1

LSB

01012

01012 = (0x8)+(1x4)+(0x2)+(1x1)

= 0 + 4 + 0 + 1

= 516

Try this: Convert 101111012 to Hexadecimal?

Convert Binary to Hexadecimal adalah dengan membahagikan nombor binary kepada 4 bit bermula dari LSB. Sekiranya bit tersebut tidak mencukupi, maka digit ‘0’ perlu ditambah pada MSB

0 1 0 123 22 21 20

= 8 = 4 = 2 = 1

Convert Binary to Hexadecimal (N2 – N16)

LSB

181

1810 = 100102

Try this: Convert 32.20210 to binary?

Convert 8910 to binary?

Example: Convert 1810 to binary

Convert Decimal to Binary (N10 – N2)

2 18

2 9 0

2 4 1

2 2 0

2 1 0

0 1

0.3410 =

300.3410 = 454.256058

Try this: Convert 32.20210 to Octal?

Example: Convert 300.3410 to Octal i. 300 Divide by 8 ii. 0.34 Multiply by 8

Convert Decimal to Octal (N10 – N8)

8 300

8 37 4

8 4 5

8 0 4

0.34 x 8 = 2.72 ( 2+0.72 )

0.72 x 8 = 5.76 ( 5+0.76 )

0.76 x 8 = 6.08 ( 6+0.08 )

0.08 x 8 = 0.64 ( 0+0.64 )

0.64 x 8 = 5.12 ( 5+0.12 ) 0.25605

454

2010= 1416

Try this: Convert 343410 to hexadecimal?

Example: Convert 2010 to Hexadecimal

Convert Decimal to Hexadecimal (N10 – N16)

16 20 Balance

16 1 4

0 1

4 58

1001018 = [(1x4)+(0x2)+(0x1)] [(1x4)+(0x2)+(1x1)]

= [4 + 0 + 0][ 4 + 0 + 1]

= 100 1012

Try this: Convert 110010112 to Octal?

Convert Octal to Binary adalah dengan menukar setiap digit oktal kepada nilai 3 bit binary nya

1 0 122 21 20

= 4 = 2 = 1

Convert Octal to Binary (N8 – N2)

1 0 022 21 20

= 4 = 2 = 1

MSB LSB

3 A16

3A16 = [(0x8)+(0x4)+(1x2)+(1x1)][(1x8)+(0x4)+(1x2)+(0x1)]

= [0 + 0 + 2 + 1][ 8 + 0 + 2 + 0]

= 0011 10102

Try this: Convert EFA16 to Binary?

Convert Octal to Binary adalah dengan menukar setiap digit hexadecimal kepada nilai 4 bit binary nya

1 0 1 023 22 21 20

= 8 = 4 = 2 = 1

Convert Hexadecimal to Binary (N16 – N2)

0 0 1 123 22 21 20

= 8 = 4 = 2 = 1

MSB LSB

2.4: Coding System Sign and magnitude, 1’s Complement & 2’s

Complement

Apply what you have learned to the binary number systems. How do you represent negative numbers in this 8-bit binary system?

Cut the number system in half.

Use 00000001 – 01111111 to indicate positive numbers.

Use 10000000 – 11111111 to indicate negative numbers.

Notice that 00000000 is not positive or negative.

8-Bit Binary Number System

01111111

01111110

01111101

00000001

00000000

11111111

11111110

10000001

10000000

pos(+)

neg(-)

+127

+126

+125

+1

0

-1

-2

-127

-128

• As there is no third symbol available to store a negative symbol explicitly we must use a bit to show if a number is negative or not.– We name this bit the ‘Sign Bit’– We use the leftmost bit.– If the ‘Sign Bit’ is 1 then the number is

negative, if it is 0 then it is positive.

Representing Negative Numbers

• What did do you notice about the most significant bit of the binary numbers?

• The MSB is (0) for all positive numbers.

• The MSB is (1) for all negative numbers.

• The MSB is called the sign bit.

• In a signed number system, this allows you to instantly determine whether a number is positive or negative.

Sign Bit

01111111

01111110

01111101

00000001

00000000

11111111

11111110

10000001

10000000

pos(+)

neg(-)

+127

+126

+125

+1

0

-1

-2

-127

-128

• This is just inverting each bit.

1’s compliment of 00000010 is 1111101

1’s Complement

1 1 1 1 1 0

flip the number.

flip the number.

1

0 0 0 0 0 1 0

The steps in the 2’s Complement process

First, complement all of the digits in a number.

– A digit’s complement is the number you add to the digit to make it equal to the largest digit in the base (i.e., 1 for binary). In binary language, the complement of 0 is 1, and the complement of 1 is 0.

Second, add 1.

– Without this step, our number system would have two zeroes (+0 & -0), which no number system has.

2’S Complement Process

2’s Complement Examples

Example #1

Example #2

Complement Digits

Add 1

5 = 00000101

-5 = 11111011

11111010

+1

Complement Digits -13 = 11110011

13 = 00001101

00001100

+1

Using The 2’s Compliment Process

9 + (-5)

4

(-9) + 5

- 4

(-9)+ (-5)

- 4

9 + 5

14

POS + POS

POS

POS + NEG

POS

NEG + POS

NEG

NEG + NEG

NEG

Use the 2’s complement process to add together the following numbers.

POS + POS → POS Answer

If no 2’s complement is needed, use regular binary addition.

00001001 9 + 5

14

00001110

00000101 +

POS + NEG → POS Answer

Take the 2’s complement of the negative number and use regular binary addition.

00001001 9 + (-5)

4

11111011+

00000101

11111010+1

11111011

2’s Complement

Process

1]000001008th Bit = 0: Answer is Positive

Disregard 9th Bit

POS + NEG → NEG Answer

Take the 2’s complement of the negative number and use regular binary addition.

11110111 (-9) + 5

-4

00000101+

00001001

11110110+1

11110111

2’s Complement

Process

111111008th Bit = 1: Answer is Negative

11111100

00000011+1

00000100

To Check:Perform 2’s ComplementOn Answer

NEG + NEG → NEG Answer

Take the 2’s complement of both negative numbers and use regular binary addition.

11110111 (-9) + (-5)

-14

11111011 +

2’s ComplementNumbers, See Conversion ProcessIn Previous Slides

1]111100108th Bit = 1: Answer is Negative

Disregard 9th Bit

11110010

00001101+1

00001110

To Check:Perform 2’s ComplementOn Answer

2.4: Coding System Binary Coded Decimal (BCD

System) ASCII and EBCDIC

Four bits per digit

Binary-Coded Decimal (BCD)

Digit Bit pattern

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

Note: the following bit patterns are not used:

101010111100110111101111

• 709310 = ? (in BCD)

Example

7 0 9 3

0111 0000 1001 0011

• ASCII = American National Standard Code for Information Interchange

• 7-bit code• 8th bit is unused (or used for a parity bit)• 27 = 128 codes• Two general types of codes:

– 95 are “Graphic” codes (displayable on a console)

– 33 are “Control” codes (control features of the console or communications channel)

ASCII

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

ASCII Chart

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

Most significant bit

Least significant bit

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

e.g., ‘a’ = 1100001

95 Graphic codes

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

33 Control codes

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

Alphabetic codes

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

Numeric codes

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL

Punctuation, etc.

• Representing text strings, such as “Hello, world”, in a computer

The Problem

“Hello, world” Example

============

Binary010010000110010101101100011011000110111100101100001000000111011101100111011100100110110001100100

Hexadecimal48656C6C6F2C207767726C64

Decimal72

1011081081114432

119103114108100

Hello,

world

============

============

Extended BCD Interchange Code (pronounced ebb’-se-dick)• 8-bit code• Developed by IBM• Rarely used today• IBM mainframes only

EBCDIC

 EBCDIC “Extended Binary Coded Decimal Interchange Code” code

table

 EBCDIC “Extended Binary Coded Decimal Interchange Code” code

tableExample:

1111 1111 1110 1001 1111 0111 1101 0111EBCDIC CODE

Z 6

P

Message below are represented in EBCDIC code. What is the message? Please convert by using EBCDIC Code table given:

i) 1111 1100 1011 0101 1101 1001 EBCDIC CODE

LSBMSB

Recommended