mburkley / tms9900-gcc

gcc cross-compiler for TMS9900
GNU General Public License v3.0
13 stars 1 forks source link

Byte subtract can give wrong result #37

Closed mburkley closed 9 months ago

mburkley commented 10 months ago

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."

mburkley commented 9 months ago

Removed the constraints that allow operands to be swapped