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

前端 未结 3 1770
北荒
北荒 2021-01-25 06:18

I am trying to understand the logic of using the \"mul\" operand in the assembler(i am using Visual Studio Community and MASM). Why, after \"mul ebx\" the result is changing als

相关标签:
3条回答
  • 2021-01-25 06:59

    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.

    0 讨论(0)
  • 2021-01-25 07:17

    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.

    0 讨论(0)
  • 2021-01-25 07:21

    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."

    0 讨论(0)
提交回复
热议问题