Re:We Need Hardware Traps for Integer Overflow
by pbsd on Wednesday June 04, 2014 @06:18PM
(#7848771)
The correct thing to do is also to check the carry and overflow flags after (I)MUL, which does the right thing even when only the lower bits of the multiplication are saved.
Here's an example where there is overflow, but your suggestion would not catch it:
mov eax, 0x7fffffff
mov ecx, 2
imul ecx ; edx:eax = eax * ecx
; edx = 0
; eax = 0xfffffffe
; overflow flag = 1
There is signed overflow here, but testing edx (the higher word of the result) would not catch it.
Re:We Need Hardware Traps for Integer Overflow
by sounds on Wednesday June 04, 2014 @06:41PM
(#7848891)
Yes. I decided not to mention IMUL to keep the post simple.
There are also issues with detecting overflow of SSE multiplies (signed and unsigned).
Re:We Need Hardware Traps for Integer Overflow
by stormbrew on Wednesday June 04, 2014 @06:10PM
(#7848728)
Yep, in assembly it's usually no question possible. That's why I specified portable C.
C has some interesting gaps for something that's long been described as a macro for assembly. A rotate operator would also be nice.
Checking for overflow is just a matter of testing if the high-order bits are zero.