English 中文(简体)
短链氯化石蜡的组装错误“`asm'”中的易碎限制”
原标题:Linux assembler error "impossible constraint in ‘asm’"
  • 时间:2009-09-25 17:11:33
  •  标签:

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.

最佳回答
__asm__ __volatile__ ("addl %%ebx,%%eax" : "=a"(foo) : "a"(foo), "b"(bar));

似乎在工作。 我认为,在某个时候登记限制的辛迪加发生了变化,但记录不甚可怕。 我认为,撰写原材料和避免 has。

问题回答

这些制约因素是single letter(可能带有外装),而且你可以具体说明几种备选办法(即中型歌剧或注册为“ir”)。 因此,限制“经济”是指限制“e”(签名的32-bit integer常数)、“a”(注册的eax)或“x”(任何SSE登记册)。 这与“e”一词的含义......和output显然没有意义。 此外,如果某些歌剧(在这种情况下是投入和产出)必须同另一剧一样,你会以一些制约因素提及。 没有必要说“轴心”会被束缚,而是产出。 您可以以百分数、百分数表示,......无需使用明确的登记名称。 因此,按照欧佩组织的设想,守则的正确版本是:

#include <stdio.h>

int main(void)
{
    int foo=10, bar=15;

    __asm__ __volatile__ (
        "addl %2, %0"
        : "=a" (foo)
        : "0" (foo), "b" (bar)
    );

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

    return 0;
}

更好的解决办法是,允许2%为任何,而登记为%(因为x86允许,但必须检查你的机器手册):

#include <stdio.h>

int main(void)
{
    int foo=10, bar=15;

    __asm__ __volatile__ (
        "addl %2, %0"
        : "=r" (foo)
        : "0" (foo), "g" (bar)
    );

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

    return 0;
}

如果有人想使用多线,这也会奏效。

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

应为汇编者添加内容,以接受多线插图(指示)。





相关问题
热门标签