views:

178

answers:

4

I don't understand why this code

#include <iostream>
using namespace std;
int main(){
    int result=0;

    _asm{
        mov eax,3;
        MUL eax,3;
        mov result,eax;
    }

    cout<<result<<endl;
    return 0;
}

shows the following error.

1>c:\users\david\documents\visual studio 2010\projects\assembler_instructions\assembler_instructions.cpp(11): error C2414: illegal number of operands

Everything seems fine, and yet why do I get this compiler error?

+11  A: 

According to this page, the mul instruction only takes a single argument:

mul arg

This multiplies "arg" by the value of corresponding byte-length in the A register, see table below:

operand size 1 byte 2 bytes 4 bytes

other operand AL AX EAX

higher part of result stored in: AH DX EDX

lower part of result stored in: AL AX EAX

Justin Ethier
@Davit: In other words, try just `mul 3`.
quixoto
You need to use another register in the MUL operator
Simeon Pilgrim
+4  A: 

Thus following the notes as per Justin's link:

#include <iostream>

int main()
{
    int result=0;

    _asm{
        mov eax, 3;
        mov ebx, 4;
        mul ebx;
        mov result,eax;
    }

    std::cout << result << std::endl;

    return 0;
}
Simeon Pilgrim
+2  A: 

Use:

imul eax, 3;

or:

imul eax, eax, 3;

That way you don't need to worry about edx -register being clobbered. It's "signed integer multiply". You seem to have 'int' -result so it shouldn't matter whether you use mul or imul.

Sometimes I've gotten errors from not having edx register zeroed when dividing or multiplying. CPU was Intel core2 quad Q9550

There's numbingly overengineered but correct intel instruction reference manuals you can read. Though intel broke its websites while ago. You could try find same reference manuals from AMD sites though.

Update: I found the manual: http://www.intel.com/design/pentiumii/manuals/243191.htm

I don't know when they are going to again break their sites, so you really always need to search it up.

Update2: ARGHL! those are from year 1999.. well most details are unfortunately the same.

Cheery
*I* like the intel instruction manuals. :)
Paul Nathan
+1  A: 

You should download the Intel architecture manuals.

http://www.intel.com/products/processor/manuals/

For your purpose, volume 2 is going to help you the most.

As of access in July 2010, they are current.

Paul Nathan
How did you find those?
Cheery
@Cheery: Googled 'intel x86 instruction set reference'. That link points to june 2010 edition manuals.
Paul Nathan