Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline assembly with intel syntax using LLVM: Unknown token in expression

When compiling this code with Apple LLVM 4.1 in Xcode I get an error:

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int a = 1;
    printf("a = %d\n", a);

    asm volatile(".intel_syntax noprefix;"
        "mov [%0], 2;"
        :
        : "r" (&a)
        );

    printf("a = %d\n", a);
    return 0;
}

The error is Unknown token in expression.

If I use AT&T syntax it works fine:

asm volatile("movl $0x2, (%0);"
                 :
                 : "r" (&a)
                 : "memory"
                 );

What is wrong with the first code?

like image 433
Tyilo Avatar asked Dec 30 '12 16:12

Tyilo


1 Answers

It looks like the compiler is translating %0 to %reg (%rcx on my machine) and the assembler does not like the % (as it is in intel mode).

I don't know if it's possible to mix the automatic register allocation feature (extended asm) with the intel syntax, as I've not seen any example yet.

Good documentation about gcc inline assembly is usually hard to come by, and clang states in its documentation that it's mostly compatible with gcc in this area...

like image 50
lbonn Avatar answered Sep 30 '22 16:09

lbonn