0

All documentation uses AT&T syntax.

int EAX;
asm( "movl $5, %0"
   : "=a" (EAX)
);

How do I re-write this using Intel syntax?

int EAX;
asm( 
 ".intel_syntax noprefix;"
 "mov %0, 5"
 :"=a"(EAX)
);

Doesn't work:

 error: unknown token in expression
 asm(".intel_syntax noprefix;"
     ^
<inline asm>:1:29: note: instantiated into assembly here
        .intel_syntax noprefix;mov %0, 5
Alexander
  • 9
  • 3
  • 2
    For the next time, “doesn't work” is not a useful error description. If something doesn't work, post all error messages you receive exactly as you get them. If you get no error, describe exactly what exactly doesn't work and in what way. – fuz Nov 16 '19 at 23:43
  • I've added the error @fuz – Alexander Nov 17 '19 at 03:55
  • Are you using gcc or are you using clang? If you are using clang, which version do you use? – fuz Nov 17 '19 at 13:08

1 Answers1

3

As a rule of thumb, gcc inline assembly must be written in AT&T syntax to compile normally. You can compile with -masm=intel to use Intel syntax, but then your code won't compile without this flag. It is possible to provide inline assembly both in Intel and AT&T assembly syntax so your code compiles both with and without -masm=intel. Refer to the manual for details.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • It works with a simple one-liner `.intel_syntax noprefix;`, no need in `-masm=intel` – Alexander Nov 17 '19 at 03:55
  • However, what if the compiler continues producing code after this "one-liner?" It won't know that it's supposed to start using intel syntax since it doesn't parse your command string. So it's going to write everything in at&t syntax, while the assembler would expect intel. You could try setting it back, but what if someone compiles your code with -masm-intel? Then "setting it back" actually results in changing to at&t syntax. Mostly you're just setting yourself up for headaches. – David Wohlferd Nov 17 '19 at 08:31
  • Ok good point @DavidWohlferd. I tried -masm-intel flag, same error unfortunately. – Alexander Nov 17 '19 at 11:54
  • @Alexander I'm sure the error is not the same. What is the exact error you get with `-masm=intel`? – fuz Nov 17 '19 at 13:08
  • Hm I'm actually getting segfault. Can you please run it? It's a 4 line program. I've been trying to figure it out for ever. – Alexander Nov 18 '19 at 05:40
  • @Alexander Sure, put the code in the question and I'll have a look. – fuz Nov 18 '19 at 09:43