1 / 31

CS 325: CS Hardware and Software Organization and Architecture

CS 325: CS Hardware and Software Organization and Architecture. Integers and Arithmetic Part 2. Outline. Representing Signed Numbers Sign and Magnitude 1’s Complement 2’s Complement Signed vs. Unsigned Numbers Data Storage. Representing Signed Numbers.

ross-ross
Download Presentation

CS 325: CS Hardware and Software Organization and Architecture

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. CS 325: CS Hardware and SoftwareOrganization and Architecture Integers and Arithmetic Part 2

  2. Outline • Representing Signed Numbers • Sign and Magnitude • 1’s Complement • 2’s Complement • Signed vs. Unsigned Numbers • Data Storage

  3. Representing Signed Numbers • So far, examples of unsigned numbers • 3 ways for handling negative numbers • Sign and Magnitude • 1’s Complement • 2’s Complement

  4. Sign and Magnitude • Easiest solution: Define leftmost bit to be the sign bit. • 0  positive number, 1  negative number • Other bits represent numerical value of number • 32-bit example: 0000 0000 0000 0000 0000 0000 0000 0001  +110 1000 0000 0000 0000 0000 0000 0000 0001  -110

  5. Convert Decimal to Sign and Magnitude: • S&N requires two things: • Sign: 0  positive, 1  negative • Magnitude: value to be represented • 16-bit Example: +7410 0000 0000 0100 1010 • 16-bit Example: -7410 1000 0000 0100 1010

  6. Convert Decimal to Sign and Magnitude: • 8-bit Example: +2310, -2310 • +2310 0001 0111 • -2310 1001 0111

  7. Convert Sign and Magnitude to Decimal: • 8-bit Example: 1000 01112, -710 • Another Example: 1000 00002 0000 00002 Produces: -0 and +0 problems with this?

  8. Sign and Magnitude – Not a Good Idea • Arithmetic circuit more complex • Special steps depending on whether signs are the same or not • Also, two zeroes: 0x00000000 = +010 0x80000000 = -010 • How would this affect programming languages? • Sign and magnitude abandoned.

  9. Another try: Complement the bits • Example: 710 = 001112, -710 = 110002 • Called 1’s Complement • Note: Positive numbers have leading 0s, negative numbers have leading 1s. • How many positive numbers in N bits? • How many negative numbers in N bits?

  10. Another try: Complement the bits • 4-bit Example: • 1’s comp representation of a 4 bit number • 2n-1-1 positive numbers • 2n-1-1 negative numbers

  11. Converting Decimal to 1’s Comp • Algorithm for Converting Decimal to 1s comp: • Ignore the minus sign, and convert the Decimal value to N bits, unsigned. • If there is a minus sign, flip all bits of the N bit number from previous step. Otherwise, the previous step has the correct answer.

  12. Converting Decimal to 1’s Comp • Example 8-bit: -4810 • First, convert to Binary 8-bits: 0011 00002 • Next, compliment the bits: 1100 11112

  13. Converting Decimal to 1’s Comp • Try 16-bit: -23310 • First, convert to Bin: 0000 0000 1110 10012 • Next, since we’re dealing with a negative number, apply 1s comp: 1111 1111 0001 01102

  14. Converting 1’s Comp to Decimal • Algorithm for Converting1s comp to Decimal: • If the MSB is 1, compliment all bits, otherwise move to next step. • Convert the result to Base 10. This should be a non-negative value. • Add a negative sign if the MSB of the 1s comp representation was 1.

  15. Converting 1’s Comp to Decimal • Example 8-bit: 1101 11102 • MSB is 1 so compliment the bits: 0010 00012 • Next, convert to Base 10: 3310 • Since the MSB is 1, the Base 10 value is negative: -3310

  16. Converting 1’s Comp to Decimal • Try 16-bit: 1101 1111 0010 11002 • MSB is 1 so compliment the bits: 0010 0000 1101 00112 • Next, convert to Base 10: 840310 • Since the MSB is 1, the Base 10 value is negative: -840310

  17. 1’s Complement Shortcomings • Arithmetic not too difficult. • But still two zeroes • 1’s complement eventually abandoned since another solution is better.

  18. Still Searching for Negative Number Representation • Sign and Magnitude didn’t work, 1’s Complement didn’t work..find another way. • Keep representation of leading 0s pos, leading 1s  neg. 000000…xxx is >= 0, 111111…xxx < 0

  19. 2’s Complement Number Line • 2N-1-1 PosNums • 2N-1NegNums • 1 zero • Must specify width • 8-bits, 16-bits, 32-bits • Overflow? • More on this later

  20. 2’s Complement 24 bits

  21. Converting Decimal to 2’s Comp • Algorithm for Converting Decimal to 2s comp: • If the number is positive, convert to N-bit unsigned Binary. Finished. • Otherwise, if the number, X, is negative, convert the ABS(X) to unsigned Binary. • Perform 1s comp on the unsigned Binary number. • Add 1 to this number to complete 2s comp.

  22. Converting Decimal to 2’s Comp • Example 8-bit: -2710 • First, convert to Binary 8-bits: 0001 10112 • Next, 1s comp: 1110 01002 • Add 1 to 1s comp value: 1110 01002 +0000 00012 1110 01012

  23. Converting Decimal to 2’s Comp • Try 16-bit: -19410 • First, convert to Binary 16-bits: 0000 0000 1100 00102 • Next, 1s comp: 1111 1111 0011 11012 • Add 1 to 1s comp value: 1111 1111 0011 11012 +0000 0000 0000 00012 1111 1111 0011 11102

  24. Converting from 2s Comp to Decimal • Algorithm for Converting 2s Comp to Decimal: • If the MSB is 1, perform 1s comp and add 1. If the MSB is 0, go to next step. • Convert the result to Base 10. This value should be positive. • If the MSB was 1, add the negative sign to the Base 10 number.

  25. Converting 2’s Comp to Decimal • Example 8-bit: 1010 10102 • MSB is 1 so compliment the bits: 0101 01012 • And add 1: 0101 01012 +0000 00012 0101 01102 • Since the MSB is 1, the Base 10 value is negative: -8610

  26. Converting 2’s Comp to Decimal • Try 16-bit: 1100 0010 1101 11102 • MSB is 1 so compliment the bits: 0011 1101 0010 00012 • And add 1: 0011 1101 0010 00012 +0000 0000 0000 00012 0011 1101 0010 00102 • Since the MSB is 1, the Base 10 value is negative: -15,65010

  27. 2’s Complement 24 bits

  28. 2’s Comp Overflow • Overflow is caused when N-bit arithmetic results will not fit within N-bits. • Example: 8-bit 2s comp 104 + 45 = 149 149 > 28 The result of this addition produces an overflow. In 8-bit 2s comp, this will result in a negative number (1001 01012)

  29. 2’s Comp Overflow • Rules for detecting 2s Comp overflow: • If the sum of two Pos numbers yields a Neg result, the sum has overflowed. • If the sum of two Neg numbers yields a Pos result, the sum has overflowed. • Otherwise, the sum has not overflowed.

  30. 2’s Comp Overflow • Rules for detecting 2s Comp overflow: • If the sum of two Pos numbers yields a Neg result, the sum has overflowed. • If the sum of two Neg numbers yields a Pos result, the sum has overflowed. • Otherwise, the sum has not overflowed.

  31. 2’s Comp Overflow Examples • -39 + 92 = 53 1 1 1 1 1 1 0 1 1 0 1 1 +0 1 0 1 1 1 0 0 0 0 1 1 0 1 0 1 Carryout without overflow. Sum is correct

More Related