gccarminline-assembly

gcc inline asm template for constant with out hash


I am trying to emit a global SYMBOL based on a #define VALUE. My attempt is as follows:

__asm__ (".globl SYMBOL");
__asm__ (".set SYMBOL, %0" :: "i" (VALUE));

What is emitted by gcc to the assembler is the following:

.globl SYMBOL
.set SYMBOL, #VALUE

How can I get rid of the hash in the .set before VALUE. FWIW, my target is ARM.


Solution

  • Use the c operand modifier:

    __asm__ (".set SYMBOL, %c0" : : "i" (VALUE));
    

    Try on godbolt

    gcc used to document this feature as supported only on x86, but it actually worked on ARM as well, and as of gcc 13, it is documented as supported on all targets.

    (The other AArch64-specific modifiers, like %w0 to emit w11 in place of x11, are likewise supported a long way back, for compatibility with armclang. They are still not documented by gcc 13, but are documented in the trunk code, so presumably will be a fully documented feature for gcc 14.)