imul 汇编指令 - 一个操作数?
我正在使用运行时调试器。
EAX:0000 0023 EDX:5555 5556
imul edx
EAX:aaaa aac2 EDX: 0000 000b
我完全困惑了,无法弄清楚这个乘法是如何工作的。这里发生了什么事?我注意到在一个类似的问题中imul ebx ;结果是 EDX:EAX
但我不明白 EDX:EAX 表示法:/
I am using a run-time debugger.
EAX: 0000 0023
EDX: 5555 5556
imul edx
EAX: aaaa aac2
EDX: 0000 000b
I am utterly confused, and can't figure out how this multiply is working. What's happening here? I notice in a similar question here that imul ebx ; result in EDX:EAX
I don't understand the EDX:EAX notation though :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
imul
的单操作数形式传递一个 32 位参数时,它实际上意味着EAX * src
,其中EAX
和源操作数都是32 位寄存器或内存。两个 32 位值的乘积不一定适合 32 位:完整的乘法结果最多可以占用 64 位。答案的高32位将写入
EDX
寄存器,低32位写入EAX
寄存器;这用EDX:EAX
表示法表示。在使用
imul edx
的情况下,您将得到EDX:EAX = EAX * EDX
。显式源操作数可以成为隐式操作数之一,甚至可以将EAX
平方为EDX:EAX
。如果您只需要结果的低 32 位,请使用
imul
的 2 操作数形式;它运行速度更快并且没有任何隐式操作数(因此您可以使用最方便的任何寄存器)。imul ecx, esi
会按照您的预期执行ecx *= esi
操作,而不会触及EAX
或EDX
。就像 C 中unsigned x=...;
x *= y;
结果的宽度与输入的宽度相同。imul
也有直接形式:imul ecx, ebx, 1234
的作用是ecx = ebx * 1234
。许多汇编器将接受imul ecx, 1234
作为imul ecx, ecx, 1234
的简写。这些 32x32 => 32 位形式的
imul
对于有符号或无符号都能正常工作;单操作数mul
和imul
的结果仅在上半部分(在EDX
中)不同,而不是下半部分EAX输出。
请参阅 Intel 的
imul
指令参考手册条目。When the one-operand form of
imul
is passed a 32 bit argument, it effectively meansEAX * src
where bothEAX
and the source operand are 32-bit registers or memory.The product of two 32 bit values doesn't necessarily fit in 32 bits: the full multiply result can take up to 64 bits. The high 32 bits of the answer will be written to the
EDX
register and the low 32 bits to theEAX
register; this is represented with theEDX:EAX
notation.In your case with
imul edx
, you getEDX:EAX = EAX * EDX
. It's fine for the explicit source operand to be one of the implicit operands, evenEAX
to square intoEDX:EAX
.If you only want the low 32 bits of the result, use the 2-operand form of
imul
; it runs faster and doesn't have any implicit operands (so you can use whatever registers are most convenient).imul ecx, esi
doesecx *= esi
like you'd expect, without touchingEAX
orEDX
. It's like C whereunsigned x=...;
x *= y;
has the same width for the result as the inputs.imul
also has an immediate form:imul ecx, ebx, 1234
doesecx = ebx * 1234
. Many assemblers will acceptimul ecx, 1234
as short-hand forimul ecx, ecx, 1234
.These 32x32 => 32-bit forms of
imul
work correctly for signed or unsigned; the results of one-operandmul
andimul
only differ in the upper half (inEDX
), not the low-halfEAX
output.See Intel's instruction reference manual entry for
imul
.