On Sun, Sep 30, 2001 at 01:38:13PM +0000, Pete Turnbull wrote:
On Sep 29, 23:32, Jeffrey S. Sharp wrote:
Yes, I'm actually going to ask a question
about two's complement
arithmetic! I feel quite stupid for mailing this, but I just can't
figure
it out. And believe it or not, it really does
have something to do with
classic computing. All right, here goes...
While I was in class Thursday [...] I created the following table:
[ snip ]
Note that the C and V flags behave differently in
each half. This is
very
disturbing, since I'm fairly certain that
subtraction is implemented in
many processors as adding a negated subtrahend (ergo, I should see no
difference in the flags). What have I done wrong in my table?
Firstly, you're assuming (on the left) that "carry" is the same as
"borrow". Wrong! If you are subtracting, working the columns
right-to-left, a borrow from one column to the previous (on the right) is
balanced by adding the borrowed digit back to the subtrahend, so it gets
subtracted from the result, not added. This is the opposite of a carry.
That's why most of the "carry" flags you show in the left side of the
table are incorrect; they're actually "borrow" flags. If you think this
is
confusing, try comparing the carry flags implemented in a Z80 and a 6502
(they do different things for subtractions!).
And (on the 6502) if you want to get a given result using addition vs.
subraction, the carry flag must be set differently. i.e., if you have
LDA #$FF LDA #$FF
ADC #$01 vs. SBC #$FF
and you want A to end up with $00, then you must put before the LDA:
CLC vs. SEC
I might never have known this except that I wanted to check my post
before posting it.
The other problem you have is with the overflow.
It's not a problem with
signed vs unsigned numbers as some people have implied, it's with the order
Well, as one of those people I may as well ask you how much of my post is
corect and how much is junk.
-- Derek