2013-12-10 79 views
6

我正在學習使用DosBox仿真器的x86程序集。我正在嘗試執行乘法。我不知道它是如何工作的。當我寫下面的代碼:是否有可能在x86彙編中立即乘以mul?

mov al, 3 
mul 2 

我得到一個錯誤。雖然,在我使用的參考,它乘說,它假定AX始終是佔位符,因此,如果我寫:

mul, 2 

它通過2乘以al值,但它不與我合作。

當我嘗試以下方法:

mov al, 3 
mul al,2 
int 3 

我的斧頭得到結果9。看到這張圖片的澄清: enter image description here

另一個問題:我可以直接乘內存位置?例如:

mov si,100 
mul [si],5 

回答

11

沒有任何形式的MUL接受立即操作數。

要麼是:

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

或:

mov ax,3 
imul ax,2 ; imul is for signed multiplication, but that doesn't matter here 
      ; the product is in ax 

或:

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

或:

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

你的第二個代碼片段中的評論是錯誤的。在'imul ax,2'之後,產品在AX中(不在DX:AX中)。 –

+1

還要注意'imul'-immediate是一個3操作數指令。所以你可以非破壞性地使用它,比如'imul cx,si,1234'。大多數彙編程序允許你編寫'imul cx,1234'作爲'imul cx,cx,1234'的簡寫,類似於寫入'vaddps ymm0,ymm1'而不是'vaddps ymm0,ymm0,ymm1':即當dst = SRC1。 –

2

英特爾手冊

Intel 64 and IA-32 Architectures Software Developer’s Manual - Volume 2 Instruction Set Reference - 325383-056US September 2015 部 「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將不起作用:有沒有形式有兩個參數。

正如邁克爾所說的那樣,imul確實有像IMUL r32, r/m32, imm32這樣的直接形式,而mul則沒有。