0

I am using GCC 10.3.0 and I have this source code:

__asm__ volatile
(
    "movl %%gs:%0, %%eax\n\t"
    : "=a"(some_output) 
    : "i"(immediate_value)
);

I want to make GCC translate this instruction into

movl %gs:immediate_value, %eax

but because I am using AT&T syntax and using immediate value I get additional '$':

movl %gs:$immediate_value, %eax

and this is an incorrect AT&T syntax.

I know tha I can use 'r' constraint instead of 'i', which will be ok, but I need to get exactly this opcode with immediate value.

The second solution is to rewrite asm code using Intel syntax and to pass -masm=intel to the compiler. But then I get errors with htons function: with optimization flag -O3 GCC substitutes some AT&T asm code instead of direct call.

Please, advise me some solutions.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    You're looking for https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#x86Operandmodifiers - `%c0` prints the constant without punctuation, even in AT&T mode. This is probably a duplicate... looking now. – Peter Cordes Jan 06 '22 at 18:40
  • @PeterCordes, thank you. That is exactly what I nedded. – Denis Popov Jan 06 '22 at 19:38
  • 1
    Also consider something like `asm volatile("movl %%gs:%0, %%eax" : "=a"(output) : "m"(*(char*)immediate));` – fuz Jan 06 '22 at 20:34

0 Answers0