mul and memory allocation in registers edx::eax with masm

冷暖自知 提交于 2019-12-02 07:08:33

Yes, as you said it depends on operand size which is not the actual value of the operand. You always get double size output, overflow or not. mul ebx is 32 bit, hence you get 64 bit output in edx:eax where edx may be zero.

As Peter Cordes pointed out, if you know you won't need the top half of the result, "you can and should use the 2-operand or 3-operand immediate form of IMUL". Don't be fooled by the signedness, to quote the instruction set reference: "The two- and three-operand forms may also be used with unsigned operands because the lower half of the product is the same regardless if the operands are signed or unsigned."

Martin Bonner

The MUL and IMUL instructions always generate output that is twice the width of the inputs. So:

  • 8 bit inputs (one in AL) give a 16 bit output (in AX)
  • 16 bit inputs (one in AX) give a 32 bit output (in DX:AX)
  • 32 bit inputs (one in EAX) give a 64 bit output (in EDX:EAX)

Note that the relevant width is always the width of the argument (and the implicit A register), not the minimum width to hold either of the values. A 32-bit multiply by zero will (slowly) set EDX:EAX to all zeros.

The unsigned multiply (MUL instruction) will always sign-extend (well, zero-extend, since it's unsigned) into EDX when it is passed an r/m32-sized operand, regardless of whether or not EDX would contain anything but zeroes.

This applies to the signed variant, IMUL, as well, though in that case EDX might contain nothing but FF bytes if the result was negative.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!