在 x86 汇编中是否可以用 mul 乘以立即数?

问题描述 投票:0回答:4

我正在使用 DosBox 模拟器学习 x86 的汇编。我正在尝试执行乘法。我不明白它是如何工作的。当我编写以下代码时:

mov al, 3
mul 2

我收到错误。尽管在我使用的参考文献中,它以乘法表示,但它假设 AX 始终是占位符,因此,如果我写:

mul, 2

它将

al
值乘以 2。但它对我不起作用。

当我尝试以下操作时:

mov al, 3
mul al,2
int 3

我在 ax 中得到结果 9。请参阅此图片以进行澄清: enter image description here

另一个问题:我可以直接使用内存位置进行乘法吗?示例:

mov si,100
mul [si],5
assembly x86 dos multiplication immediate-operand
4个回答
19
投票

没有任何形式的

MUL
接受立即操作数。

要么:

mov al,3
mov bl,2
mul bl     ; the product is in ax

或(立即数需要 186):

mov ax,3
imul ax,2  ; imul is for signed multiplication, but low half is the same
           ; the product is in ax.  dx is not modified

或:

mov al,3
add al,al  ; same thing as multiplying by 2

或:

mov al,3
shl al,1   ; same thing as multiplying by 2

4
投票

英特尔手册

Intel 64 和 IA-32 架构软件开发人员手册 - 第 2 卷指令集参考 - 325383-056US 2015 年 9 月 “MUL - 无符号乘法”列

Instruction
部分仅包含:

MUL r/m8
MUL r/m8*
MUL r/m16
MUL r/m32
MUL r/m64

r/mXX
表示寄存器或内存:因此任何形式都不允许像
immXX
这样的立即数 (
mul 2
):处理器根本不支持该操作。

这也回答了第二个问题:可以通过内存进行乘法:

x: dd 0x12341234
mov eax, 2
mul dword [x]
; eax == 0x24682468

并且还说明了为什么像

mul al,2
这样的东西不起作用:没有任何形式需要两个参数。

然而,正如 Michael 所提到的,

imul
确实有像
IMUL r32, r/m32, imm32
这样的直接形式以及许多其他
mul
没有的形式。


2
投票

没有立即

mul
,但在 186 及更新版本中有非扩大
imul
-立即,在 386 及更新版本中则有
imul reg, r/m
。有关更多详细信息,请参阅@phuclv 对 problem in Understanding mul & imul instructions of Assembly language 的回答,当然还有 Intel 的 mul 和 imul 指令集参考手册:

即使在最新的 CPU 上也没有内存目标

mul
imul

如果您愿意,可以在 186 及更新版本上使用

imul cx, [si], 5
,用于 16 位操作数大小及更宽的操作数。在 386 上,也是
imul di, [si]

但是对于 8 位操作数大小,这些新形式的 imul 不存在,因此没有

imul cl, [si], 5

在 386 或更高版本上,使用 LEA 乘以简单常量通常会更有效,尽管它确实会花费更多的代码大小。

; assuming 16-bit mode
    mov  cx, [si]              ; or better movzx ecx, word [si] on newer CPUs
    lea  cx, [ecx + ecx*4]     ; CX *= 5

0
投票

.型号小 .stack 100h 。数据 num1 dw 4
数字 2 深度 4 var db '结果是:$'

.代码

    main proc

    mov ax,@data
    mov ds,ax
                
     lea dx,var
     mov ah,9
    int 21h

     mov ax,num1
     mov bx,num2
     mul bx

     mov dx, ax
     add dx,'0'

     mov ah,2
     int 21h

           
     mov ah,4ch
     int 21h

     main endp

     end main
© www.soinside.com 2019 - 2024. All rights reserved.