Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

34
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction

Transcript of Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Page 1: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Cosc 2150:Computer Organization

Chapter 2Part 1 Integers

addition and subtraction

Page 2: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Introduction

• A bit is the most basic unit of information in a computer.—It is a state of “on” or “off” in a digital circuit.—Sometimes these states are “high” or “low”

voltage instead of “on” or “off..”

• A byte is a group of eight bits.—A byte is the smallest possible addressable

unit of computer storage.—The term, “addressable,” means that a

particular byte can be retrieved according to its location in memory.

Page 3: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• A word is a contiguous group of bytes.

—Words can be any number of bits or bytes.

—Word sizes of 16, 32, or 64 bits are most common.

—In a word-addressable system, a word is the smallest addressable unit of storage.

• A group of four bits is called a nibble.

—Bytes, therefore, consist of two nibbles: a “high-order nibble,” and a “low-order” nibble.

Introduction (2)

Page 4: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Positional Numbering Systems

• Bytes store numbers using the position of each bit to represent a power of 2.

—The binary system is also called the base-2 system.

—Our decimal system is the base-10 system. It uses powers of 10 for each position in a number.

—Any integer quantity can be represented exactly using any base (or radix).

Page 5: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• The decimal number 947 in powers of 10 is:

• The decimal number 5836.47 in powers of 10 is:

5 103 + 8 102 + 3 101 + 6 100 + 4 10-1 + 7 10-2

9 102 + 4 101 + 7 100

Positional Numbering Systems (2)

Page 6: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• The binary number 11001 in powers of 2 is:

• When the radix of a number is something other than 10, the base is denoted by a subscript.

—Sometimes, the subscript 10 is added for emphasis:

110012 = 2510

1 24 + 1 23 + 0 22 + 0 21 + 1 20

= 16 + 8 + 0 + 0 + 1 = 25

Positional Numbering Systems (3)

Page 7: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• A method of converting integers from decimal to some other radix/base uses division.

• This method is mechanical and easy.

• It employs the idea that successive division by a base is equivalent to successive subtraction by powers of the base.

• Let’s use the division remainder method to again convert 190 in decimal to base 3.

Converting Between Bases

Page 8: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• Converting 190 to base 3...

—First we take the number that we wish to convert and divide it by the radix in which we want to express our result.

—In this case, 3 divides 190 63 times, with a remainder of 1.

—Record the quotient and the remainder.

Converting Between Bases

Page 9: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• Converting 190 to base 3...

—63 is evenly divisible by 3.

—Our remainder is zero, and the quotient is 21.

Converting Between Bases

Page 10: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• Converting 190 to base 3...

—Continue in this way until the quotient is zero.

—In the final calculation, we note that 3 divides 2 zero times with a remainder of 2.

—Our result, reading from bottom to top is:

19010 = 210013

Converting Between Bases

Page 11: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Converting algorithm

• To convert between Base 10 and Base 2 (or any base for that matter)

• value = <number to be converted>• loop until value is 0

digit = value mod basevalue = value / base (integer

division) end loop• Read digits in reverse order

Page 12: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Examples

• to convert 8 to binary• 0 = 8 mod 2, v=8/2 =

4• 0 = 4 mod 2, v=4/2 =

2• 0 = 2 mod 2, v=2/2 =

1• 1 = 1 mod 2, v=1/2 =

0• 1000 = 8To get to 8 bits, pack

with zeros• 00001000 = 8

• 12 to binary• 0=12 mod 2,

v=12/2=6• 0=6 mod 2, v=6/2=3• 1=3 mod 2, v=3/2=1• 1=1 mod 2, v=1/2=0• 1100 = 12

• 00001100 = 12

Page 13: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Binary to Base 10

• value = 0• loop I=0 to length(string –1)

digit = string[I]value += digit*base^I

end loop

Page 14: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Example

• 1000 to Base 10• v = 0• v += 0* 2^0=0• v += 0* 2^1=0• v += 0* 2^2=0• v += 1* 2^3=8• 1000 = 8

• 1100 to Base 10• v = 0• v += 0* 2^0=0• v += 0* 2^1=0• v += 1* 2^2=4• v += 1* 2^3=12• 1100 = 12

Page 15: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Signed Integer Representation

• The conversions we have so far presented have involved only unsigned numbers.

• To represent signed integers, computer systems allocate the high-order bit to indicate the sign of a number.—The high-order bit is the leftmost bit. It is

also called the most significant bit.— 0 is used to indicate a positive number; 1

indicates a negative number.

• The remaining bits contain the value of the number (but this can be interpreted different ways)

Page 16: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• There are three ways in which signed binary integers may be expressed: —Signed magnitude

—One’s complement

—Two’s complement

• In an 8-bit word, signed magnitude representation places the absolute value of the number in the 7 bits to the right of the sign bit.

Signed Integer Representation (2)

Page 17: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• For example, in 8-bit signed magnitude representation: +3 is: 00000011 - 3 is: 10000011

• Computers perform arithmetic operations on signed magnitude numbers in much the same way as humans carry out pencil and paper arithmetic.—Problems:

– Need to consider both sign and magnitude in arithmetic BAD!

– Two representations of zero (+0 and -0)+ REALLY REALLY BAD!

—The fix is to use two-complement.

Signed Integer Representation (3)

Page 18: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• To express a value in two’s complement representation:—If the number is positive, just convert it to binary

and you’re done.—If the number is negative, find the one’s

complement of the number and then add 1.• Example:

—In 8-bit binary, 3 is: 00000011

—-3 using one’s complement representation is: 11111100

—Adding 1 gives us -3 in two’s complement form: 11111101.

Two’s complement

Page 19: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Two’s Compliment

• +3 = 00000011• +2 = 00000010• +1 = 00000001• +0 = 00000000• -1 = 11111111• -2 = 11111110• -3 = 11111101

Page 20: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

How to: 2’s Complement• +2 = 0010• reverse numbers

1101• Then add 1 (binary)

1110• So 1110 = -2

• Can positive 8, be represented in 4 bits?

No, because –8 is 1000Called an Overflow!

• -7 = 10010110 (reversed)0111 (add 1)

+7 = 0111

Page 21: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Benefits

• One representation of zero• Arithmetic works easily• Negating is fairly easy

—3 = 00000011—Boolean complement gives 11111100—Add 1 to LSB 11111101

Page 22: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Negation Special Case 1

• 0 = 00000000• Bitwise not 11111111• Add 1 to LSB +1• Result 1 00000000• Overflow is ignored, so:• - 0 = 0

Page 23: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Negation Special Case 2

• -128 = 10000000• bitwise not 01111111• Add 1 to LSB +1• Result 10000000• So:• -128 = -128 X

Page 24: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Range of Numbers

• 8 bit 2s compliment—+127 = 01111111 = 27 -1— -128 = 10000000 = -27

• 16 bit 2s compliment—+32767 = 011111111 11111111 = 215 - 1— -32768 = 100000000 00000000 = -215

Page 25: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Conversion Between Lengths

• Positive number pack with leading zeros—+18 = 00010010—+18 = 00000000 00010010

• Negative numbers pack with leading ones—-110 = 10010010—-110 = 11111111 10010010

• i.e. pack with MSB (sign bit)

Page 26: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• Binary addition is as easy as it gets. You need to know only four rules:

0 + 0 = 0 0 + 1 = 11 + 0 = 1 1 + 1 = 10

• The simplicity of this system makes it possible for digital circuits to carry out arithmetic operations.—We will describe these circuits later on.

Binary Addition

Page 27: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Addition and Subtraction

• Normal binary addition• Monitor sign bit for overflow• Take twos compliment of subtrahend and add to

minuend—i.e. a - b = a + (-b)

• So we only need addition and complement circuits

Page 28: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• With two’s complement arithmetic, all we do is add our two binary numbers. Just discard any carry emitting from the high order bit.

We note that 19 in binary is: 00010011,

so -19 using one’s complement is: 11101100,and -19 using two’s complement is: 11101101.

– Example: Using one’s complement binary arithmetic, find the sum of 48 and - 19.

Two complement addition

carry

Page 29: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• When we use any finite number of bits to represent a number, we always run the risk of the result of our calculations becoming too large or too small to be stored in the computer.

• While we can’t always prevent overflow, we can always detect overflow.

• In complement arithmetic, an overflow condition is easy to detect.

Overflow

Page 30: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

• Example:—Using two’s complement binary

arithmetic, find the sum of 107 and 46.

• But here with have the erroneous result: 107 + 46 = -103.

• Overflow—When two numbers are added

and they have the same sign and the result has the opposite sign

– An overflow is not possible when adding to number of different signs.

Overflow detection

Page 31: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Addition and subtraction Examples

• (-7) + (+5) = -2 11001 = -7+00101 = +5 11110 = -2

• (+12) + (+7) = 19 01100 = +12+00111 = +7 10011 = -13 Overflow!!

• 2 + (-7) = -5 00010 = 2+11001 = -7 11011 = -5

• 7 + (-5) = 2 00111 = 7+11011 = +(-5) 100010 = 2 Note the carry!

Page 32: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Addition and subtraction Examples (2)

• 2 – (-3) =5 (2 + 3 = 5)

00010 = 2+00011 = 3 (-3

negated) 00101 = 5

• -9–8 =-17 (-9+(-8)=-17)

10111 = -9+11000 = -8 101111 = 15 Overflow and carry!

Page 33: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

Hardware for Addition and Subtraction

Page 34: Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.

QA&