Quaxton Hale
Quaxton Hale

Reputation: 2540

Inline Assembly with GCC

#include <stdio.h>

int
get_random(void)
{
    asm(".intel_syntax noprefix\n"
        "mov eax, 42           \n");
    asm("mov eax, 42 \n");
}
int
main(void)
{
    return printf("The answer is %d.\n", get_random());
}

I am trying to compile this C++ program with these CLI commands: g++ asm.cpp -o asm

Error messages:

/tmp/ccXHbaRO.s: Assembler messages:
/tmp/ccXHbaRO.s:41: Error: no such instruction: `movl %eax,%esi'
/tmp/ccXHbaRO.s:42: Error: no such instruction: `movl $.LC0,%edi'
/tmp/ccXHbaRO.s:43: Error: no such instruction: `movl $0,%eax'

Because I added asm(".intel_syntax noprefix\n"); I thought I wouldn't need to add the GCC flag -masm=intel?

Also, where can I find out more information about the -masm flag? Is there a NASM equivalent?

Upvotes: 2

Views: 847

Answers (1)

6502
6502

Reputation: 114579

The code you write in assembly gets placed verbatim in the output of the compiler.

This means that if you change the format or other global options about how to parse assembly code you will need to restore the default options at the end.

If you don't do this the code generated by the compiler after your part will become invalid.

Upvotes: 3

Related Questions