问题
I'm working with masm and I've encountered a scenario I do not readily understand how to solve, for example:
X = (A)/(C*D)
If I multiple C*D first, my value is DX:AX and to my knowledge, I cannot use that as a divisor. If I do division separately as A/C and A/D, I run the risk of losing precision (from the reminders, etc.). What's the best way to implement this?
回答1:
As you correctly note, you can't use a 32-bit number as the divisor in a 16-bit division, but since we're only interested in doing integer division that's not a problem.
There are two cases to consider (for unsigned division):
DX == 0
: The result ofC*D
fits in 16 bits so we can proceed as normal usingax
as the 16-bit divisor.DX > 0
(DX != 0
):C*D
is greater than 65335 (0xFFFF
) and the 16-bit unsigned division ofA
and that number will always be 0 and the remainder is simplyA
.
Or you could do as C and just assume that the result of C*D
fits in 16 bit. :)
来源:https://stackoverflow.com/questions/7970290/assembly-x86-division-with-dxax