MUL instruction doesn't support an immediate value

Tanner Babcock picture Tanner Babcock · Dec 25, 2010 · Viewed 36.2k times · Source

I've read a few tutorials and examples, but I cannot wrap my head around how the MUL instruction works. I've used ADD and SUB without problems. So apparently this instruction multiplies its operand by the value in a register.

What register (eax, ebp, esp, etc.) is multiplied by the first operand? And what register is the result stored in, so I can move it to the stack? Sorry, I'm just learning x86 assembly.

When I try to compile this line...

mul     9

I get, Error: suffix or operands invalid for 'mul'. Can anyone help me out?

    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

Answer

Eugene Smith picture Eugene Smith · Dec 25, 2010

MUL can't use an immediate value as an argument. You have to load '9' into a register, say,

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

which would multiply eax by ecx and store the 64-bit product in edx:eax.

There's a good comprehensive reference of x86 assembly instructions on the Intel web site, see here

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

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

But that is probably far more information that you need now.