CS 3401 - Computer organization & assembly language

Download Report

Transcript CS 3401 - Computer organization & assembly language

Data Representation in Computer Systems

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 1

• • • • •

Outline

Data Organization

– Bits, Nibbles, Bytes, Words, Double Words

Numbering Systems

– Unsigned Binary System – Signed and Magnitude System – 1’s Complement System – 2’s Complement System – Hexadecimal System

Floating Point Representation BCD Representation Characters

– ASCII Code – UNICODE CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 2

Data Organization

Computers use binary number system to store information as 0’s and 1’s

Bits

– A

bit

is the fundamental unit of computer storage – A bit can be 0 (off) or 1 (on) – Related bits are grouped to represent different types of information such as numbers, characters, pictures, sound, instructions CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 3

Nibbles

Nibbles

– A nibble is a group of 4 bits – A nibble is used to represent a digit in Hex (from 0-15) and BCD (from 0-9) numbers CS 3401 Comp. Org. & Assembly 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 1000 5 6 3 4

BCD

0 1 2 7 8 9 Data Representation in Computer Systems

Hex

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

Bytes

Bytes

– A

byte

is a group of 8 bits that is used to represent numbers and characters – A standard code for representing numbers and characters is ASCII (

A

merican

S

tandard

C

ode for

I

nformation

I

nterchange ) CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 5

Byte Size

Bytes

– How many different combinations of 0’s and 1’s with 8 bits can be formed?

– In general, how many different combinations of 0’s and 1’s with N bits can be formed?

– How many different characters can be represented with a byte (8 bits)?

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 6

Words

Words

– A

word

is a group of 16 bits or 2 bytes – UNICODE is an international standard code for representing characters including non-Latin characters like Asian, Greek, etc.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 7

Double Words

Double Words

– A double

word

is a group of 32 bits or 4 bytes or 2 words CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 8

Related Bytes

– A

nibble

is a half-byte (4-bit) - hex representation – A

word

is a 2-byte (16-bit) data item – A

doubleword

is a 4-byte (32-bit) data item – A

quadword

is an 8-byte (64-bit) data item – A

paragraph

is a 16-byte (128-bit) area – A – A

kilobyte

(KB) is 2

megabyte

10 (MB) is 2 = 1,024 bytes  20 1,000 bytes) = 1,048,576  1 Million Bytes – A

Gigabyte

(GB) is 2 30 = 1,073,741,824  1 Billion CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 9

Numbering Systems

• •

Unsigned number system Signed binary Systems

– Signed and magnitude system – 1’s complement system – 2’s complement system •

Hexadecimal system

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 10

Binary Number System

• •

base 10 -- has ten digits: 0,1,2,3,4,5,6,7,8,9

– positional notation 2401 = 2  10 3 + 4  10 2 + 0  10 1 + 1  10 0

base 2 -- has two digits: 0 and 1

– positional notation 1101 2 = 1  2 3 + 1  2 2 + 0  2 1 + 1  2 0 = 8 + 4 + 0 + 1 = 13 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 11

Binary Positional Notation

If

N = b n -

1

b n -

2 

b

1

b

0 then

N = b n -

1  2

n -

1

+ b n - 2

 2

n -

2

+



+ b

0  2 0 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 12

Unsigned Binary Code

Use for representing integers without signed (natural numbers) 0 0000 8 1000 1 0001 9 1001 2 3 4 5 6 7 0010 0011 0100 0101 0110 0111 10 11 12 13 14 15 1010 1011 1100 1101 1110 1111 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 13

Number of Bits Required in Unsigned Binary Code

What is the range of values that can be represented with n bits in the Unsigned Binary Code?

[0, 2

n

-1]

How many bits are required to represent a given number N in decimal?

Min. Number of Bits =

log 2 (N+1) CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 14

Decimal to Binary Conversion

• • •

The binary numbering system is the most important radix system for digital computers.

However, it is difficult to read long strings of binary numbers-- and even a modestly-sized decimal number becomes a very long binary number.

– For example: 11010100011011 2 = 13595 10

For compactness and ease of reading, binary values are usually expressed using the hexadecimal, or base-16, numbering system.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 15

Unsigned Conversion

Convert an unsigned binary number to decimal

use positional notation (polynomial expansion) •

Convert a decimal number to unsigned Binary

use successive division by 2 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 16

Examples

• •

Represent 26 10 in unsigned Binary Code

26 10 = 11010 2

Represent 26 10 using 8 bits in unsigned Binary Code

• 26 10 = 000 11010 2

Represent (26) 10 in Unsigned Binary Code using 4 bits - not possible

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 17

Signed Binary Codes

These are codes used to represent positive and negative numbers.

• • •

Sign-Magnitude System 1’s Complement System 2’s Complement System

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 18

Signed and Magnitude

The most significant (left most) bit represent the sign bit

– 0 is positive – 1 is negative •

The remaining bits represent the magnitude

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 19

Examples of Signed & Magnitude Decimal

+5 -5 +13 -13

5-bit Sign and Magnitude

0 0101 1 0101 0 1101 1 1101

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 20

Signed and Magnitude in 4 bits

0 1 2 3 4 5 6 7 0000 0001 0010 0011 0100 0101 0110 0111 -0 -1 -2 -3 -4 -5 -6 -7 1000 1001 1010 1011 1100 1101 1110 1111 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 21

Examples

Decimal S-M 8-bit S-M 26 10 0 11010 SM 0 0011010 SM -26 10 1 11010 SM 1 0011010 SM

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 22

1’s Complement System

• •

Positive numbers:

– same as in unsigned binary system – pad a 0 at the leftmost bit position

Negative numbers:

– convert the magnitude to unsigned binary system – pad a 0 at the leftmost bit position – complement every bit CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 23

Examples of 1’s Complement

Decimal 5 -5 13 -13

CS 3401 Comp. Org. & Assembly

5-bit 1’s complement 0 0101 1 1010 0 1101 1 0010

Data Representation in Computer Systems 24

1’s Complement in 4 bits

0 1 2 3 4 5 6 7 CS 3401 Comp. Org. & Assembly 0000 0001 0010 0011 0100 0101 0110 0111 -0 -1 -2 -3 -4 -5 -6 -7 1111 1110 1101 1100 1011 1010 1001 1000 Data Representation in Computer Systems 25

Examples

Decimal 1s Comp 8-bit 1s Comp 26 10 0 11010 1s 0 0011010 1s -26 10 1 00101 1s 1 1100101 1s

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 26

2’s Complement System

• •

Positive numbers:

– same as in unsigned binary system – pad a 0 at the leftmost bit position

Negative numbers:

– convert the magnitude to unsigned binary system – pad a 0 at the leftmost bit position – complement every bit – add 1 to the complement number CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 27

Examples of 2’s Complement

Decimal 5 -5 13 -13

CS 3401 Comp. Org. & Assembly

5-bit 2’s complement 0 0101 1 1011 0 1101 1 0011

Data Representation in Computer Systems 28

2’s Complement in 4 bits

0 1 2 3 4 5 6 7 0000 0001 0010 0011 0100 0101 0110 0111 -1 -2 -3 -4 -5 -6 -7 -8 1111 1110 1101 1100 1011 1010 1001 1000 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 29

Examples

Decimal 2s Comp 8-bit 2s Comp 26 10 0 11010 2s 0 0011010 2s -26 10 1 00110 2s 1 1100110 2s

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 30

More Examples

Represent 65 in 2’s complement

65 = 0100 0001 2 •

Represent -65 in 2’s complement

-65 = 1011 1111 2 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 31

Convert 2’s Complement to decimal

Positive 2’s complement numbers

– convert the same as in unsigned binary

Negative 2’s complement numbers

– complement the 2’s complement number – add 1 to the complemented number – convert the same as in unsigned binary CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 32

Examples

2’s complement Decimal 00101 5

CS 3401 Comp. Org. & Assembly

11011

-5 Mag = 00100 + 1 = 5 10011

-13 Mag =01100 + 1=13

Data Representation in Computer Systems 33

S&M 1s Comp 2s Comp CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 34

Mathematical Formula

• Formula to convert a decimal number to a 1’s complement --

N' = 2 n - N - 1

• Formula to convert a decimal number to a 2’s complement --

N' = 2 n - N

where

N

is the binary number representing the decimal with

n

number of bits CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 35

Hexadecimal Notation

• •

base 16 -- has 16 digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F

each Hex digit represents a group of 4 bits (i.e. half of a byte or a nibble) 0000 to 1111 used as a shorthand notation for long sequences of binary bits.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 36

Convert Binary Hex

Binary

1111 0110 b

Hex

F6 h 1001 1101 0000 1010 b 9D0A h 1111 0110 1110 0111 b F6E7 h 1011011 b 5B h CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 37

Examples

– ASCII value of character ‘D’ in Hex D = 0100 0100 b

ASCII

= 44 h

ASCII

– Represent 37 d in 2’s complement using Hex. 37 d = 010 0101 b

2’s

= 0 010 0101 b

2’s

= 25 h

2’s

– Represent -37 d in 2’s complement using Hex.

-37 d = 101 1011 b

2’s

= 1 101 1011 b

2’s

= DB h

2’s

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 38

Convert Hex Decimal

• •

Convert Hex to decimal

– use positional (polynomial expansion) notation 3BA h = 3  = 3  16 2 + B  256 + 11  16 1 + A 16 + 10   16 0 1 = 954 d

Convert decimal to Hex

– Use successive divisions by 16 359/16 22 R 7, 22/16 1 R 6, 1/16 0 R 1 359 d = 167 h CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 39

Covert Large Binary to Decimal

Convert 1001 0011 0101 1100 b to decimal

Method 1:

– Use polynomial expansion methods

Method 2:

– Convert number to hex, then convert it to decimal. 1001 0011 0101 1100 b = 935C h 935C h = 37724 d CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 40

Addition and Subtraction in Sign and Magnitude

CS 3401 Comp. Org. & Assembly (a) (b) (c) (d) 5 +2 7 -5 -2 -7 5 -2 3 -5 +2 -3 0101 +0010 0111 1101 +1010 1111 0101 +1010 0011 1101 +0010 1011 Data Representation in Computer Systems 41

Addition and Subtraction in 1’s Complement

1. Add bits as in base 2.

(a) 5 +2 7 0101 +0010 0111 2. Always add carry-out to result (b) -5 -2 -7 1010 +1101 1 0111 1 1000 3. overflow: if operands are of the same sign and sum of opposite sign CS 3401 Comp. Org. & Assembly (c) (d) 5 -2 3 0101 +1101 1 0010 1 0011 -5 +2 -3 1010 +0010 1100 Data Representation in Computer Systems 42

Addition and Subtraction in 2’s Complement

1. Add bits as in base 2.

2. Always discard carry-out 3. overflow: if operands are of the same sign and sum of opposite sign (a) (b) (c) (d) CS 3401 Comp. Org. & Assembly -5 -2 -7 5 +2 7 0101 +0010 0111 1011 +1110 1 1001 5 -2 3 0101 +1110 1 0011 -5 +2 -3 1011 +0010 1101 Data Representation in Computer Systems 43

Overflow Conditions in 2’s Complement Addition

If you add two numbers of the same sign and the result is of opposite sign

Overflow

5 0101 -5 1011 + + 3 0011 ---------- -8 1000 -4 1100 --------------- 7 1 0111 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 44

Overflow Conditions in 2’s Complement Addition

If Carry-in

carry-out

Overflow

0 1 11 5 0101 +3 +0011 -8 1000 1 0 00 -5 1011 -4 +1100 7 1 0111

If Carry-in = carry-out

no Overflow

0 0 00 +5 0101 -2 1110 +2 +0010 1 1 10 -6 +1010 7 0111 -8 1 1000 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 45

Addition and Subtraction in Hexadecimal System

Addition

(9F1B) 16 +(4A36) 16 : + 1 1 9F1B 4A36 E951

Subtraction

(9F1B) 16 -(4A36) 16 : 16 9F1B 4A36 54E5 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 46

Representing Real Numbers in Binary

– Fractional decimal values have nonzero digits to the right of the decimal point.

– Numerals to the right of a radix point represent negative powers of the radix: 65.4710 = 6 x 10 1 101.11 = 1  2 2 + 5 x 10 + 0  2 1 0 + 1 +  4 2  0 10 -1 + 1  + 7  10 -2 2 -1 + 1  2 -2 = 4 + 0 + 1 + ½ + ¼ = 5 + 0.5 + 0.25

= 5.

75 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 47

Representing Real Numbers in Binary

• Using the multiplication method to convert the decimal 0.8125 to binary, we multiply by the radix 2.

– The first product carries into the units place.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 48

Representing Real Numbers in Binary

• Converting 0.8125 to binary . . .

– Ignoring the value in the units place at each step, continue multiplying each fractional part by the radix.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 49

Representing Real Numbers in Binary

• Converting 0.8125 to binary . . .

– You are finished when the product is zero, or until you have reached the desired number of binary places.

– Our result, reading from top to bottom is: 0.8125

10 = 0.1101

2 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 50

Representing Real Numbers in Binary

• • •

5.75 = 101.11

How to you represent the binary point?

Fixed point notation Floating point notation

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 51

2.5 Floating-Point Representation

Floating-Point Representation

• •

Floating-point numbers allow an arbitrary number of decimal places to the right of the decimal point.

– For example: 0.5  0.25 = 0.125

They are often expressed in scientific notation.

– For example: 0.125 = 1.25  10 -1 5,000,000 = 5.0  10 6 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 52

Floating-Point Representation

• •

Floating-point numbers allow an arbitrary number of decimal places to the right of the decimal point.

– For example: 0.5  0.25 = 0.125

They are often expressed in scientific notation.

– For example: 0.125 = 1.25  10 -1 5,000,000 = 5.0  10 6 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 53

Floating-Point Representation

• •

Computers use a form of scientific notation for floating-point representation Numbers written in scientific notation have three components:

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 54

Floating-Point Representation

Computer representation of a floating-point number consists of three fixed-size fields:

This is the standard arrangement of these fields.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 55

Floating-Point Representation

• • •

The one-bit sign field is the sign of the stored value.

The size of the exponent field, determines the range of values that can be represented.

The size of the significand determines the precision of the representation.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 56

Floating-Point Representation

• •

The IEEE-754 single precision floating point standard uses an 8-bit exponent and a 23-bit significand.

The IEEE-754 double precision standard uses an 11-bit exponent and a 52-bit significand.

For illustrative purposes, we will use a 14-bit model with a 5-bit exponent and an 8-bit significand.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 57

Floating-Point Representation

• • •

The significand of a floating-point number is always preceded by an implied binary point.

Thus, the significand always contains a fractional binary value.

The exponent indicates the power of 2 to which the significand is raised.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 58

Floating-Point Representation

• • •

Example:

– Express 32 10 in the simplified 14-bit floating-point model.

We know that 32 is 2 5 . So in (binary) scientific notation 32 = 1.0 x 2 5 = 0.1 x 2 6 .

Using this information, we put 110 (= 6 10 ) in the exponent field and 1 in the significand as shown.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 59

Floating-Point Representation

• The illustrations shown at the right are

all

equivalent representations for 32 using our simplified model.

• Not only do these synonymous representations waste space, but they can also cause confusion.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 60

Floating-Point Representation

Another problem with our system is that we have made no allowances for negative exponents. We have no way to express 0.5 (=2 1 )! (Notice that there is no sign in the exponent field!)

All of these problems can be fixed with no changes to our basic model.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 61

Floating-Point Representation

To resolve the problem of synonymous forms, we will establish a rule that the first digit of the significand must be 1. This results in a unique pattern for each floating point number.

– In the IEEE-754 standard, this 1 is implied meaning that a 1 is assumed after the binary point.

– By using an implied 1, we increase the precision of the representation by a power of two. (Why?)

In our simple instructional model, we will use no implied bits.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 62

Floating-Point Representation

• • •

To provide for negative exponents, we will use a biased exponent.

A bias is a number that is approximately midway in the range of values expressible by the exponent. We subtract the bias from the value in the exponent to determine its true value.

– In our case, we have a 5-bit exponent. We will use 16 for our bias. This is called

excess-16

representation.

In our model, exponent values less than 16 are negative, representing fractional numbers.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 63

Floating-Point Representation

• • • •

Example:

– Express 32 10 in the revised 14-bit floating-point model.

We know that 32 = 1.0 x 2 5 = 0.1 x 2 6 .

To use our excess 16 biased exponent, we add 16 to 6, giving 22 10 (=10110 2 ). Graphically:

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 64

Floating-Point Representation

• • •

Example:

– Express 0.0625

10 in the revised 14-bit floating-point model.

We know that 0.0625 is 2 -4 . So in (binary) scientific notation 0.0625 = 1.0 x 2 -4 = 0.1 x 2 -3 .

To use our excess 16 biased exponent, we add 16 to -3, giving 13 10 (=01101 2 ).

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 65

Floating-Point Representation

• • •

Example:

– Express -26.625

10 in the revised 14-bit floating-point model.

We find 26.625

have: 26.625

10 10 = 11010.101

2 . Normalizing, we = 0.11010101 x 2 5 .

To use our excess 16 biased exponent, we add 16 to 5, giving 21 sign bit. 10 (=10101 2 ). We also need a 1 in the

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 66

IEEE Floating Point Standards

• •

The IEEE-754 single precision floating point standard uses bias of 127 over its 8-bit exponent.

– An exponent of 255 indicates a special value.

• If the significand is zero, the value is  infinity.

• If the significand is nonzero, the value is NaN, “not a number,” often used to flag an error condition.

The double precision standard has a bias of 1023 over its 11-bit exponent.

– The “special” exponent value for a double precision number is 2047, instead of the 255 used by the single precision standard.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 67

IEEE Floating Point Standards

Both the 14-bit model that we have presented and the IEEE-754 floating point standard allow two representations for zero.

– Zero is indicated by all zeros in the exponent and the significand, but the sign bit can be either 0 or 1.

This is why programmers should avoid testing a floating-point value for equality to zero.

– Negative zero does not equal positive zero.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 68

Examples:

Represent 51.875 using the following FP format:

– Matissa: 10 bits – Exponent: 5 bits with 16 bias •

Convert the following FP number written using the above format, to decimal: 1100111010110010

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 69

Floating-Point Representation: Addition & Subtraction

• • •

Floating-point addition and subtraction are done using methods analogous to how we perform calculations using pencil and paper.

The first thing that we do is express both operands in the same exponential power, then add the numbers, preserving the exponent in the sum.

If the exponent requires adjustment to normalize the mantissa, we do so at the end of the calculation.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 70

Floating-Point Representation: Addition & Subtraction

• • •

Example:

– Find the sum of 12 10 model.

and 1.25

10 using the 14-bit floating-point

We find 12 10 = 0.1100 x 2 = 0.000101 x 2 4 .

4 . And 1.25

10 = 0.101 x 2 1 Thus, our sum is 0.110101 x 2 4 .

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 71

Floating-Point Representation: Multiplication

• •

Floating-point multiplication is also carried out in a manner akin to how we perform multiplication using pencil and paper.

We multiply the two operands and add their exponents.

If the exponent requires adjustment, we do so at the end of the calculation.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 72

Floating-Point Representation: Multiplication

• • • •

Example:

– Find the product of 12 10 point model.

and 1.25

10 using the 14-bit floating-

We find 12 10 = 0.1100 x 2 1 .

Thus, our product 4 . And 1.25

10 = 0.101 x 2 is 0.0111100 x 2 5 = 0.1111 x 2 4 . The normalized product requires an exponent of 20 10 = 10110 2 .

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 73

Floating-Point Representation: Multiplication

• • • •

No matter how many bits we use in a floating point representation, our model must be finite.

The real number system is, of course, infinite, so our models can give nothing more than an approximation of a real value. At some point, every model breaks down, introducing errors into our calculations.

By using a greater number of bits in our model, we can reduce these errors, but we can never totally eliminate them.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 74

Floating-Point Representation

• • •

Our job becomes one of reducing error, or at least being aware of the possible magnitude of error in our calculations.

We must also be aware that errors can compound through repetitive arithmetic operations.

For example, our 14-bit model cannot exactly represent the decimal value 128.5. In binary, it is 9 bits wide:

10000000.1

2 = 128.5

10 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 75

Floating-Point Representation

When we try to express 128.5

10 in our 14-bit model, we lose the low-order bit, giving a relative error of:

128.5 - 128 128  0.39% •

If we had a procedure that repetitively added 0.5 to 128.5, we would have an error of nearly 2% after only four iterations.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 76

Floating-Point Representation

Floating-point errors can be reduced when we use operands that are similar in magnitude.

If we were repetitively adding 0.5 to 128.5, it would have been better to iteratively add 0.5 to itself and then add 128.5 to this sum.

In this example, the error was caused by loss of the low-order bit.

Loss of the high-order bit is more problematic.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 77

Floating-Point Representation

• • •

Floating-point overflow and underflow can cause programs to crash.

Overflow occurs when there is no room to store the high-order bits resulting from a calculation.

Underflow occurs when a value is too small to store, possibly resulting in division by zero.

Experienced programmers know that it’s better for a program to crash than to have it produce incorrect, but plausible, results.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 78

Character Representations

• • •

BCD & EBCDIC ASCII UNICODE

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 79

Character Codes

• • •

Calculations aren’t useful until their results can be displayed in a manner that is meaningful to people.

We also need to store the results of calculations, and provide a means for data input.

Thus, human-understandable characters must be converted to computer understandable bit patterns using some sort of character encoding scheme.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 80

Character Codes

• •

As computers have evolved, character codes have evolved.

Larger computer memories and storage devices permit richer character codes.

The earliest computer coding systems used six bits.

Binary-coded decimal (BCD) was one of these early codes. It was used by IBM mainframes in the 1950s and 1960s.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 81

Character Codes

In 1964, BCD was extended to an 8-bit code, Extended Binary-Coded Decimal Interchange Code (EBCDIC).

EBCDIC was one of the first widely-used computer codes that supported upper and lowercase alphabetic characters, in addition to special characters, such as punctuation and control characters.

EBCDIC and BCD are still in use by IBM mainframes today.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 82

Character Codes

• • •

Other computer manufacturers chose the 7 bit ASCII (American Standard Code for Information Interchange) as a replacement for 6-bit codes.

While BCD and EBCDIC were based upon punched card codes, ASCII was based upon telecommunications (Telex) codes.

Until recently, ASCII was the dominant character code outside the IBM mainframe world.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 83

Character Codes

ASCII: American Standard Code for Information Interchange.

– –

Used to represent characters and control information Each character is represented with 1 byte

• • • •

upper and lower case letters: a...z and A...Z

decimal digits -- 0,1,…,9 punctuation characters -- ; , . : special characters --$ & @ / {

control characters -- carriage return (CR) , line feed (LF), beep

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 84

Examples of ASCII Code

Bit contents (S): Bit position: 01010011 76543210

S 83 (decimal) , 53 (hex)

Bit contents (8): Bit position: 00111000 76543210

8 56 (decimal) , 38 (hex) CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 85

ASCII Code in Binary and Hex Character

A

Binary

0100 0001 D a ?

2 DEL 0100 0100 0110 0001 0011 1111 0011 0010 0111 1111 CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems

Hex

41 44 61 3F 32 7F 86

ASCII Groups

Bit 6 Bit 5

0 0 1 0 1 CS 3401 Comp. Org. & Assembly 1 0 1

Group

Control Character Digits & Punctuation Upper Case & Special Lower Case & Special Data Representation in Computer Systems 87

Character Codes

Many of today’s systems embrace Unicode, a 16-bit system that can encode the characters of every language in the world.

– The Java programming language, and some operating systems now use Unicode as their default character code.

The Unicode codespace is divided into six parts. The first part is for Western alphabet codes, including English, Greek, and Russian.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 88

Character Codes

• • •

The Unicode codes pace allocation is shown at the right.

The lowest numbered Unicode characters comprise the ASCII code.

The highest provide for user-defined codes.

CS 3401 Comp. Org. & Assembly Data Representation in Computer Systems 89