The -masm=intel flag is not working for running assembly language in gcc compiler with Intel syntax

Viewed 371

I am trying to use inline assembler __asm in my C program with Intel syntax as opposed to AT&T syntax. I am compiling with gcc -S -masm=intel test.c but it is giving error. Below is my test.c file.

#include <stdio.h>
//using namespace std;
int AsmCode(int num,int power) {
    __asm {
        mov eax, num;
        mov ecx, power;
        shl eax, cl;
    };
}
int main()
{
    printf("eax value is %d\n",AsmCode(2,3));

    //getchar();
    return 0;



}

Expected result was eax value is 16, but errors are occurring like unknown type name 'mov',unknown type name 'shl' etc.

Edit: I have updated the code as:

int AsmCode(int num,int power) {
    __asm__ (
        "movl eax, num;"
        "mov ecx, power;"
        "shl eax, cl;"
    );
}
int main()
{
    printf("eax value is %d\n",AsmCode(2,3));
    return 0;
}

And compiled this code with gcc -S -masm=intel test.c. This resulted in NO OUTPUT, whereas it should produce output as eax value is 16.

When compiled with gcc test.c it produced the errors:

Error: too many memory references for 'mov'
Error: too many memory references for 'shl'

Please help..

1 Answers

The most important error is the first one:

main.cpp:4:11: error: expected '(' before '{' token
     __asm {
           ^
           (

You're using the wrong syntax for GCC. You've used Microsoft Visual Studio syntax. So, your GCC doesn't know that you're trying to give it assembly instructions.

Instead of __asm { ... }, it should be __asm__ ( "..." ).

Like this:

int AsmCode(int num,int power) {
    __asm__ (
        "mov eax, num;"
        "mov ecx, power;"
        "shl eax, cl;"
    );
}

Read more here.

Note that there are further issues with your ASM that you should ask about separately.

Related