Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error with embedded assembler

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?


2 Answers

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

like image 71
Justin Ethier Avatar answered Sep 08 '26 03:09

Justin Ethier


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;
}
like image 33
Simeon Pilgrim Avatar answered Sep 08 '26 01:09

Simeon Pilgrim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!