GCC内联汇编错误:错误:垃圾`(%ESP)'前pression后 [英] GCC inline assembly error: Error: junk `(%esp)' after expression

查看:1599
本文介绍了GCC内联汇编错误:错误:垃圾`(%ESP)'前pression后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC内联汇编错误:错误:垃圾`(%ESP)'后的前pression

GCC inline assembly error: Error: junk `(%esp)' after expression

我学习gcc的内嵌汇编。我的环境是Win 7的32位,MinGW的-GCC 4.6.1。

I'm studying gcc inline assembly. My environment is Win 7 32bit, mingw-gcc 4.6.1.

我有一个关于M约束问题。这里是我的C函数code:

I have got a problem about the 'm' constraint. Here is my c function code:

static int asm_test(int a, int b)
{

    int c = 0;
    __asm__ __volatile__(".intel_syntax\n"
            "mov eax, %1\n" //error
            "mov edx, %2\n" //error
            "add eax, edx\n"
            "mov %0, eax\n" //error
            ".att_syntax"
            :"=m"(c)\
            :"m"(a),"m"(b)\
            :"eax","edx"
            );
    return c;
}

有关AT& T公司code,它是这样的:

For at&t code, it is like this:

static int asm_test(int a, int b)
{

    int c = 0;
    __asm__ __volatile__(
            "movl %1, $eax\n" //error
            "movl %2, $edx\n" //error
            "addl $edx, $eax\n"
            "movl $eax, %0\n" //error
            :"=m"(c)\
            :"m"(a),"m"(b)\
            :"eax","edx"
            );
    return c;
}

有关每个操作的输入/输出操作数的三条线,GCC编译生成时的错误,这样写的:

For each of the three lines which operate input/output operands, gcc generate an error when compiling, read like this:

C:\\用户\\ farta \\应用程序数据\\本地的\\ Temp \\ cc99HxYj.s:22:错误:垃圾`(%ESP)'前pression后

C:\Users\farta\AppData\Local\Temp\cc99HxYj.s:22: Error: junk `(%esp)' after expression

如果我使用'R'的输入/输出约束时,code会工作。但我不明白为什么它的工作原理,哪些错误主张。谁能告诉我?据我所知,'M'就是告诉GCC不分配寄存器,而是直接在内存中访问他们,如果内联汇编code尝试访问输入/输出操作数。这是正确的?

If i use 'r' for input/output constraint, the code will work. But I cannot understand why it works and what the error stands for. Can anyone tell me? As far as I know 'm' is just telling gcc not to allocate registers but directly access them in memory if inline asm code try to access input/output operands. Is this correct?

非常感谢。

推荐答案

这里的问题是海湾合作委员会产生AT& T公司的语法构建%0 %1 %2 。如果你看一下生成的汇编,它看起来像:

The problem here is the GCC generates AT&T syntax construct for %0, %1 and %2. If you look at the generated assembly, it looks like:

.intel_syntax
mov eax, 8(%ebp)
mov edx, 12(%ebp)
add eax, edx
mov -4(%ebp), eax

这不是有效的Intel语法

which is not a valid Intel syntax.

一般情况下,你不需要在内嵌汇编明确的加载/存储操作,包括 - 只需指定寄存器约束,编译器会自行生成负载/存储。这样做,即使你的变量(参数,当地人)不驻留在内存中,但在注册您的code仍然是正确的优点 - 不同于如果你明确地把内存负载/存储在那里的​​情况。

Generally, you don't need to include in the inline assembly explicit load/store operation - just specify register constraint and the compiler will generate loads/stores by itself. This has the advantage that even if your variables (parameters, locals) do not reside in memory at all, but are in registers your code will still be correct - unlike in the case if you explicitly put memory load/stores there.

有关你的榜样,请尝试以下code,看组件( GCC -S ),并通知编译器将如何执行从参数区域移动(如:堆叠86)全部由自己。

For your example, try the following code, look at the assembly (gcc -S) and notice how the compiler will perform moves from argument area (e.g. stack on x86) all by itself.

int asm_test(int a, int b)
{
  __asm__ __volatile__ (
                        ".intel_syntax\n"
                        "add %0, %1 \n"
                        ".att_syntax \n"
                        :"+r"(a)
                        :"r"(b));
  return a;

}

这篇关于GCC内联汇编错误:错误:垃圾`(%ESP)'前pression后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆