MUL instruction does not support immediate value

I have read several lessons and examples, but I cannot understand how the MUL instruction works. I used ADD and SUB without any problems. Thus, obviously, this instruction multiplies its operand by a value in the register.

Which register (eax, ebp, esp, etc.) is multiplied by the first operand? And in what register is the result stored so that I can move it onto the stack? Sorry, I'm only studying the x86 build.

When I try to compile this line ...

 mul 9 

I get an Error: suffix or operands invalid for 'mul' . Can someone help me?

  global main main: push ebp movl ebp, esp sub esp, byte +8 mov eax, 7 mul 9 mov [esp], eax call _putchar xor eax, eax leave ret 
+6
assembly x86
source share
6 answers

MUL cannot use an immediate value as an argument. You must load '9' into the register, say

  movl $7, %eax movl $9, %ecx mull %ecx 

which multiplies eax by ecx and stores the 64-bit product in edx: eax.

There is a good comprehensive reference to x86 assembly instructions on the Intel website, see here

http://www.intel.com/Assets/PDF/manual/253666.pdf

http://www.intel.com/Assets/PDF/manual/253667.pdf

But this is probably much more information that you need now.

+12
source share

http://siyobik.info/index.php?module=x86&id=210

destination operand means an operand located in the AL, AX or EAX register (depending on the size of the operand); source operand located in general register or memory location

The result is stored in the register AX, register a pair of DX: AX or a pair of registers EDX: EAX (depending on the operand size), with the highest-order product contained in the register AH, DX, or EDX, respectively. If the high-order bits of the product are 0, the CF and OF flags are cleared; otherwise flags are set.

In your source, it should be mul instead of mull

+4
source share

I am not an expert, but I think that your problem is that mul will not accept a direct operand in your case, the decimal constant is 9, so try putting 9 in, say, in the bx register as follows:

 mov bx, 9 mul bx 

To answer your question about mul in general, it looks like this:

 mul reg/memory 

and does it:

 dx:ax := ax*reg/mem 

Thus, with words, he expects one operand, which must be either a register or a memory cell (storing the value multiplied in essence as a variable), and multiplies it by an ax, and then depending on the length of the register / memory (such Thus, the operand you gave) stored it either in the ax or in dx: ax or edx: eax

I hope a friend helped you there!

+3
source share

If you look at the table of the MUL format, you will notice that it accepts only one register parameter. However, switch to IMUL and you will see that there are many forms that take immediate

  6B / r ib IMUL r16, r / m16, imm8 word register โ† r / m16 โˆ— sign-extended immediate byte.
 6B / r ib IMUL r32, r / m32, imm8 doubleword register โ† r / m32 โˆ— sign-extended immediate byte.
 REX.W + 6B / r ib IMUL r64, r / m64, imm8 Quadword register โ† r / m64 โˆ— sign-extended immediate byte.
 69 / r iw IMUL r16, r / m16, imm16 word register โ† r / m16 โˆ— immediate word.
 69 / r id IMUL r32, r / m32, imm32 doubleword register โ† r / m32 โˆ— immediate doubleword.
 REX.W + 69 / r id IMUL r64, r / m64, imm32 Quadword register โ† r / m64 โˆ— immediate doubleword.

So, to multiply Eax by 9, you can do it

 mov eax, 7 imul eax, eax, 9 ; eax = eax*9 

Take a closer look and you may also notice that these versions are not expandable. In fact, imul is imul used exclusively for almost all multiplications, because multiplication without extension is the same for signed and unsigned values, and multiplication in high-level languages โ€‹โ€‹does not expand (that is, the output types and input operand types are the same) if you do not bring the operands to more wide type. As a result, modern x86 processors are often optimized for imul and also have much more forms than mul

+2
source share

I will give you a link to my favorite easy-to-read but full link for x86: http://www.ousob.com/ng/iapx86/ng1840d.php

+1
source share

How to read in the Intel manual to see that this is not possible

Download the Intel manual first. Current download page: http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html , but just a google intel x86 manual, as that will change.

What we need is a combination with instructions for building volumes 2-A, which contains all the instructions.

In this guide, find the documentation for the DIV . We see that the DIV has the following forms in the "Instruction" column:

  • DIV r/m8
  • DIV r/m16
  • DIV r/m32
  • DIV r/m64

r/mX means that it can accept a register or memory cell of size X.

Therefore, there is no form that accepts immX , which is an immediate value or literal.

Thus, it is impossible to code this instruction for the processor, and now we know that the only way is to go through the registers or memory.

Registers are faster, so we use them if possible.

0
source share

All Articles