Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI206 - Computer Organization & Programming

Similar presentations


Presentation on theme: "CSCI206 - Computer Organization & Programming"— Presentation transcript:

1 CSCI206 - Computer Organization & Programming
Integer Addition and Subtraction zyBook: 10.1, 10.2

2 Review Positional Number Systems
n-th digit Base

3 Binary to Decimal (b=2) 110011 543210 Digit count (n), or position

4 Decimal to Binary (b=2) (left to right)
42 = ?? First power of two <= N is the first 1, e.g., 2^5 which is 32 Subtract the value from the original, e.g., == 10 Repeat for remaining digits insert zero if greater than remaining value else insert 1 and subtract value

5 Decimal to Binary (b=2) (left to right)
insert 0’s in where the value is too small, e.g., 42-32 == 10, no 2^4 == 16 b/c 16 > 10 42 = first power of 2 <= 42 subtract, have 10 remaining next power of 2 <= 10, subtract 2 remain 2 remains, 2^1 is the last 1

6 Decimal to Binary (b=2) (right to left)
Let N be the base 10 number while N > 0 If N is odd (N%2==1) add a 1 else add a 0 N = N / 2 (integer divide)

7 Decimal to Binary (b=2) (right to left)
42 = even 0, 42/2 = 21R0 21 = odd 1, 21/2 = 10R1 10 = even 0, 10/2 = 5R0 5 = odd 1, 5/2 = 2R1 2 = even 0, 2/2 = 1R0 1 = odd 1, 1/2 = 0R1

8 Decimal to Binary (b=2) (right to left)
42 = even 0, 42/2 = 21R0 21 = odd 1, 21/2 = 10R1 10 = even 0, 10/2 = 5R0 5 = odd 1, 5/2 = 2R1 2 = even 0, 2/2 = 1R0 1 = odd 1, 1/2 = 0R1 Generate result right to left: 101010

9 Binary addition (zyBook 10.2)
same as elementary base-10 arithmetic 1 529 + 742 1271 Sum each place value from right to left and add carry bits if result requires an additional digit.

10 Binary Addition 101101 1 A B Sum Carry 1

11 Binary Addition 101101 11 A B Sum Carry 1

12 Binary Addition 1 101101 011 A B Sum Carry 1

13 Binary Addition 1 101101 011 A B Sum Carry 1

14 Binary Addition 1 101101 011 A B Sum Carry 1

15 Binary Addition 1 1 101101 1011 A B Sum Carry 1

16 Binary Addition 1 1 101101 11011 A B Sum Carry 1

17 Binary Addition 1 1 101101 111011 A B Sum Carry 1

18 Binary Addition = 45 = 14 = 59 A B Sum Carry 1

19 Ripple Carry Adder Add bits sequentially
Carry out goes into next carry in ALU Symbol

20 Ripple Carry Adder 1001 + 0101 1110 Simple to understand, but slow because the result has to be computed 1 bit at a time Faster, more complex, circuits exist!

21 Subtraction 2’s complement negation is done with bitwise inverse plus one: Easily implemented by computing the bitwise inverse and setting carry in to be 1 on bit 0, i.e., Bi = not(yi)

22 Multiplexer (Mux) By Lenehey at en.wikipedia Later version(s) were uploaded by Fresheneesz at en.wikipedia. (Transferred from en.wikipedia) [Public domain], from Wikimedia Commons

23 Adder/subtractor

24 Overflow Only possible when signs are the same of both operands.
Differing signs always produce a smaller absolute value. Same signs produce a larger absolute value. Overflow if positive + positive = negative or negative + negative = positive. carry out of MSB (most significant bit) is not a valid test for overflow!

25 Absolute Value if (x > 0) x else -x

26 Absolute Value if (x > 0) x else -x

27 Absolute Value 1

28 Absolute Value Overflow?
if result is negative only a problem with the C standard library implementation NAME abs, labs, llabs, imaxabs - compute the absolute value of an integer SYNOPSIS #include <stdlib.h> int abs(int j); Result of abs is a signed integer!

29 Consider the representations …
Assuming an 8-bit integer INT_MIN : // -128 INT_MAX: // +127 If unsigned, == 128 If signed, 2’s comp: = because it is signed, it is now -128

30 Absolute Value Overflow?
the invert plus 1 will overflow if the inverted value is all 1’s is the most negative 2’s complement value (INT_MIN) Detect if the number is negative and its absolute value is still negative! (INT_MIN!)


Download ppt "CSCI206 - Computer Organization & Programming"

Similar presentations


Ads by Google