According to NEG and NEGU definitions,
NEG $X,Y,$Z (negate signed): s($X) := Y - s($Z).
NEGU $X,Y,$Z (negate unsigned): u($X) := (Y - u($Z)) mod 2^64.
Let's suppose $Z = s(-1)
or u(2^64 - 1)
. Then the first opcode is going to put value 1 into the $X
register when Y = 0
, and the recent one will give the same result because u(-(2^64 - 1)) mod 2^64 = 1
. Am i correct? Should NEG
instruction raise overflow exception when $Z = -2^63
?
Short answer to "Should NEG instruction raise overflow exception when $Z = -2^63?"
yes, but you probably already suspected that.
Logically, NEG $X,0,-2^63 should give 2^63, which is out of bounds for signed positive integers, and thus overflows. But if you're like me, you want proof that an integer overflow actually occurs. Here it is:
t IS $255
LOC #20 //handle the integer overflow event
PUSHJ 255,Err
PUT rJ,$255
GET $255,rB
RESUME
LOC #100
Main SET t,#4000
PUT rA,t //set the integer overflow event bit
SETH $0,#8000
NEG $1,0,$0
GETA t,End
TRAP 0,Fputs,StdOut
TRAP 0,Halt,0
End BYTE "End of program",#a,0
Err SET $0,$255 //overflow subroutine, prints out message
GETA t,Emes
TRAP 0,Fputs,StdOut
GET t,rW
INCL t,4
PUT rW,t
SET $255,$0
POP 0,0
Emes BYTE "Error: integer overflow",#a,0