1

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?

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
Minor Threat
  • 2,025
  • 1
  • 18
  • 32

1 Answers1

1

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
NickO
  • 731
  • 1
  • 6
  • 20
  • For further fun, you can run it with mmix -i program_name.mms to be able to inspect register contents at each step of the way – NickO Jul 16 '13 at 04:51
  • Incidentally, if you would like confirmation that the above code is the correct way to handle integer overflow, check out [this github post](https://github.com/ottocode/MMIX/blob/master/Nim/overflow.mms) – NickO Jul 16 '13 at 05:31