Intel 8086 汇编——寄存器的平方
原则上,对寄存器的值求平方并不难:
mov ax, [var]
mov cx, [var]
mul cx // square of answer is in DX:AX
但我不得不思考——我正在学习汇编的课程非常注重效率;即使少一行线的差异也最多可扣 5 分。
我意识到这是微优化,但是以下代码会以相同的方式执行吗?:
mov ax, [var]
mul ax // is answer still in DX:AX ?
我想用一种更简单的方式来表达我的问题:AX(或 AL/AH)是 mul 和
imul
命令?
In principle, squaring the value of a register isn't hard:
mov ax, [var]
mov cx, [var]
mul cx // square of answer is in DX:AX
But I got to thinking -- the course that I'm learning Assembly for prizes efficiency very highly; a difference of even a single line less could be worth up to 5 points.
I realize this is micro-optimization, but would the following code perform the the same way?:
mov ax, [var]
mul ax // is answer still in DX:AX ?
I suppose a much simpler way of expressing my question: is AX (or AL/AH) a valid parameter for the mul
and imul
commands in 8086 assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
mul ax
将ax*ax
放入dx:ax
。Yes,
mul ax
putsax*ax
todx:ax
.您可以使用 mul ax 来执行 DX:AX = AX * AX
但请注意,在这种情况下,您将丢失 AX 中的值,因此如果您需要在某个时候再次使用该值,最好使用 mul bx 选项,因为 BX 保持不变。
另外,如果您使用 mul al 或 (mul ah),您将不会执行 AX=AXAX 而是 AX=ALAL (或 AX=AL*AH),因此如果 AX 中的值大于 255,您将没有进行 AX 的乘法,因为您完全忽略并覆盖了值的较高部分(您忽略了 AH)。
You can use mul ax it would do DX:AX = AX * AX
but note that in this case you will lose the value you had in AX so if you need to use this value again sometime it is better to use the mul bx option, because BX remains intact.
Also if you use mul al or (mul ah) you will not do AX=AXAX but AX=ALAL (or AX=AL*AH) so if the value in AX is bigger than 255 you aren't doing a multiplication of AX because you completely ignore and overwrite the higher part of the value (you ignore AH).