I am a beginner at verilog and encountered this problem:
Assume that you have two 8-bit 2's complement numbers,
a[7:0]
andb[7:0]
. These numbers are added to produces[7:0]
. Compute whether a (signed) overflow has occurred.
Now my answer to the asked value of overflow was
assign overflow = ~(s[7]&a[7]&b[7]);
While the correct answer shown was
assign overflow= (~s[7]&a[7]&b[7])|(s[7]&(~a[7])&(~b[7]));
I want to know where I went wrong.
Best Answer
Signed overflow occurs when the result of addition is too large for a given type to represent.
This occurs when either:
or
For each condition, we would then have:
(Positive integers input)
s[7] would be '1' for a[7] and b[7] both being '0' given by
(s[7]
[1 if result is negative]& (~a[7])
[result is 1 if input was positive]& (~b7))
[result 1 if input was positive]; the logical and would show that two positive inputs resulted in a negative output.(Negative integers input)
Overflow occurs if we have a positive result, so:
s[7] would be zero for a[7] and b[7] both being 1. This is given by
(~s[7]
[result is 1 if positive result]&a[7]
[would be 1 for negative input]&b[7])
[would be 1 for negative input. if all these bits evaluate 1, overflow has occurred.Your assignment captures the negative input case and not the positive input case where the quoted answer captures both.