6502 assembly's numbers can be signed or not signed, depending on if the seventh bit is set. If it is signed, it ranges from -128 to 127, and if not, it ranges from 0 to 255. My question is: what happens if I add a signed and an unsigned number together? How would I account for overflow? Would I default the result to overflow for the signed variant, or not? I am trying to implement the adc
instruction for an emulator and this was a nuance that I was thinking about.
1 Answers
Whether a number is signed or not is entirely the programmer's problem; the 6502 does not differentiate behaviour based on the programmer's intent.
That being the case, overflow is always calculated on the assumption that both numbers are signed.
The specific test for ADC
is: if the two input numbers have the same sign, and the result has a different sign, set overflow. Otherwise clear it.
Which is the same as asking: is the sign of the result incorrect? Adding numbers of different signs can never overflow due to the ranges involved, and adding numbers of the same sign should never produce one of a different sign.
So, a typical C-esque implementation might look like:
overflow = ~(a^operand) & (a^result) & 0x80;
As an aside, this is how overflow is calculated even in decimal mode, though an intermediate version of the result is used. That's not an attempt to provide useful information, it's just a side effect — overflow doesn't really mean anything in decimal arithmetic.