为什么x86汇编中有64位的红利? [英] Why is the dividend 64 bits in x86 assembly?

查看:54
本文介绍了为什么x86汇编中有64位的红利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么idiv x86汇编指令将EDX:EAX(64位)除以给定的寄存器,而其他数学运算(包括乘法)仅对单个输入和输出寄存器进行运算?

Why does the idiv x86 assembly instruction divide EDX:EAX (64 bits) by a given register whereas other mathematical operations, including multiplication, simply operate on single input and output registers?

乘法:

mov eax, 3
imul eax, 5

部门:

mov edx, 0
mov eax, 15
mov ebx, 5
idiv ebx

我知道EDX用于存储余数,但是为什么没有针对此行为的单独指令?对我来说似乎不一致.

I am aware that EDX is used to store the remainder, but why is there no separate instruction for this behaviour? It just seems inconsistent to me.

推荐答案

指令集提供有效实现任意宽度整数算术所需的指令.对于加法和减法,您需要知道的只是固定宽度结果之外的操作是产生进位(加法)还是借位(减法).这就是为什么有一个进位标志的原因.对于乘法,您需要能够将两个单词相乘并得到一个双单词结果.这就是imuledx:eax中产生其结果的原因.对于除法,您需要能够对全角数字进行除法并获得商和余数.

The instruction set provides the instructions that are necessary to implement arbitrary-width integer arithmetic efficiently. For addition and subtraction, all you need to know for this beyond a fixed width result is whether the operation resulted in a carry (for addition) or a borrow (for subtraction). This is why there is a carry flag. For multiplication, you need to be able to multiply two words and get a double word result. This is why imul produces its result in edx:eax. For division, you need to be able to divide a double-width number and get the quotient and the remainder.

要了解为什么需要这些特定操作,请参阅Knuth的计算机编程艺术,第2卷,其中详细介绍了用于实现任意宽度算术的算法.

To understand why you need these specific operations, see Knuth's The Art of Computer Programming, Volume 2, which goes into detail on the algorithms for implementing arbitrary-width arithmetic.

至于为什么在x86指令集中没有更多不同形式的乘法和除法指令,没有二乘幂的乘法和除法比其他指令稀有得多,因此Intel可能没有想要用完可能用于更频繁使用的指令的操作码.通用程序中的大多数乘法和除法都是由2的幂组成的.对于这些,您可以使用移位或lea指令来代替.

As for why there aren't more different forms of multiplication and division instructions in the x86 instruction set, multiplication and division that isn't by a power of two is much more rare than other instructions, so Intel probably didn't want to use up opcodes that could be used for instructions that will be used more frequently. Most multiplications and divisions in general purpose programs are by powers of two; for these you can use bitshifts or the lea instruction instead.

这篇关于为什么x86汇编中有64位的红利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆