cmsc 313 lecture 02 - department of computer …chang/cs313.f04/topics/slides...cmsc 313 lecture 02...

29
CMSC 313 Lecture 02 • Bits of Memory • Data formats for negative numbers signed magnitude one’s complement two’s complement excess bias Modulo arithmetic & two’s complement UMBC, CMSC313, Richard Chang <[email protected]>

Upload: vothu

Post on 15-Apr-2018

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

CMSC 313 Lecture 02

• Bits of Memory

• Data formats for negative numberssigned magnitude

one’s complement

two’s complement

excess bias

• Modulo arithmetic & two’s complement

UMBC, CMSC313, Richard Chang <[email protected]>

Page 2: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-5

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Base Conversion with the RemainderMethod

• Example: Convert 23.375 10 to base 2. Start by converting the inte-ger portion:

23/2 = 11 R 1

11/2 = 5 R 1

5/2 = 2 R 1

2/2 = 1 R 0

1/2 = 0 R 1

Integer Remainder

Least significant bit

Most significant bit

(23)10 = (10111)2

Page 3: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-6

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Base Conversion with the Multiplica-tion Method

• Now, convert the fraction:

.375 × 2 = 0.75

.75 × 2 = 1.50

.5 × 2 = 1.00

Least significant bit

Most significant bit

(.375)10 = (.011)2

• Putting it all together, 23.375 10 = 10111.0112.

Page 4: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-7

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Nonterminating Base 2 Fraction• We can’t always convert a terminating base 10 fraction into an

equivalent terminating base 2 fraction:

.2

.4

.8

.6

.2

.

.

.

0.4

0.8

1.6

1.2

0.4

=

=

=

=

=

2

2

2

2

2

×

×

×

×

×

Page 5: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 1: Introduction1-6

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Syst

em B

us

Data Bus

Address Bus

Control Bus

(ALU, Registers,

and Control)

Memory Input and Output (I/O)

CPU

The System Bus Model• A refinement of the von Neumann model, the system bus model

has a CPU (ALU and control), memory, and an input/output unit.

• Communication among components is handled by a shared path-way called the system bus , which is made up of the data bus, theaddress bus, and the control bus. There is also a power bus, andsome architectures may also have a separate I/O bus.

Page 6: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Random Access Memory (RAM)

• A single byte of memory holds 8 binary digits (bits).

• Each byte of memory has its own address.• A 32-bit CPU can address 4 gigabytes of memory,

but a machine may have much less (e.g., 256MB).

• For now, think of RAM as one big array of bytes.

• The data stored in a byte of memory is not typed.• The assembly language programmer must

remember whether the data stored in a byte is a character, an unsigned number, a signed number, part of a multi-byte number, ...

UMBC, CMSC313, Richard Chang <[email protected]>

Page 7: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-11

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Signed Fixed Point Numbers• For an 8-bit number, there are 2 8 = 256 possible bit patterns.

These bit patterns can represent negative numbers if we chooseto assign bit patterns to numbers in this way. We can assign halfof the bit patterns to negative numbers and half of the bit patternsto positive numbers.

• Four signed representations we will cover are:

Signed Magnitude

One’s Complement

Two’ s Complement

Excess (Biased)

Page 8: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-12

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Signed Magnitude• Also know as “sign and magnitude,” the leftmost bit is the sign (0

= positive, 1 = negative) and the remaining bits are the magnitude.

• Example:

+2510 = 000110012

-2510 = 100110012

• Two representations for zero: +0 = 00000000 2, -0 = 100000002.

• Largest number is +127, smallest number is -127 10, using an 8-bitrepresentation.

Page 9: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-13

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

One’s Complement• The leftmost bit is the sign (0 = positive, 1 = negative). Negative of

a number is obtained by subtracting each bit from 2 (essentially,complementing each bit from 0 to 1 or from 1 to 0). This goes bothways: converting positive numbers to negative numbers, and con-verting negative numbers to positive numbers.

• Example:

+2510 = 000110012

-2510 = 111001102

• Two representations for zero: +0 = 00000000 2, -0 = 111111112.

• Largest number is +127 10, smallest number is -127 10, using an 8-bit representation.

Page 10: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-14

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Two’s Complement• The leftmost bit is the sign (0 = positive, 1 = negative). Negative of

a number is obtained by adding 1 to the one’s complement nega-tive. This goes both ways, converting between positive and nega-tive numbers.

• Example (recall that -25 10 in one’s complement is 11100110 2):

+2510 = 000110012

-2510 = 111001112

• One representation for zero: +0 = 00000000 2, -0 = 000000002.

• Largest number is +127 10, smallest number is -128 10, using an 8-bit representation.

Page 11: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-15

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Excess (Biased)• The leftmost bit is the sign (usually 1 = positive, 0 = negative).

Positive and negative representations of a number are obtainedby adding a bias to the two’s complement representation. Thisgoes both ways, converting between positive and negative num-bers. The effect is that numerically smaller numbers have smallerbit patterns, simplifying comparisons for floating point exponents.

• Example (excess 128 “adds” 128 to the two’s complement ver-sion, ignoring any carry out of the most significant bit) :

+1210 = 100011002

-1210 = 011101002

• One representation for zero: +0 = 10000000 2, -0 = 100000002.

• Largest number is +127 10, smallest number is -128 10, using an 8-bit representation.

Page 12: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Example: Convert -123

• Signed Magnitude12310 = 64 + 32 + 16 + 8 + 2 + 1 = 0111 10112-12310 => 1111 10112

• One’s Complement (flip the bits)-12310 => 1000 01002

• Two’s Complement (add 1 to one’s complement)-12310 => 1000 01012

• Excess 128 (add 128 to two’s complement)-12310 => 0000 01012

UMBC, CMSC313, Richard Chang <[email protected]>

Page 13: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

3-bit Signed Integer Representations

UMBC, CMSC313, Richard Chang <[email protected]>

000

001

010

011

100

101

110

111

100

101

110

111

000

001

010

011

100

101

110

000/111

001

010

011

111

110

101

000/100

001

010

011

000

001

010

011

100

101

110

111

-3

-4

-2

-1

0

1

2

3

4

5

6

7

Excess 42’s Comp1’s CompSign MagUnsignedDecimal

Page 14: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 2: Data Representation2-10

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Binary Addition• This simple binary addition example provides background for the

signed number representations to follow.

Operands0

0+

00

SumCarry out

Carry in 0

0

1+

10

0

1

0+

10

0

1

1+

01

0

Example:

Carry

Addend: A

Augend: B

Sum

0 1 1 1 1 1 0 0

0 1 0 1 1 0 1 0

1 1 1 1 0 0 0 0

1 1 0 1 0 1 1 0

+

(124)10

(90)10

(214)10

0

0+

10

1

0

1+

01

1

1

0+

01

1

1

1+

11

1

Page 15: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 3: Arithmetic3-4

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Number Circle for 3-Bit Two’sComplement Numbers

• Numbers can be added or subtracted by traversing the numbercircle clockwise for addition and counterclockwise for subtraction.

• Overflow occurs when a transition is made from +3 to -4 while pro-ceeding around the number circle when adding, or from -4 to +3while subtracting.

100

010110

000

111

101 011

001

0

1

2

3

-4

-3

-2

-1

Adding numbers

Subtracting numbers

Page 16: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

8-bit Two’s Complement Addition

UMBC, CMSC313, Richard Chang <[email protected]>

4410 = 0010 1100+ -4810 = 1101 0000 -410 = 1111 1100

5410 = 0011 0110+ -4810 = 1101 0000 610 = 0000 0110

-4410 = 1101 0100+ -4810 = 1101 0000 -9210 = 1010 0100

Page 17: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 3: Arithmetic3-11

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

One’s Complement Addition• An example of one’s complement integer addition with an end-

around carry:

+

1

10

0

01

0

01

0

10

0

11

0

(–12)10(+13)10

+

0

0

0

0

1

1 (+1)10

End-around carry

• The end-around carry is needed because there are two represen-tations for 0 in one’s complement. Both representations for 0 arevisited when one or both operands are negative.

Page 18: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 3: Arithmetic3-12

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Number Circle (Revisited)• Number circle for a three-bit signed one’s complement represen-

tation. Notice the two representations for 0.

100

010110

000

111

101 011

001

+0

1

2

3

-3

-2

-1

-0

Adding numbers

Subtracting numbers

Page 19: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 3: Arithmetic3-13

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

End-Around Carry for Fractions• The end-around carry complicates one’s complement addition for

non-integers, and is generally not used for this situation.

• The issue is that the distance between the two representations of0 is 1.0, whereas the rightmost fraction position is less than 1.

1

01

0

11

0

01

1

10

1

.

.

.

(+5.5)10(–1.0)10

+

(+4.5)10

10

1

+

0

1

0

1

0

.

.

0

1

Richard Chang
1 1 1 0 . 0 +
Richard Chang
1 . 0
Richard Chang
Richard Chang
Page 20: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Chapter 3: Arithmetic3-13

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

End-Around Carry for Fractions• The end-around carry complicates one’s complement addition for

non-integers, and is generally not used for this situation.

• The issue is that the distance between the two representations of0 is 1.0, whereas the rightmost fraction position is less than 1.

1

01

0

11

1

01

0

10

0

.

.

.

(+5.5)10(–1.0)10

+

(+4.5)10

11

0

+

0

1

0

0

0

.

.

1

1

Richard Chang
Page 21: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Two’s Complement Overflow

• An overflow occurs if adding two positive numbers yields a negative result or if adding two negative numbers yields a positive result.

• Adding a positive and a negative number never causes an overflow.

• Carry out of the most significant bit does not indicate an overflow.

• An overflow occurs when the carry into the most significant bit differs from the carry out of the most significant bit.

UMBC, CMSC313, Richard Chang <[email protected]>

Page 22: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Two’s Complement Overflow Examples

UMBC, CMSC313, Richard Chang <[email protected]>

-10310 = 1001 1001+ -4810 = 1101 0000 -15110 ≠ 0110 1001

5410 = 0011 0110+ 10810 = 0110 1100 16210 ≠ 1010 0010

Page 23: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Is Two’s Complement “Magic”?

• Why does adding positive and negative numbers work?

• Why do we add 1 to the one’s complement to negate?

• Answer: Because modulo arithmetic works.

UMBC, CMSC313, Richard Chang <[email protected]>

Page 24: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Modulo Arithmetic

• Definition: Let a and b be integers and let m be a positive integer. We

say that a ≡ b (mod m) if the remainder of a divided by m is equal to

the remanider of b divided by m.

• In the C programming language, a ≡ b (mod m) would be written

a % m == b % m

• We use the theorem:

If a ≡ b (mod m) and c ≡ d (mod m)

then a + c ≡ b + d (mod m).

� � � � 1

Page 25: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

A Theorem of Modulo Arithmetic

Thm: If a ≡ b (mod m) and c ≡ d (mod m) then a + c ≡ b + d (mod m).

Example: Let m = 8, a = 3, b = 27, c = 2 and d = 18.

3 ≡ 27 (mod 8) and 2 ≡ 18 (mod 8).

5 ≡ 45 (mod 8).

Proof: Write a = qam + ra, b = qbm + rb, c = qcm + rc and d = qdm + rd,

where ra, rb, rc and rd are between 0 and m − 1. Then,

a + c = (qa + qc)m + ra + rc

b + d = (qb + qd)m + rb + rd = (qb + qd)m + ra + rc.

Thus, a + c ≡ ra + rc ≡ b + d (mod m).

� � � � 2

Page 26: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Consider Numbers Modulo 256

0000 00002 = 0 ≡ −256 ≡ 256 ≡ 512

0000 00012 = 1 ≡ −255 ≡ 257 ≡ 513

0000 00102 = 2 ≡ −254 ≡ 258 ≡ 514...

0000 11112 = 15 ≡ −241 ≡ 271 ≡ 527...

0111 11112 = 127 ≡ −129 ≡ 383 ≡ 639

1000 00002 = 128 ≡ −128 ≡ 384 ≡ 640...

1000 11112 = 143 ≡ −113 ≡ 399 ≡ 655...

1111 00112 = 243 ≡ −13 ≡ 499 ≡ 755...

1111 11112 = 255 ≡ −1 ≡ 511 ≡ 767

If 0000 00002 thru 0111 11112 represents 0 thru 127 and 1000 00002 thru 1111 11112

represents -128 thru -1, then the most significant bit can be used to determine the sign.

� � � � 3

Page 27: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Some Answers

• In 8-bit two’s complement, we use addition modulo 28 = 256, so adding

256 or subtracting 256 is equivalent to adding 0 or subtracting 0.

• To negate a number x, 0 ≤ x ≤ 128:

−x = 0 − x ≡ 256 − x = (255 − x) + 1 = (1111 11112 − x) + 1

Note that 1111 11112 − x is the one’s complement of x.

• Now we can just add positive and negative numbers. For example:

3 + (−5) ≡ 3 + (256 − 5) = 3 + 251 = 254 ≡ 254 − 256 = −2.

or two negative numbers (as long as there’s no overflow):

(−3) + (−5) ≡ (256 − 3) + (256 − 5) = 504 ≡ 504 − 512 = −8.

� � � � 4

Page 28: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

CMSC 313 Computer Organization & Assembly Language Programming Fall 2004Homework 1

For the following questions, show all of your work. It is not sufficient to provide the answers.

Exercise 1. Convert the following numbers.

a. 6234710 to unsigned binary

b. 8DF616 to base 2

c. 41.37510 to base 4

d. 10011101.01012 to base 10

Exercise 2. Convert each of the following numbers to 8-bit signed magnitude, 8-bit one’scomplement, 8-bit two’s complement and 8-bit excess 128 formats.

a. (−122)10

b. (−31)10

c. (−16)10

d. 12710

Exercise 3. Find the decimal equivalents for the following 8-bit two’s complement num-bers.

a. 1000 0001

b. 0111 1011

c. 1111 0001

d. 0010 1010

Exercise 4. Perform two’s complement addition on the following pairs of numbers. Ineach case, indicate whether an overflow has occured.

a. 1110 1011 + 0110 1001

b. 1110 1011 + 1111 1111

c. 1000 1100 + 1100 0001

d. 0111 1001 + 0000 1001

Page 29: CMSC 313 Lecture 02 - Department of Computer …chang/cs313.f04/topics/Slides...CMSC 313 Lecture 02 •Bits of Memory •Data formats for negative numbers signed magnitude one’s

Next Time

• Multiplication

• Floating Point numbers• ASCII code

UMBC, CMSC313, Richard Chang <[email protected]>