1 year ago

#156251

test-img

Wot

gcc inline assembly: how to use intel syntax temporarily with constraint 'm' under 'gcc -masm=att'

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

gcc

x86

inline-assembly

intel-syntax

0 Answers

Your Answer

Accepted video resources