Search code examples
assemblyclanginline-assemblyllvm-clangemit

How use _emit in clang?


How to use _emit to emit bytes in clang compiler?

e.g(in MSVC):

#define emit_nop() _asm _emit 0x90

Solution

  • In compilers that support GNU extensions, there's no need for a separate emit keyword, just use GNU C inline assembly:

    asm(".byte 0x90");   // implicitly   asm volatile
    

    Or .long to emit a 32-bit constant.

    GNU C inline asm is not parsed to detect clobbers or anything, so you could just asm("nop");

    If you want to use instructions that modify registers, you normally need to tell the compiler about it with GNU C Extended inline assembly (output/input/clobbers). See https://stackoverflow.com/tags/inline-assembly/info.