signed addition overflow

Assume that you have two 8-bit 2's complement numbers, a[7:0] and b[7:0]. These numbers are added to produce s[7:0]. Also compute whether a (signed) overflow has occurred.

Exams/ece241 2014 q1c - HDLBits (01xz.net)

 1 module top_module (
 2     input [7:0] a,
 3     input [7:0] b,
 4     output [7:0] s,
 5     output overflow
 6 ); 
    
7   assign s = a + b; 8 assign overflow = (a[7]&b[7]&~s[7]) | ((~a[7])&(~b[7])&s[7]); 9 //没看懂这个 10 // assign s = ... 11 // assign overflow = ... 12 endmodule

 再写:

module top_module (
    input [7:0] a,
    input [7:0] b,
    output [7:0] s,
    output overflow
); 
    assign s = a + b;  
    assign overflow = ( a[7] && b[7] && ~s[7] ) || (~a[7] && ~b[7] && s[7]);

endmodule

有符号数溢出有两种情况:一是正正相加,产生正溢出;另一种情况是负负相减,产生负溢出。所以就分别考虑了这两种情况,将这两种情况取或判断溢出。

posted @ 2023-04-22 13:10  江左子固  阅读(29)  评论(0)    收藏  举报