Assembly x86 division with DX:AX

2019-02-28 15:20发布

问题:

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 of C*D fits in 16 bits so we can proceed as normal using ax as the 16-bit divisor.
  • DX > 0 (DX != 0): C*D is greater than 65335 (0xFFFF) and the 16-bit unsigned division of A and that number will always be 0 and the remainder is simply A.

Or you could do as C and just assume that the result of C*D fits in 16 bit. :)