SB followed by NEG is problematic as SB is 8-bit and NEG is 16-bit
From Tursi : "The problem is the NEG in the second half there. Because the least significant byte is not necessarily initialized, the effect on the most significant byte is going to be just an INV most of the time. I noticed it when my values were equal, but it seems it would be off-by-one in half the cases.
NEG is described as getting the two's complement by inverting, and then adding one. So if we walk through my particular case, imagine R1 and R2 have previous garbage 'XXXX'. a and b are loaded, so we have "aaXX" and "bbXX". Assuming a and b are equal, we jump into the second case there. After the subtract, we have "00XX".
Now the NEG runs to fix the sign, NEG of >0000 is zero (inverts to >FFFF, add one back to >0000), but because the least significant byte is unknown, that only works if we entered with it cleared. In this case "00XX" becomes "FFYY", add one and it's "FFYZ". The MSB has not been changed at all, so we return >FF instead of >00."
SB followed by NEG is problematic as SB is 8-bit and NEG is 16-bit
From Tursi : "The problem is the NEG in the second half there. Because the least significant byte is not necessarily initialized, the effect on the most significant byte is going to be just an INV most of the time. I noticed it when my values were equal, but it seems it would be off-by-one in half the cases.
NEG is described as getting the two's complement by inverting, and then adding one. So if we walk through my particular case, imagine R1 and R2 have previous garbage 'XXXX'. a and b are loaded, so we have "aaXX" and "bbXX". Assuming a and b are equal, we jump into the second case there. After the subtract, we have "00XX".
Now the NEG runs to fix the sign, NEG of >0000 is zero (inverts to >FFFF, add one back to >0000), but because the least significant byte is unknown, that only works if we entered with it cleared. In this case "00XX" becomes "FFYY", add one and it's "FFYZ". The MSB has not been changed at all, so we return >FF instead of >00."