简体   繁体   English

x86汇编对16位及更高位的指令操作数进行乘法和除法

[英]x86 assembly multiply and divide instruction operands, 16-bit and higher

I'm rather confused about how the multiply and divide operations work in x86 assembly. 我对x86汇编中的乘法和除法运算如何工作感到困惑。 For example, the code below doesn't seem too difficult since deals with 8-bit. 例如,自从处理8位以来,下面的代码似乎并不太难。

8-Bit Multiplication: 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

But what happens when you want to multiply two 16-bit numbers? 但是当你想要乘以两个16位数时会发生什么? How would one multiply two 16 bit numbers the same way as it has been done with the 8 bit numbers? 如何将两个16位数字乘以与8位数字相同的方式?

I'm confused as to what registers the values would be stored in. Would they be stored in AL and AH or would it simply store the 16-bit number in AX. 我很困惑这些值将存储在哪些寄存器中。它们是存储在AL和AH中还是只是将16位数存储在AX中。 To show what I mean: 显示我的意思:

; 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

Could someone elaborate a bit on how the multiplying and dividing works? 有人可以详细说明乘法和除法的工作原理吗? (specifically with 16-bit and 32-bit numbers? Would I need to rotate bits if the values are stored in the lower AL and AH? (特别是16位和32位数字?如果值存储在较低的AL和AH中,我是否需要旋转位?

Or can one simply mov num1 and num2 into ax and bx respectively and then multiply them to get the product in eax ? 或者可以简单地将mov num1num2分别移动到axbx ,然后将它们相乘以获得eax的产品?

A quick glance at the documentation shows that there are 4 possible operand sizes for MUL . 快速浏览一下文档就会发现MUL有4种可能的操作数大小。 The inputs and outputs are summarized in a handy table: 输入和输出汇总在一个方便的表格中:

------------------------------------------------------
| 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     |
------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM