[ Pobierz całość w formacie PDF ]
.We will briefly cover operations on 1 s comple-ment and BCD numbers, which have a foundational significance for other areasof computing, such as networking (for 1 s complement addition) and hand-heldcalculators (for BCD arithmetic.)66 CHAPTER 3 ARITHMETIC3.2.1 TWO S COMPLEMENT ADDITION AND SUBTRACTIONIn this section, we look at the addition of signed two s complement numbers.Aswe explore the addition of signed numbers, we also implicitly cover subtraction aswell, as a result of the arithmetic principle:a - b = a + (- b).We can negate a number by complementing it (and adding 1, for two s comple-ment), and so we can perform subtraction by complementing and adding.Thisresults in a savings of hardware because it avoids the need for a hardware subtrac-tor.We will cover this topic in more detail later.We will need to modify the interpretation that we place on the results of additionwhen we add two s complement numbers.To see why this is the case, considerFigure 3-1.With addition on the real number line, numbers can be as large or asSubtractingnumbers000-1 1111 001-2 110 010 2101 0113-3Adding100-4numbersFigure 3-1 Number circle for 3-bit two s complement numbers.small as desired the number line goes to ±" , so the real number line canaccommodate numbers of any size.On the other hand, as discussed in Chapter2, computers represent data using a finite number of bits, and as a result can onlystore numbers within a certain range.For example, an examination of Table 2.1shows that if we restrict the size of a number to, for example, 3 bits, there willonly be eight possible two s complement values that the number can assume.InFigure 3-1 these values are arranged in a circle beginning with 000 and proceed-ing around the circle to 111 and then back to 000.The figure also shows the dec-imal equivalents of these same numbers.Some experimentation with the number circle shows that numbers can be addedor subtracted by traversing the number circle clockwise for addition and counter-CHAPTER 3 ARITHMETIC 67clockwise for subtraction.Numbers can also be subtracted by two s complement-ing the subtrahend and adding.Notice that overflow can only occur for additionwhen the operands ( addend and augend ) are of the same sign.Furthermore,overflow occurs if a transition is made from +3 to - 4 while proceeding aroundthe number circle when adding, or from - 4 to +3 while subtracting.(Two s com-plement overflow is discussed in more detail later in the chapter.)Here are two examples of 8-bit two s complement addition, first using two posi-tive numbers:0 0 0 0 1 0 1 0 (+10)10+ 0 0 0 1 0 1 1 1 (+23)10 0 0 1 0 0 0 0 1 (+33)10A positive and a negative number can be added in a similar manner:0 0 0 0 0 1 0 1 (+5)10+ 1 1 1 1 1 1 1 0 (- 2)10___________Discard carry ’! (1) 0 0 0 0 0 0 1 1 (+3)10The carry produced by addition at the highest (leftmost) bit position is discardedin two s complement addition.A similar situation arises with a carry out of thehighest bit position when adding two negative numbers:1 1 1 1 1 1 1 1 (- 1)10+ 1 1 1 1 1 1 0 0 (- 4)10 Discard carry ’! (1) 1 1 1 1 1 0 1 1 (- 5)10The carry out of the leftmost bit is discarded because the number system is mod-ular it wraps around from the largest positive number to the largest negativenumber as Figure 3-1 shows.Although an addition operation may have a (discarded) carry-out from the MSB,this does not mean that the result is erroneous.The two examples above yield68 CHAPTER 3 ARITHMETICcorrect results in spite of the fact that there is a carry-out of the MSB.The nextsection discusses overflow in two s complement addition in more detail.OverflowWhen two numbers are added that have large magnitudes and the same sign, anoverflow will occur if the result is too large to fit in the number of bits used inthe representation.Consider adding (+80)10 and (+50)10 using an eight bit for-mat.The result should be (+130)10, however, as shown below, the result is(- 126)10:+ 0 1 0 1 0 0 0 0 (+80)10+ 0 0 1 1 0 0 1 0 (+50)10 + 1 0 0 0 0 0 1 0 (- 126)10This should come as no surprise, since we know that the largest positive 8-bittwo s complement number is +(127)10, and it is therefore impossible to represent(+130)10.Although the result 100000102 looks like 13010 if we think of it inunsigned form, the sign bit indicates a negative number in the signed form,which is clearly wrong.In general, if two numbers of opposite signs are added, then an overflow cannotoccur.Intuitively, this is because the magnitude of the result can be no largerthan the magnitude of the larger operand.This leads us to the definition of two scomplement overflow:If the numbers being added are of the same sign and the result is of theopposite sign, then an overflow occurs and the result is incorrect.If thenumbers being added are of opposite signs, then an overflow will neveroccur.As an alternative method of detecting overflow for addition, anoverflow occurs if and only if the carry into the sign bit differs from thecarry out of the sign bit [ Pobierz caÅ‚ość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl przylepto3.keep.pl
.We will briefly cover operations on 1 s comple-ment and BCD numbers, which have a foundational significance for other areasof computing, such as networking (for 1 s complement addition) and hand-heldcalculators (for BCD arithmetic.)66 CHAPTER 3 ARITHMETIC3.2.1 TWO S COMPLEMENT ADDITION AND SUBTRACTIONIn this section, we look at the addition of signed two s complement numbers.Aswe explore the addition of signed numbers, we also implicitly cover subtraction aswell, as a result of the arithmetic principle:a - b = a + (- b).We can negate a number by complementing it (and adding 1, for two s comple-ment), and so we can perform subtraction by complementing and adding.Thisresults in a savings of hardware because it avoids the need for a hardware subtrac-tor.We will cover this topic in more detail later.We will need to modify the interpretation that we place on the results of additionwhen we add two s complement numbers.To see why this is the case, considerFigure 3-1.With addition on the real number line, numbers can be as large or asSubtractingnumbers000-1 1111 001-2 110 010 2101 0113-3Adding100-4numbersFigure 3-1 Number circle for 3-bit two s complement numbers.small as desired the number line goes to ±" , so the real number line canaccommodate numbers of any size.On the other hand, as discussed in Chapter2, computers represent data using a finite number of bits, and as a result can onlystore numbers within a certain range.For example, an examination of Table 2.1shows that if we restrict the size of a number to, for example, 3 bits, there willonly be eight possible two s complement values that the number can assume.InFigure 3-1 these values are arranged in a circle beginning with 000 and proceed-ing around the circle to 111 and then back to 000.The figure also shows the dec-imal equivalents of these same numbers.Some experimentation with the number circle shows that numbers can be addedor subtracted by traversing the number circle clockwise for addition and counter-CHAPTER 3 ARITHMETIC 67clockwise for subtraction.Numbers can also be subtracted by two s complement-ing the subtrahend and adding.Notice that overflow can only occur for additionwhen the operands ( addend and augend ) are of the same sign.Furthermore,overflow occurs if a transition is made from +3 to - 4 while proceeding aroundthe number circle when adding, or from - 4 to +3 while subtracting.(Two s com-plement overflow is discussed in more detail later in the chapter.)Here are two examples of 8-bit two s complement addition, first using two posi-tive numbers:0 0 0 0 1 0 1 0 (+10)10+ 0 0 0 1 0 1 1 1 (+23)10 0 0 1 0 0 0 0 1 (+33)10A positive and a negative number can be added in a similar manner:0 0 0 0 0 1 0 1 (+5)10+ 1 1 1 1 1 1 1 0 (- 2)10___________Discard carry ’! (1) 0 0 0 0 0 0 1 1 (+3)10The carry produced by addition at the highest (leftmost) bit position is discardedin two s complement addition.A similar situation arises with a carry out of thehighest bit position when adding two negative numbers:1 1 1 1 1 1 1 1 (- 1)10+ 1 1 1 1 1 1 0 0 (- 4)10 Discard carry ’! (1) 1 1 1 1 1 0 1 1 (- 5)10The carry out of the leftmost bit is discarded because the number system is mod-ular it wraps around from the largest positive number to the largest negativenumber as Figure 3-1 shows.Although an addition operation may have a (discarded) carry-out from the MSB,this does not mean that the result is erroneous.The two examples above yield68 CHAPTER 3 ARITHMETICcorrect results in spite of the fact that there is a carry-out of the MSB.The nextsection discusses overflow in two s complement addition in more detail.OverflowWhen two numbers are added that have large magnitudes and the same sign, anoverflow will occur if the result is too large to fit in the number of bits used inthe representation.Consider adding (+80)10 and (+50)10 using an eight bit for-mat.The result should be (+130)10, however, as shown below, the result is(- 126)10:+ 0 1 0 1 0 0 0 0 (+80)10+ 0 0 1 1 0 0 1 0 (+50)10 + 1 0 0 0 0 0 1 0 (- 126)10This should come as no surprise, since we know that the largest positive 8-bittwo s complement number is +(127)10, and it is therefore impossible to represent(+130)10.Although the result 100000102 looks like 13010 if we think of it inunsigned form, the sign bit indicates a negative number in the signed form,which is clearly wrong.In general, if two numbers of opposite signs are added, then an overflow cannotoccur.Intuitively, this is because the magnitude of the result can be no largerthan the magnitude of the larger operand.This leads us to the definition of two scomplement overflow:If the numbers being added are of the same sign and the result is of theopposite sign, then an overflow occurs and the result is incorrect.If thenumbers being added are of opposite signs, then an overflow will neveroccur.As an alternative method of detecting overflow for addition, anoverflow occurs if and only if the carry into the sign bit differs from thecarry out of the sign bit [ Pobierz caÅ‚ość w formacie PDF ]