computer engineering (logic circuits) lec. # 2 dr. tamer samy gaafar dept. of computer & systems...

44
Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Upload: wilfred-park

Post on 17-Dec-2015

220 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Computer Engineering(Logic Circuits)

Lec. # 2

Dr. Tamer Samy Gaafar

Dept. of Computer & Systems EngineeringFaculty of Engineering

Zagazig University

Page 2: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Course Web Page

http://www.tsgaafar.faculty.zu.edu.eg

Email: [email protected]

Page 3: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Sections will start this week (today)

A quiz will be held on the next week lecture (23/2/2015) at the 1st 20 mins.

The quiz will be on ch.1 as a whole.

No cheating is allowed (zero for both cheaters).

Announcements

Page 4: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Lecture 2

Number Systems (cont.)

Page 5: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

5

Binary Numbers Again•Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2N-1.

4 bits = 0 to 158 bits = 0 to 25516 bits = 0 to 65535

•Besides simply representation, we would like to also do arithmetic operations on numbers in binary form.

•Principal operations are addition and subtraction.

Page 6: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

6

Binary Arithmetic, SubtractionThe rules for binary arithmetic are:

0 + 0 = 0, carry = 0

1 + 0 = 1, carry = 0

0 + 1 = 1, carry = 0

1 + 1 = 0, carry = 1

The rules for binary subtraction are:

0 - 0 = 0, borrow = 0

1 - 0 = 1, borrow = 0

0 - 1 = 1, borrow = 1

1 - 1 = 0, borrow = 0

Borrows, Carries from/to digits to left of current of digit.

Binary subtraction, addition works just the same as decimal addition, subtraction.

Page 7: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

7

Binary, Decimal addition

34

+ 17------ 51

from LSD to MSD:7+4 = 1; with carry out of 1 to next column

1 (carry) + 3 + 1 = 5.answer = 51.

Decimal

101011

+ 000001--------------- 101100

From LSB to MSB:1+1 = 0, carry of 11 (carry)+1+0 = 0, carry of 11 (carry)+0 + 0 = 1, no carry1 +0 = 10 + 0 = 01 + 0 = 1 answer = 101100

Binary

Page 8: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

8

SubtractionDecimal

900

- 001------- 899

0-1 = 9; with borrow of 1 from next column0 -1 (borrow) - 0 = 9, with borrow of 1 9 - 1 (borrow) - 0 = 8.Answer = 899.

Binary 100

- 001 ------- 011

0-1 = 1; with borrow of 1 from next column0 -1 (borrow) - 0 = 1, with borrow of 1 1 - 1 (borrow) - 0 = 0.Answer = 011.

Page 9: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

9

Number systems

◦ Sign and magnitude

◦ Ones-complement ( 9’s Complement )

◦ Twos-complement (10’s Complement )

Signed Numbers

Page 10: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Decimal

Binary

10

Arithmetic

1 1 1011+ 1010

10101

1011– 0110 0101

11 1234+ 5678

6912

5274– 1638 3636

Page 11: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

How do we write negative binary numbers?◦ Prefix numbers with minus symbol?

3 approaches:

◦ Sign and magnitude◦ Ones-complement◦ Twos-complement

All 3 approaches represent positive numbers in the same way

11

Negative numbers

Page 12: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

12

Most significant bit (MSB) is the sign bit◦ 0 ≡ positive◦ 1 ≡ negative

Remaining bits are the number's magnitude

Sign and magnitude

0000

0001

0011

1111

1110

1100

1011

1010

1000 0111

0110

0100

0010

0101

1001

1101

+ 0

+ 1

+ 2

+ 3

+ 4

+ 5

+ 6

+ 7– 0

– 1

– 2

– 3

– 4

– 5

– 6

– 7

Page 13: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Problem 1: Two representations of for zero◦ +0 = 0000 and also –0 = 1000

Problem 2: Arithmetic is cumbersome

◦ 4 – 3 != 4 + (-3)

13

Sign and magnitude

Page 14: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

14

Negative number: Bitwise complement of positive number◦ 0111 ≡ 710

◦ 1000 ≡ –710

Ones(1’s)-complement

0000

0001

0011

1111

1110

1100

1011

1010

1000 0111

0110

0100

0010

0101

1001

1101

+ 0

+ 1

+ 2

+ 3

+ 4

+ 5

+ 6

+ 7– 7

– 6

– 5

– 4

– 3

– 2

– 1

– 0

Page 15: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

15

Solves the arithmetic problem

1’s complement

end-around carry

Page 16: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

16

The ones-complement of an 4-bit positive number y is 11112 – y◦ 0111 ≡ 710

◦ 11112 – 01112 = 10002 ≡ –710

What is 11112?◦ 1 less than 100002 = 24 – 1◦ –y is represented by (24 – 1) – y

Why 1’s complement works

Page 17: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

17

Still have two representations for zero!

◦ +0 = 0000 and also –0 = 1111

So what's wrong?

Page 18: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Negative number: Bitwise complement plus one◦ 0111 ≡ 710

◦ 1001 ≡ –710

Benefits:◦ Simplifies

arithmetic◦ Only one zero!

18

Twos (2’s)-complement

0000

0001

0011

1111

1110

1100

1011

1010

1000 0111

0110

0100

0010

0101

1001

1101

0

+ 1

+ 2

+ 3

+ 4

+ 5

+ 6

+ 7– 8

– 7

– 6

– 5

– 4

– 3

– 2

– 1

Page 19: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

19

2’s complement

Page 20: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Recall: The ones-complement of a b-bit positive number y is (2b – 1) – y

Twos-complement adds one to the bitwise complement, thus, –y is 2b – y

Or Leaving all zeros and the first one from the

right as it is then, complement each 0 and 1 after the first 1 from the right

20

Obtaining 2’s complement

Page 21: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

21

Adding representations of x and –y where x, y are positive numbers, we get x + (2b – y) = 2b + (x – y)◦ If there is a carry, that means that x y and

dropping the carry yields x – y

◦ If there is no carry, then x < y, then we can think of it as 2b – (y – x), which is the twos-complement representation of the negative number resulting from x – y.

Why 2’s complement works

Page 22: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

22

Sign-extension

◦ Write +6 and –6 as twos-complement 0110 and 1010

◦ Sign-extend to 8-bit bytes

00000110 and 11111010

Can't infer a representation from a number

◦ 11001 is 25 (unsigned)◦ 11001 is -9 (sign and magnitude)◦ 11001 is -6 (ones complement)◦ 11001 is -7 (twos complement)

Miscellaneous

Page 23: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

10’s Complement Process The 10’s Complement process uses base-10 (decimal) numbers. Later, when we’re working with base-2 (binary) numbers, you will see that the 2’s Complement process works in the same way.

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., 9 for decimal). The complement of 0 is 9, 1 is 8, 2 is 7, etc.

Second, add 1.

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

23

Page 24: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

10’s Complement Examples

24

-003

+1

996

997

-214

+1

785

786

Example #1

Example #2

Complement Digits

Add 1

Complement Digits

Add 1

Page 25: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

8-Bit Binary Number SystemApply 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.

25

01111111

01111110

01111101

00000001

00000000

11111111

11111110

10000001

10000000

pos(+)

neg(-)

+127

+126

+125

+1

0

-1

-2

-127

-128

Page 26: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Sign Bit 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.

26

01111111

01111110

01111101

00000001

00000000

11111111

11111110

10000001

10000000

pos(+)

neg(-)

+127

+126

+125

+1

0

-1

-2

-127

-128

Page 27: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

2’S Complement Process The steps in the 2’s Complement process are similar to the 10’s Complement process. However, you will now use the base two.

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.

27

Page 28: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

2’s Complement Examples

28

Example #1

Example #2

Complement Digits

Add 1

5 = 00000101

-5 = 11111011

11111010

+1

Complement Digits

Add 1

-13 = 11110011

13 = 00001101

00001100

+1

Page 29: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Using The 2’s Complement Process

29

9 + (-5)

4

(-9) + 5

- 4

(-9)+ (-5)

- 14

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.

Page 30: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

POS + POS → POS Answer

30

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

00001001 9 + 5

14

00001110

00000101 +

Page 31: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

POS + NEG → POS Answer

31

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

Page 32: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

POS + NEG → NEG Answer

32

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

Page 33: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

NEG + NEG → NEG Answer

33

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

Page 34: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

34

◦ Summing two positive numbers can give a negative result

◦ Summing two negative numbers can give a positive result

Overflow

00000001

0011

11111110

1100

10111010

1000 01110110

0100

0010

01011001

1101

0+ 1

+ 2

+ 3

+ 4

+ 5

+ 6+ 7– 8

– 7

– 6

– 5

– 4

– 3– 2

– 10000

0001

0011

11111110

1100

10111010

1000 01110110

0100

0010

01011001

1101

0+ 1

+ 2

+ 3

+ 4

+ 5

+ 6+ 7– 8

– 7

– 6

– 5

– 4

– 3– 2

– 1

6 + 4 ⇒ –6 –7 – 3 ⇒ +6

Page 35: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

35

Addition Examples (8 bit numbers)

Add 7 and 4 (both positive)

Add 15 and -6 (positive > negative)

Add 16 and -24 (negative > positive)

Add -5 and -9 (both negative)

00000111 7+00000100 +4 00001011 11

00001111 15 +11111010 +(-6)1 00001001 9Discard carry

00010000 16+11101000 +(-24) 11111000 -8Sign bit is negative so negative

number in 2’s complement form

11111011 -5 +11110111 +-9 1 11110010 -14Discard carry

Page 36: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

36

Subtraction Examples Find 8 minus 3.

Find 12 minus -9.

Find -25 minus 19.

Find -120 minus -30.

00001000 8 +11111101 -31 00000101 5Discard carry

MinuendSubtrahendDifference

11100111 -25 +11101101 - 191 11010100 -44Discard carry

00001100 12 +00001001 - -9 00010101 21

10001000 -120 +00011110 - -30 10100110 -90

Page 37: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

BCD Addition

Page 38: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

Result must be corrected if

1. Result > 9 (further during all the addition steps)

2. A carry occurs in the first addition step only from one digit ( 4bit binary ) to another.

BCD Addition Rules

Page 39: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

BCD example

Example #1

52 +

46----- ----------------

98

No correction is needed

39

0101 0010

0100 0110

1001 1000 nc

Page 40: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

40

BCD example Example #2

17 0 0001 0111 +

56 0 0101 0110----- ---------------------

73 0 nc 0110 nc 1101 Correction 0 0000 0110

--------------------0 0111 0011

Page 41: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

9/15/09 - L2Copyright 2009 - Joanne DeGroat,

ECE, OSU 41

BCD example Example #3

39 0 0011 1001 +

28 0 0010 1000----- ---------------------

67 0 nc 0110 c 0001

Correction 0 0000 0110--------------------0 0110 0111

Page 42: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

42

BCD example Example #4

71 0 0111 0001 +

65 0 0110 0101----- --------------------- 136 0 nc 1101 nc 0110 Correction 0 0110 0000

-------------------- 0 0001 0011 0110

Page 43: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

43

BCD example Example #5

87 0 1000 0111 +

15 0 0001 0101----- ---------------------

102 0 nc 1001 nc 1100

Correction 0 0000 0110--------------------0 1010 0010

0 0110 0000 -------------------------

0 0001 0000 0010

Page 44: Computer Engineering (Logic Circuits) Lec. # 2 Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University