3

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..

Abhilash
  • 63
  • 6

1 Answers1

3

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    You can't just use `num`, `power`, etc. in GCC syntax without declaring them as inputs. And you'd probably want to specify clobbers. – Dan M. Jan 15 '19 at 12:20
  • There are certainly other issues. I'll let the OP raise a ASM question if they can't figure that bit out. – Lightness Races in Orbit Jan 15 '19 at 12:21
  • Alright. Just seems to be confusing to provide not working code as an example (without indication that it's not supposed to work) to already struggling OP. – Dan M. Jan 15 '19 at 12:37
  • @DanM. Perhaps but I'm only keen to answer the C++ question that was asked, particularly as I don't know ASM. – Lightness Races in Orbit Jan 15 '19 at 12:43
  • @DanM. I know we can't use directly the variable names in GCC syntax without declaring them, because they use AT&T syntax. But ```masm= intel``` should make them compatible to run with GCC compiler as they were with Visual studio compiler – Abhilash Jan 15 '19 at 14:10
  • @AbhilashBarigidad no. You are confusing intel vs at&t syntax with inline assembly compiler extension (and its "syntax"). They have little to do with each over. You can use intel syntax for asm in GCC (i.e. implicit types, no $ before constants, different argument order and etc.), but you still need to conform to `__asm__` [syntax](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html). – Dan M. Jan 15 '19 at 14:17
  • @DanM. I have updated the code as requested, please look into it. – Abhilash Jan 15 '19 at 15:05
  • @LightnessRacesinOrbit. Have updated code, please look into it. – Abhilash Jan 15 '19 at 15:05
  • @AbhilashBarigidad Like I said, you have errors unrelated to your original question - please ask them in a new question. I do not know ASM so will not be able to help with that one. – Lightness Races in Orbit Jan 15 '19 at 15:13