3 ms·
Is this the trick where you multiply by the two's-compliment inverse instead of dividing? That was the coolest thing I learned when I spent a few days coding as
by tehwalrus 10y ago
Is this the trick where you multiply by the two's-compliment inverse instead of dividing? That was the coolest thing I learned when I spent a few days coding assembly.
I can't see the shift that you normally need after pulling the result out of the overflow register though. My asm is a little rusty though.
- gpderetta 10y agoIIRC that only work if a%b==0 otherwise the general division trick is more complex.
- caf 10y agoYes, you are correct - multiplying by the integer inverse only works for dividends that are an integer multiple of the divisor.
- tomsmeding 10y agoIt doesn't need the shift because it retrieves the result of the first imul from edx, which contains the upper word of the result. The lower word is placed in eax, which this code completely ignores.
- tehwalrus 10y agoYes but even in the upper register you still need to lshift by one or two bits depending on the constant IIRC. Five had me shifting by (1 or 2), and seven by (2 or 1) I seem to recall. I can't remember what property of the number determines the size of the shift though.