【问题标题】:x86 assembly multiply and divide instruction operands, 16-bit and higherx86 汇编乘法和除法指令操作数,16 位及更高位
【发布时间】:2012-04-05 08:52:09
【问题描述】:

我对 x86 汇编中的乘法和除法运算如何工作感到很困惑。例如,下面的代码似乎并不太难,因为处理的是 8 位。

8 位乘法:

; User Input:
; [num1], 20
; [num2] , 15

mov    ax, [num1]    ; moves the 8 bits into AL
mov    bx, [num2]    ; moves the 8 bits into BL

mul    bl            ; product stored in AX

print  ax

但是当您想将两个 16 位数字相乘时会发生什么?如何将两个 16 位数字相乘,就像用 8 位数字一样?

我很困惑这些值将存储在哪些寄存器中。它们是存储在 AL 和 AH 中,还是只是将 16 位数字存储在 AX 中。表达我的意思:

; User Input:
; [num1], 20
; [num2], 15

mov    eax, [num1]    ; Does this store the 16-bit number in AL and AH or just in AX
mov    ebx, [num2]    ; Does this store the 16-bit number in BL and BH or just in BX

mul    ???            ; this register relies on where the 16-bit numbers are stored

print  eax

有人可以详细说明乘法和除法的工作原理吗? (特别是 16 位和 32 位数字?如果值存储在较低的 AL 和 AH 中,我是否需要轮换位?

或者可以简单地将mov num1num2分别转换成axbx,然后将它们相乘得到eax中的乘积?

【问题讨论】:

标签: assembly x86 division multiplication


【解决方案1】:

快速浏览documentation 会发现MUL 有4 种可能的操作数大小。输入和输出汇总在一个方便的表格中:

------------------------------------------------------
| Operand Size | Source 1 | Source 2   | Destination |
------------------------------------------------------
| Byte         | AL       | r/m8       | AX          |
| Word         | AX       | r/m16      | DX:AX       |
| Doubleword   | EAX      | r/m32      | EDX:EAX     |
| Quadword     | RAX      | r/m64      | RDX:RAX     |
------------------------------------------------------

【讨论】:

  • 我看了看,知道mul 之后值的存储位置。我想知道,当我将值存储在eaxmul 之前)中时,它是否存储在低位(AL 和 AH)而不是高位(AX)中。如果它确实将它们存储在较低的位中,我是否能够将它们旋转到 AX,然后将 AX 和 BX 相乘并打印出答案(我相信它会在 EAX 中)?
  • 如果您在eax 中存储一个值,则它在eaxall 中。我不确定我是否理解。我想你可能会混淆eax 的哪些部分也是ahalax。您可以在同一文档的第 1 卷中看到该图表。特别是,ax 指的是与ah:al 相同的 16 位,而不是 eax 的“高位”16 位。
  • 关闭,但产品将在DX:AX。检查我的答案中的表格。
  • 当然可以 - DX 是 16 位,AX 是 16 位。这使得 DX:AX 成为 32 位数字。 16+16=32,对吧?
  • AX 已经存在 - 它是 EAX 的底部 16 位。您需要将 DX 一半移到 EAX 的上半部分,是的。
猜你喜欢
  • 1970-01-01
  • 2019-10-24
  • 2017-03-11
  • 2018-07-18
  • 1970-01-01
  • 2015-10-11
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多