Stupid question that's been annoying me

From: Pete Turnbull <pete_at_dunnington.u-net.com>
Date: Sun Sep 30 15:19:52 2001

On Sep 30, 12:06, Jeffrey S. Sharp wrote:
> On Sun, 30 Sep 2001, Pete Turnbull wrote:
>
> > you're assuming (on the left) that "carry" is the same as "borrow".
>
> This works itself out exactly like that below.
>
> > However, you'll get the right answer if you say that "A - B" is the
> > same as "A + inv(B) + 1" where "inv(B)" is the ones-complement of B.
>
> Bingo! That fixed it:
>
> * I get correct overflow flag values.
>
> * The correct borrow flag value is the opposite of the carry out of the
> addition.

Did you notice that in a few cases, your carry wasn't the opposite of the
borrow, in the original table? But it sorted itself out so that it was
consistent once you used "A + inv(B) + 1" on the right hand side.

> Now what's disturbing is that the same wrong ideas that I was taught were
> taught to an entire roomful of college students a few years ago, by a
> professor in the School of Electrical and Computer Engineering.

Maybe part of the confusion arises because many processors (including the
8080) complement the carry flag at the end of a subtraction, so that it can
be used directly as a "borrow" flag in multibyte subtractions. Others
(like the 6502) don't do that.

The confusion over the overflow usually arises because people are told that
adding the two's complement of a number is equivalent to subtraction.
 That's not so much "wrong" as "incomplete". As you've seen, it's
arithmetically but not logically equivalent. If you combine the
carry-into-MSB from the compliment op with the carry-into-MSB from the
subsequent addition, they would be logically equivalent as well, but you'd
also need to do this for the carry flag otherwise subtracting 0 tends to
give the wrong result :-)

Since you're thinking about 2's complements, and I've mentioned you could
combine the carry-in from the complement operation with the carry-in in the
addition, you might like to think about different ways of performing the
2's complement. The way usually explained is "invert the bits and add a
1". There's another common way: find the *rightmost* '1' in the number.
 Invert every bit to its left. Done. Now where's the carry in that? Oh,
and you need to use a particular search order to find the "rightmost 1".

> Thanks for the help.

You're welcome. It took me a few minutes to realise what was wrong, so it
was an interesting exercise :-)

-- 
Pete						Peter Turnbull
						Network Manager
						University of York
Received on Sun Sep 30 2001 - 15:19:52 BST

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:34:26 BST