1

根据 NEG 和 NEGU 定义,

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.

让我们假设$Z = s(-1)u(2^64 - 1)。然后第一个操作码将在 时将值 1 放入$X寄存器Y = 0中,而最近的操作码将给出相同的结果,因为u(-(2^64 - 1)) mod 2^64 = 1。我对么?NEG指令应该在什么时候引发溢出异常$Z = -2^63

4

1 回答 1

1

“当 $Z = -2^63 时 NEG 指令是否应该引发溢出异常?”的简短回答

yes, but you probably already suspected that.

从逻辑上讲,NEG $X,0,-2^63 应该给出 2^63,这超出了有符号正整数的范围,因此会溢出。但是如果你像我一样,你想要证明整数溢出确实发生了。这里是:

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
于 2013-07-16T04:48:32.987 回答