Intel 8086 汇编——寄存器的平方

发布于 2024-09-10 13:01:27 字数 413 浏览 1 评论 0原文

原则上,对寄存器的值求平方并不难:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2024-09-17 13:01:27

是的,mul axax*ax 放入 dx:ax

Yes, mul ax puts ax*ax to dx:ax.

望笑 2024-09-17 13:01:27

您可以使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文