On Sep 30, 14:41, Derek Peschel wrote:
On Sun, Sep 30, 2001 at 01:38:13PM +0000, Pete
> confusing, try comparing the carry flags
implemented in a Z80 and a
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.
Yes, I found that confusing when I got my first 6502 machine (I had a Z80
> The other problem you have is with the overflow.
It's not a problem
> signed vs unsigned numbers as some people have
implied, it's with the
Well, as one of those people I may as well ask you how much of my post is
corect and how much is junk.
I don't think much was junk :-) You just didn't quite solve the puzzle.
You suggested the flags might change if you change the system (from signed
to unsigned, I think you meant), and they don't. In general, a processor
doesn't know whether you're thinking of signed or unsigned numbers when you
write the code. However, the meanings may change. Normally you don't pay
much attention to the carry for signed arithmetic, it's the overflow that
tells you useful things like whether the sign bit is correct or the answer
is meaningful. You still use the carry for multi-byte arithmetic, of
course, but it's automagically correct and you don't have to think about it
(other than a preliminary SEC or CCF or whatever). On the other hand, you
don't normally have any interest at all in the overflow for unsigned
arithmetic, though it's still there; it just doesn't mean anything useful.
You also suggested the flags might change between addition and subtraction,
and in some processors, yes they do. Some processors complement the carry
flag after a subtraction (ones that don't want a SEC before a subtraction,
for example :-))
The example you chose was perhaps what confused you (1--2= 3 1+-2=-1);
the problem being that -2 is its own 2's-complement in a 2-bit signed
system (0 is the other one that causes trouble, this time with the carry).
You'd get the same problem with -8 in a 4-bit system, or -128 in an 8-bit
Pete Peter Turnbull
University of York