1 / 54

Chapter 8 Computer arithmetics

CS147 Lecture 15. Chapter 8 Computer arithmetics. Prof. Sin-Min Lee Department of Computer Science San Jose State University. The two complementary representations are called "2's complement" and "1's complement". and the 2's complement is simply the 1's complement plus 1!.

Télécharger la présentation

Chapter 8 Computer arithmetics

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. CS147 Lecture 15 Chapter 8 Computer arithmetics Prof. Sin-Min Lee Department of Computer Science San Jose State University

  2. The two complementary representations are called "2's complement" and "1's complement". and the 2's complement is simply the 1's complement plus 1!

  3. Interpret 11011011 as a twos compliment binary number, and give its decimal equivalent. • First, note that the number is negative, since it has a 1 in the sign bit place. • Change the sign to get the magnitude of the number. 1 • Convert the magnitude to decimal: 001001012 = 2516 = 2 × 16 + 5 = 3710. • Since the original number was negative, the result is -37.

  4. Addition And Subtraction • For both the non-negative and two’s complement notations, addition and subtraction are fairly straightforward. • However, when the result cannot be represented as an 8-bit value, a problem arises. For the non-negative notation, consider the addition 255+1, 1111 1111 + 0000 0001. Straight binary addition yields the result 1 0000 0000, a 9-bit value, which cannot be stored in an 8-bit register.

  5. Overflow • Arithmetic overflow: The extra bit generates a carry out from the parallel adder. In non-negative notation, this carry bit can set an overflow flag, signaling the rest of the system that an overflow has occurred, and that the result generated is not entirely correct. The rest of the system can either fix the result of handle the error appropriately.

  6. Overflow • In two’s complement notation an overflow can occur at either end of the numeric range. At the positive end, adding 127+1, 0111 1111 + 0000 0001, yields a result of 1 0000 0000. However, in two’s complement notation, this is -128, not the desired value of +128. In this notation, the key to recognizing overflow is to check not only the carry out, but also the carry in to the most significant bit of the result. If two carries are equal, then there is no overflow.

  7. Overflow • Overflow only occurs when two numbers with the same sign are added. Adding two numbers with different signs always produces a valid result.

  8. Overflow generation in unsigned two’s complement addition 126 01111110 +1 00000001 01111111 00 127 01111111 +1 00000001 10000000 01 -127 10000001 +(-1) 11111111 10000000 11 -128 10000000 +(-1) 11111111 01111111 10

  9. Whether there is an overflow or not depends on the interpretation (signed or unsigned number).

  10. each partial product is the product of one bit of the multiplier times the multiplicand. There are as many partial products as there are bits in the multiplier, and there are as many bits in each partial product as there are bits in the multiplicand.

  11. In looking for an algorithmic statement of this approach to binary multiplication, it was found that a group of Russian peasants used precisely this method to multiply decimal numbers, and as a result, the binary multiplication algorithm given here is commonly known as the Russian Peasant Algorithm:

  12. The best of these fast multiplication algorithms was developed by HP for the HP PA RISC architecture. In developing the first generation of this architecture, the HP designers concluded that a hardware multiply instruction was not justified, because most multiplies are multiplies by constants and can be replaced by add-and-shift instructions (as on the Hawk) and because the rare multiply of one variable by another could be done quickly enough in software.

  13. The HP PA RISC multiply algorithm is based on the following notions: • First, do the multiply in base 16, so each step involves multiplying the multiplicand by the least significant 4 bits of the multiplier and then adding the partial product to the result. • To multiply the multiplicand by one hex digit of the multiplier, use an efficient case/select control structure to select one of 16 different blocks of code for multiplying by a constant. Each of these blocks will be only a few instructions long because of the use of add-and-shift instructions. • To avoid the cost of loop control instructions, note that the fixed word size implies that each multiply can be done in a fixed number of iterations (4 hex digits in a 16 bit multiplier, or 8 hex digits in a 32 bit multiplier). So, just write out the body of the loop 4 or 8 times in series and eliminate any need for loop counters.

  14. What are BCD Numbers? • Binary Coded Decimal numbers are actually binary numbers that are "coded" to represent decimal numbers • Coded numbers are "not real numbering systems". In fact, they are just what they say they are, "codes that represent actual numbers". Although the actual numbers can be mathematically manipulated, codes follow no such rules.

  15. Example 1BCD (365)_10 -------------> 0011 | 0110 | 0101 Example 2.

  16. Addition • Addition is analogous to decimal addition with normal binary addition taking place from right to left. For example,

  17. Where the result of any addition exceeds 9(1001) then six (0110) must be added to the sum to account for the six invalid BCD codes that are available with a 4-bit number. This is illustrated in the example below

  18. When one adds two BCD digits, • if the binary sum is less than 1010, the corresponding BCD sum digit is correct. • if the binary sum is greater than or equal to 1010, add (0110)2 to the corresponding BCD sum digit and produce a carry.

More Related