NASM 中的 MUL 运算符与 IMUL 运算符
MUL 运算符仅采用单操作数形式有什么原因吗?
IMUL 运算符可以采用三种不同的形式(具有一个、两个或三个操作数),这更加方便。从技术角度来看,我看不出 MUL 运算符不能采用两个/三个操作数形式的任何原因。
Is there any reason why MUL operator is only in single operand form?
IMUL operator can be in three different forms (with one, two or three operands) and this is much more convenient. From the technical point of view I don't see any reason why MUL operator can't be in two/three operands form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它与输出的字节码有关。在 80286 之前的世界中,操作码太多,因此英特尔工程师正在寻找解决该问题的方法。一种解决方案是将指定操作(在本例中为乘法)的字节码部分扩展为对第一个操作数进行编码的字节码部分。这显然意味着执行 MUL 操作码时只能支持一个操作数。由于乘法需要两个操作数,因此他们通过将第一个操作数始终是 eax 寄存器硬编码到处理器中来解决问题。后来的处理器支持多种长度的字节码,这允许它们将更多数据编码到单个命令中。这使得他们能够使 IMUL 操作码变得更加有用。
如今,IP 地址即将耗尽,这有一个有趣的相似之处。
It has to do with the bytecodes that are output. In the pre-80286 world there were too many opcodes so the Intel engineers were finding ways to overcome the problem. One solution was to extend the portion of the bytecode that specifies the operation (multiplication in this case) into the portion of the bytecode that encoded the first operand. This obviously meant that only one operand could by supported when executing the MUL opcode. Because a multiplication requires two operands, they solved the problem by hard coding into the processor that the first operand would always be the eax register. Later processors supported bytecodes that were of multiple lengths wich allowed them to encode more data into a single command. This allowed them to make the IMUL opcode much more useful.
There is an interesting parallel today with the IP addresses running out.
并不是说 NASM 不支持 - 在 CPU 上,指令的签名版本只是比未签名版本支持更多的变体。
It's not that NASM doesn't support it - on the CPU, the signed version of the instruction simply supports more variants than the unsigned version.
186 指令集引入了三操作数
imul
以及带有立即数操作数的两操作数形式(这是三操作数形式的别名)。后来,386 添加了双操作数形式,即一个寄存器和一个 r/m 操作数。所有这些新形式的共同点是,乘法要么是 16 位乘以 16 位(可能来自符号扩展的 8 位立即数),得到 16 位结果,要么是 32 位乘以 32 位,得到 32 位结果。在这些情况下,
imul
结果的低 16 或低 32 位与等效mul
的结果相同,只有标志(CF 和 OF)可能有所不同。因此不需要单独的多操作数mul
。据推测,设计者采用了助记符imul
,因为具有 8 位立即数的形式会对该立即数进行符号扩展。Three-operand
imul
, as well as two-operand forms with an immediate operand (which is an alias to the three-operand form) were introduced with the 186 instruction set. Later, the 386 added two-operand forms with a register and an r/m operand.All of these new forms have in common that the multiplication is done either with 16 bits times 16 bits (possibly from sign-extended 8-bit immediate) with a 16 bit result, or 32 bits times 32 bits with a 32 bit result. In these cases, the low 16 or low 32 bits of the result is the same for
imul
as they would be for an equivalentmul
, only the flags (CF and OF) may differ. So a separate multi-operandmul
isn't needed. Presumably the designers went with the mnemonicimul
because the forms with an 8 bit immediate do sign-extend that immediate.