0

I'd like to use intel syntax gcc inline assembly, leaving gcc's default -masm=att dialect untouched.

The following code works fine:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int a = 123;
    int b = 0;
    printf("before: a = %d\n", a);
    printf("before: b = %d\n", b);
    __asm__ __volatile__ (
        ".intel_syntax noprefix\n\t"
        "mov eax, %[a]\n\t"
        "mov %[b], eax\n\t"
        ".att_syntax prefix\n\t"
        : [b]"+r"(b)
        : [a]"r"(a)
        : "eax"
    );
    printf("after: a = %d\n", a);
    printf("after: b = %d\n", b);
    return 0;
}
// before: a = 123
// before: b = 0
// after: a = 123
// after: b = 123

But if i change Output Operands Constraint from register('r') to memory('m'), error occurs:

Error: junk `(%rbp)' after expression

In the generated assembly file, I find this:

#APP
    .intel_syntax noprefix
    mov eax, -16(%rbp)
    mov -12(%rbp), eax
    .att_syntax prefix
#NO_APP

It looks like gcc renders Assembler Template using AT&T Effective-Address dialect.

I searched the web, Extended Asm shows something like "Multiple assembler dialects in asm templates" and "x86 Operand Modifiers", but I still didn't solve the problem.

Is there a way to tell gcc, (maybe some instructions around __asm__, telling gcc to do operand-substitution with Intel-syntax addressing modes temporarily, like -masm=intel do in the whole file), render the Assembler Template using Intel Effective-Address dialect temporarily in __asm__ () block, not the whole file, like this:

#APP
    .intel_syntax noprefix
    mov eax, [%rbp - 16]
    mov [%rbp - 12], eax
    .att_syntax prefix
#NO_APP
Wot
  • 102
  • 11
  • There is `-masm=intel` but it is going to apply to a whole translation unit, or at least a whole function. – Marc Glisse Feb 09 '22 at 09:31
  • @MarcGlisse: `-masm=intel` is the correct way to make all inline asm statements use Intel syntax, and AFAIK the only way to make GCC substitute in Intel-syntax memory operand syntax. (Unfortunately I don't know a way that works with clang). This crappy hack of using `.intel_syntax noprefix` inside the asm statement only works for registers because GAS happens to still accept `%ecx` in intel-noprefix mode, and using `.att_syntax` at the end breaks the later compiler-generated code if you did compile with `-masm=intel`. I'd never recommend it. – Peter Cordes Feb 09 '22 at 13:37
  • @PeterCordes Clang may have made a [change](https://reviews.llvm.org/D113707) to improve this. Haven't checked it myself. – David Wohlferd Feb 09 '22 at 19:50
  • @DavidWohlferd: thanks; coincidentally PSkocik just today [pointed out](https://stackoverflow.com/questions/49822854/c-inline-assembly-getting-operand-size-mismatch-when-using-cmpxchg/49839296?noredirect=1#comment125605202_49839296) that clang-trunk works; now I know why. And have confirmation that previously asm template strings and operand-substitution were unconditionally AT&T. Will have to update [How to set gcc to use intel syntax permanently?](https://stackoverflow.com/q/38953951) accordingly. – Peter Cordes Feb 09 '22 at 20:15
  • @PeterCordes I found out when the closed [my bug](https://bugs.llvm.org/show_bug.cgi?id=21401). – David Wohlferd Feb 10 '22 at 00:12
  • This question is about solving operand-substitution problem under 'gcc -masm=att'. Question [https://stackoverflow.com/questions/38953951/how-to-set-gcc-to-use-intel-syntax-permanently] is about wrongly added `.att_syntax` assembler command under 'gcc -masm=intel'. I think they should not be duplicate. @PeterCordes – Wot Feb 10 '22 at 07:31
  • 1
    There is no way to mark a specific `asm` instruction as intel/att. As Marc pointed out, the only option that affects which assembly is used by the asm instruction is the `masm=` command line option. Using `.intel_syntax` is a bad idea for a number of reasons, as peter points out. If that's not going to work for you, all that leaves is writing each statement twice, once in att syntax and once in intel syntax, using [dialects](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Multiple-assembler-dialects-in-asm-templates). Then your code will work no matter which command line options are used. – David Wohlferd Feb 10 '22 at 08:11
  • @Wot: The main point of my answer on [How to set gcc to use intel syntax permanently?](https://stackoverflow.com/q/38953951) is how to use Intel-syntax inline asm. The details of what the question did wrong didn't seem super relevant, although I think there have been some about `"m"` operands with the wrong syntax. I can add [Can I use Intel syntax of x86 assembly with GCC?](https://stackoverflow.com/q/9347909) as another duplicate, which isn't about any specific buggy code, if that makes you happier. – Peter Cordes Feb 10 '22 at 14:24
  • Oh, I see you edited the title of this, to ask for something that's impossible with GCC. (Clang-trunk's new support for Intel-syntax asm has a pragma: `#pragma clang asm_dialect push "intel"` / ... pop - https://reviews.llvm.org/D113707). – Peter Cordes Feb 10 '22 at 14:32

0 Answers0