views:

1609

answers:

2

I'm starting with assembler under Linux. I have saved the following code as testasm.c
and compiled it with: gcc testasm.c -otestasm
The compiler replies: "impossible constraint in ‘asm’".

#include <stdio.h>
int main(void)
{
    int foo=10,bar=15;

    __asm__ __volatile__ ("addl %%ebx,%%eax"
     : "=eax"(foo) 
     : "eax"(foo), "ebx"(bar) 
     : "eax" 
    );

    printf("foo = %d", foo);

    return 0;
}

How can I resolve this problem? (I've copied the example from here.)

Debian Lenny, kernel 2.6.26-2-amd64
gcc version 4.3.2 (Debian 4.3.2-1.1)

Resolution:
See the accepted answer - it seems the 'modified' clause is not supported any more.

+3  A: 
__asm__ __volatile__ ("addl %%ebx,%%eax" : "=a"(foo) : "a"(foo), "b"(bar));

seems to work. I believe that the syntax for register constraints changed at some point, but it's not terribly well documented. I find it easier to write raw assembly and avoid the hassle.

Stephen Canon
slashmais
The modified clause still works; I think the problem is that "output" implicitly specifies "modified" as well, so having eax represented in both field was causing the issue.
Stephen Canon
+1  A: 

If one wants to use multiline, then this will also work..

  __asm__ __volatile__ (
        "addl %%ebx,%%eax; \
         addl %%eax, %%eax;" 
        : "=a"(foo) 
        : "a"(foo), "b"(bar)
    );

'\' should be added for the compiler to accept a multiline string (the instructions).

Rahul SB