Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc 2150: Computer Organization

Similar presentations


Presentation on theme: "Cosc 2150: Computer Organization"— Presentation transcript:

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

2 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.

3 Introduction (2) 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.

4 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).

5 Positional Numbering Systems (2)
The decimal number 947 in powers of 10 is: The decimal number in powers of 10 is: 9    100 5     100 + 4   10-2

6 Positional Numbering Systems (3)
The binary number 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: = 2510 1      20 = = 25

7 Converting Between Bases
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.

8 Converting Between Bases
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 times, with a remainder of 1. Record the quotient and the remainder.

9 Converting Between Bases
Converting 190 to base 3... 63 is evenly divisible by 3. Our remainder is zero, and the quotient is 21.

10 Converting Between Bases
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 =

11 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 base value = value / base (integer division) end loop Read digits in reverse order

12 Examples to convert 8 to binary 0 = 8 mod 2, v=8/2 = 4
1000 = 8 To get to 8 bits, pack with zeros = 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 = 12

13 Binary to Base 10 value = 0 loop I=0 to length(string –1) digit = string[I] value += digit*base^I end loop

14 Example 1000 to Base 10 v = 0 v += 0* 2^0=0 v += 0* 2^1=0
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

15 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)

16 Signed Integer Representation (2)
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.

17 Signed Integer Representation (3)
For example, in 8-bit signed magnitude representation: +3 is: - 3 is: 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.

18 Two’s complement 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: -3 using one’s complement representation is: Adding 1 gives us -3 in two’s complement form:

19 Two’s Compliment +3 = +2 = +1 = +0 = -1 = -2 = -3 =

20 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 1000 Called an Overflow! -7 = 1001 0110 (reversed) 0111 (add 1) +7 = 0111

21 One representation of zero Arithmetic works easily
Benefits One representation of zero Arithmetic works easily Negating is fairly easy 3 = Boolean complement gives Add 1 to LSB

22 Negation Special Case 1 0 = Bitwise not Add 1 to LSB Result Overflow is ignored, so: - 0 = 0 

23 Negation Special Case 2 -128 = bitwise not Add 1 to LSB Result So: -128 = X

24 Range of Numbers 8 bit 2s compliment 16 bit 2s compliment
+127 = = 27 -1 -128 = = -27 16 bit 2s compliment = = = = -215

25 Conversion Between Lengths
Positive number pack with leading zeros +18 = +18 = Negative numbers pack with leading ones -110 = -110 = i.e. pack with MSB (sign bit)

26 Binary Addition Binary addition is as easy as it gets. You need to know only four rules: 0 + 0 = = 1 1 + 0 = = 10 The simplicity of this system makes it possible for digital circuits to carry out arithmetic operations. We will describe these circuits later on.

27 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

28 Two complement addition
With two’s complement arithmetic, all we do is add our two binary numbers. Just discard any carry out emitting from the high order bit. carry out Example: Using one’s complement binary arithmetic, find the sum of 48 and - 19. We note that 19 in binary is: , so -19 using one’s complement is: , and -19 using two’s complement is:

29 Overflow 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.

30 Overflow detection Overflow Example:
Using two’s complement binary arithmetic, find the sum of 107 and 46. But here with have the erroneous result: = -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.

31 Addition and subtraction Examples
(-7) + (+5) = -2 11001 = -7 = +5 11110 = -2 (+12) + (+7) = 19 01100 = +12 = +7 10011 = -13 Overflow!! 2 + (-7) = -5 00010 = 2 = -7 11011 = -5 7 + (-5) = 2 00111 = 7 = +(-5) = 2 Note the carry out!

32 Addition and subtraction Examples (2)
2 – (-3) =5 (2 + 3 = 5) 00010 = 2 = 3 (-3 negated) 00101 = 5 -9–8 =-17 (-9+(-8)=-17) 10111 = -9 = -8 = 15 Overflow and carry out!

33 Hardware for Addition and Subtraction

34 Q A &


Download ppt "Cosc 2150: Computer Organization"

Similar presentations


Ads by Google