How to generate assembly code with clang in Intel syntax?

58,245

Solution 1

As noted below by @thakis, newer versions of Clang (3.5+) accept the -masm=intel argument.


For older versions, this should get clang to emit assembly code with Intel syntax:

clang++ -S -mllvm --x86-asm-syntax=intel test.cpp

You can use -mllvm <arg> to pass in llvm options from the clang command line. Sadly this option doesn't appear to be well documented, and thus I only found it by browsing through the llvm mailing lists.

Solution 2

As of clang r208683 (clang 3.5+), it understands -masm=intel. So if your clang is new enough, you can just use that.

Solution 3

Presuming you can have Clang emit normal LLVM byte codes, you can then use llc to compile to assembly language, and use its --x86-asm-syntax=intel option to get the result in Intel syntax.

Share:
58,245
Jesse Good
Author by

Jesse Good

Currently diving deep into Blazor and ASP.NET Core.

Updated on July 05, 2022

Comments

  • Jesse Good
    Jesse Good almost 2 years

    As this question shows, with g++, I can do g++ -S -masm=intel test.cpp. Also, with clang, I can do clang++ -S test.cpp, but -masm=intel is not supported by clang (warning argument unused during compilation: -masm=intel). How do I get intel syntax with clang?

  • Jesse Good
    Jesse Good almost 12 years
    Thanks again. I also found out llc by default turns optimizations on (-02) while gcc and clang use no optimizations (-O0) by default (it took me forever to figure out why the assembly output was different).
  • Trass3r
    Trass3r over 10 years
    llvm.org/bugs/show_bug.cgi?id=17465 requests support for -masm=intel and beyond.
  • Saumya Suhagiya
    Saumya Suhagiya almost 9 years
    @dcoles Not working for clang 3.7. I executed $clang -S -mllvm --x86-asm-syntax=intel file.c. Am I doint something wrong here? It's not giving any error and silently exiting.
  • dcoles
    dcoles almost 9 years
    @saumya-suhagiya I've just confirmed that this still works with clang 3.8.0-svn244195-1~exp1. The command shown above will generate a file called test.s. You change this output location to a custom location using the -o <path> option.
  • Alexander Cska
    Alexander Cska over 8 years
    @dcoles Hi, perhaps a not so smart question. Is it possible to compile and link the assembly code with nasm?
  • dcoles
    dcoles over 8 years
    @AlexanderCska I'm not sure this is possible without some manual fixup. While the syntax for NASM is similar to Intel's, there is some incompatibilities between them.
  • Yai0Phah
    Yai0Phah almost 5 years
    It seems to me that the code generated by -masm=intel cannot be compiled by clang again? I am running clang 3.8.