Integer overflow: Difference between revisions

No edit summary
Line 134:
Signed overflow (crossing the 7F-80 boundary) is detected by the CPU setting the overflow flag.
Unsigned overflow from 255 to 0 is detected by setting the carry flag.
In either case, if the programmer expects either to occur, a branch can be taken depending on the status of the flag after a calculation where overflow could cause a problem. By default the CPU will continue with the wrong result, since the CPU has no concept of data types whatsoever. The CPU is capable of detecting overflow automatically, but it's up to the programmer to decide if an overflow actually matters.
 
<lang 6502asm>LDA #$7F
CLC
ADC #$01
BVS ErrorHandler ;this branch will always be taken.</lang>
 
 
Note that not all operations will set the flag. Only addition, subtraction, and bitwise OR will set it.
 
<lang 6502asm>LDX #$7F
INX
BVS ErrorHandler ;this branch will never be taken, since INX doesn't affect the overflow flag.</lang>
 
=={{header|Ada}}==
1,489

edits