Presentation is loading. Please wait.

Presentation is loading. Please wait.

Integer Operations Computer Organization and Assembly Language: Module 5.

Similar presentations


Presentation on theme: "Integer Operations Computer Organization and Assembly Language: Module 5."— Presentation transcript:

1 Integer Operations Computer Organization and Assembly Language: Module 5

2 Integer Operations u Addition  Unsigned  2’s complement, 1’s complement u Subtraction u Multiplication u Division

3 Unsigned Integer Addition u The algorithm for addition of unsigned integers in a positional representation works for any radix.  You learned this algorithm as a child  Given a pair of bit (digit) sequences X and Y, where X = x n x n-1...x 1 x 0 Y = y n y n-1...y 1 y 0  For i from 0 to n, add c i, x i and y i. If the sum is less than the radix assign the sum to s i and 0 to c i+1. Otherwise subtract the radix from the sum and assign the result to s i and 1 to c i+1. c i is the carry bit in the i th bit position.  Assume c 0 = 0.

4 Unsigned Binary Addition 011(3 10 ) +001(1 10 ) 100(4 10 ) c 0 = 0 1+1=10 c 1 =1 s0s0 Note: c i+1 =(x i +y i +c i )div 2 s i =(x i +y i +c i )mod 2

5 Unsigned Binary Addition 010010000111 +011000+011001 101010100000

6 Overflow in Unsigned Addition u In a computer, the result of addition is stored in a fixed sized container: a register.  MIPS registers contain 32 bits  Sometimes the result of addition is a number too large to be represented: this is called overflow.  In unsigned addition, an overflow is indicated by a value of 1 in the most significant carry bit (often called the carry-out bit).

7 Overflow Example u Assume 4-bit registers 11110 1111 + 0001 1 10000 Carry out of 1 from the most significant position Although the true result is 10000, the register contains 0000.

8 Biased Addition u The various biased representations use the same addition algorithm as unsigned integers, with one modification  The bias must be subtracted from the result

9 Complement Addition u The 2’s complement representation uses the same addition algorithm as unsigned integers.  The overflow condition is different  Overflow occurs if x n-1 and y n-1 both have the same value and s n-1 has a different value.  This is logically equivalent to c n-1 = c n-2  Carry-out is otherwise ignored u The 1’s complement representation adds a slight wrinkle to the addition algorithm.  If the carry-out is 1, the sum is incremented by 1.  Overflow is handled as it is for 2’s complement

10 2’s Complement Addition 0101 (5)0011 (3) +0010 (2) +1100 (-4) 0111 (7)1111 (-1) 0101 (5)1001 (-7) +1101 (-3) +0111 (7) 10010 (2) 10000 (0) Carry out is discarded, no overflow.

11 2’s Complement Addition 1111 1000 (-8)0000 0101 (5) 1111 1000 (-8)0100 0000 (64) 11111 0000 (-16)0100 0101 (69) 0111 1110 (126)1000 0010 (-126) 0110 0000 (96)1111 1101 (-3) 1101 1110 (-34) 10111 1111 (127) overflow

12 0011 (+3)0001 (+1) + 1100 (-3) +1001 (-6) 1111 (0)1010 (-5) 1101 (-2) 0111 (+7) + 1011 (-4) + 1100 (-3) 1100010011 + 1 + 1 1001 (-6) 0100 (+4) 1’s Complement Addition carry-out is added to the partial sum

13 Integer Subtraction u The complement representations can handle subtraction as addition by first complementing the subtrahend, then adding.  There is no subtraction algorithm for complement representations. u All other integer representations require separate logic to handle subtraction.  For this reason, and because it has a unique zero, 2’s complement is used to represent integers in almost all architectures.  In such architectures, other representations are translated into 2’s complement before addition/subtraction, then the result is translated back into the original representation.

14 Integer Multiplication

15 u It may take as many as 2n bits to represent the product of two n -bit numbers. u Multiplication of unsigned binary integers can be done with the same algorithm we use for decimal multiplication. u By sign extending both the multiplier and the multiplicand to the size needed for the result, the algorithm for multiplying 2’s complement is the same as that of unsigned integers.

16 Multiplication Algorithm while (count < no. of integer bits){ check if the multiplier’s last bit is 1 if (multiplier bit = 1){ add the multiplicand to hi} count = count + 1 rotate the multiplier right 1 bit shift hi and lo one bit to the right }

17 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 0110 Multiplier Lo 0000 0 Using 4-bit registers, multiply 13 by 6.

18 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 0011 Multiplier Lo 0000 0 After the first shift the lo bit of the multiplier is 1, so we will add the multiplicand to hi.

19 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 0011 Multiplier Lo 11010000 0

20 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 1001 Multiplier Lo 01101000 0 After the shift the low bit of the multiplier is still 1. We add the multiplicand to hi…

21 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 1001 Multiplier Lo 00111000 1 …and the result is bigger than 4 bits. So carry is set to one. The carry bit is shifted into hi…

22 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 1100 Multiplier Lo 10011100 0 …and the low bit of hi is shifted into lo. One more shift step will produce the answer.

23 Unsigned Integer Multiplication 1101 Multiplicand Hi Carry 0110 Multiplier Lo 01001110 0 The multiplier is again 0110. The result, 1001110 is 78. And 6 x 13 = 78.

24 u With n-bit two’s complement integers, both the multiplier and the multiplicand are sign extended to 2n. u This produces a 4n-bit result. u The correct product is contained in the least significant 2n bits of the 4n-bit result. 2’s Complement Multiplication

25 2’s complement Multiplication 1101 Multiplicand Hi Carry 0110 Multiplier Lo 01001110 0 Interpreted as 2’s complement numbers, this result is –3 x 6 = 78, which is obviously false.

26 2’s complement Multiplication 11111101 Multiplicand Hi Carry 00000110 Multiplier Lo 00000000 0 For an accurate result we must sign extend the multiplier and multiplicand to 8 bits.

27 2’s complement Multiplication 11111101 Multiplicand Hi Carry 00000110 Multiplier Lo 0000010111101110 0 After multiplication, lo contains all the meaningful data: 11101110 = - 18, which is –3 x 6!

28 Division u Division of unsigned binary integers is the performed similar to the longhand division of decimal numbers u No convenient algorithm exists for division of signed magnitude and complement integers  Determine sign of the result  Take absolute value of the numbers  Do unsigned division  Convert the result to the original format

29 Division Algorithm remainder = dividend while count < no. of integer bits + 1{ remainder = remainder - divisor if (remainder < 0){ remainder = remainder + divisor shift left quotient & set lsb to 0 else shift left quotient & set lsb to 1 } shift the divisor right count = count + 1 }


Download ppt "Integer Operations Computer Organization and Assembly Language: Module 5."

Similar presentations


Ads by Google