26 ms·
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 multiplicati
by pbsd 12y ago
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.
- sounds 12y agoYes. I decided not to mention IMUL to keep the post simple. There are also issues with detecting overflow of SSE multiplies (signed and unsigned).