1 / 30

CSCI206 - Computer Organization & Programming

CSCI206 - Computer Organization & Programming. Integer Addition and Subtraction. Revised by Alexander Fuchsberger and Xiannong Meng in spring 2019 based on the notes by other instructors. zyBook: 10.1, 10.2. Review Positional Number Systems. n-th digit. Base. Binary to Decimal (b=2).

cliffordr
Télécharger la présentation

CSCI206 - Computer Organization & Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI206 - Computer Organization & Programming Integer Addition and Subtraction Revised by Alexander Fuchsberger and Xiannong Meng in spring 2019 based on the notes by other instructors. 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., 42 - 32 == 10 • Repeat for remaining digits • insert zero at kth bit if 2^k 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 = 101010 5 4 3 2 1 0 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 + 1110 1

  11. Binary Addition 101101 + 1110 11

  12. Binary Addition 1 101101 + 1110 011

  13. Binary Addition 1 101101 + 1110 011

  14. Binary Addition 1 101101 + 1110 011

  15. Binary Addition 1 1 101101 + 1110 1011

  16. Binary Addition 1 1 101101 + 1110 11011

  17. Binary Addition 1 1 101101 + 1110 111011

  18. Binary Addition 101101 = 45 + 1110 = 14 111011 = 59

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

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

  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 0 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 : 1000 0000 // -128 INT_MAX: 0111 1111 // +127 If unsigned, 1000 0000 == 128 If signed, 2’s comp: 0111 1111 + 1 = 1000 0000 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 • 1000000...0 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!)

More Related