Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to multiply by an immediate with mul in x86 Assembly?

I am learning assembly for x86 using DosBox emulator. I am trying to perform multiplication. I do not get how it works. When I write the following code:

mov al, 3
mul 2

I get an error. Although, in the reference I am using, it says in multiplication, it assumes AX is always the place holder, therefore, if I write:

mul, 2

It multiplies al value by 2. But it does not work with me.

When I try the following:

mov al, 3
mul al,2
int 3

I get result 9 in ax. See this picture for clarification: enter image description here

Another question: Can I multiply using memory location directly? Example:

mov si,100
mul [si],5
like image 462
user2192774 Avatar asked Dec 10 '13 15:12

user2192774


People also ask

What does MUL do in assembly?

The mul instruction multiplies the contents of general-purpose register (GPR) RA and GPR RB, and stores bits 0-31 of the result in the target GPR RT and bits 32-63 of the result in the MQ Register. The mul instruction has four syntax forms.

Which instruction can be used instead of MUL to multiply a number by 2?

Use shifts and adds/subs instead of multiplication.

What does MUL do in 8086?

MUL is used to multiply two 16-bit numbers. HLT is used to stop the program. AX is an accumulator which is used to store the result. BX, DX are general purpose registers where BX is used for multiplication and DX is used for result.

How many operands does the MUL mnemonic need?

The mul instruction has 2 operands: one is specified and the other one is implicit. When you write mul cx it means something like: ax = ax * cx .


2 Answers

There's no form of MUL that accepts an immediate operand.

Either do:

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

or (requires 186 for imul-immediate):

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

or:

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

or:

mov al,3
shl al,1   ; same thing as multiplying by 2
like image 151
Michael Avatar answered Sep 25 '22 14:09

Michael


Intel manual

The Intel 64 and IA-32 Architectures Software Developer’s Manual - Volume 2 Instruction Set Reference - 325383-056US September 2015 section "MUL - Unsigned Multiply" column Instruction contains only:

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

r/mXX means register or memory: so immediates (immXX) like mul 2 are not allowed in any of the forms: the processor simply does not support that operation.

This also answers the second question: it is possible to multiply by memory:

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

And also shows why things like mul al,2 will not work: there is no form that takes two arguments.

As mentioned by Michael however, imul does have immediate forms like IMUL r32, r/m32, imm32 and many others that mul does not.