Integer overflow: Difference between revisions

Line 147:
INX
BVS ErrorHandler ;this branch will never be taken, since INX doesn't affect the overflow flag.</lang>
 
=={{header|68000 Assembly}}==
Overflow happens when certain arithmetic operations result in the most significant byte of the register crossing over from 0x7F to 0x80. (Which byte of the 32-bit register is treated as "most significant" depends on the data size of the last instruction. See the example below)
 
<lang 68000devpac>MOVE.W D0,#0000117F
ADD.W #1,D0 ;DOESN'T SET THE OVERFLOW FLAG, SINCE AT WORD LENGTH WE DIDN'T CROSS FROM 7FFF TO 8000
 
SUB.B #1,D0 ;WILL SET THE OVERFLOW FLAG SINCE AT BYTE LENGTH WE CROSSED FROM 80 TO 7F</lang>
 
Like the 6502, the 68000 doesn't care about overflow unless you tell it to. As with the majority of computer architectures, whether a value is "signed" or "unsigned" is not actually a property of the value itself, but of the comparators used to evaluate it. Otherwise even unsigned arithmetic would produce overflow errors! There are a few options for handling overflow errors:
* <code>TRAPV</code> will call an exception handler if the overflow flag is set, otherwise it will do nothing.
* <code>DBVS Dn</code> will loop a section of code until the overflow flag is set or the chosen data register is decremented to 0xFFFF, whichever occurs first.
* <code>BVS</code> branches if the overflow flag is set.
 
=={{header|Ada}}==
1,489

edits