【问题标题】:How do I resolve error C2059: syntax error : '__asm' in Visual C++ 2010 Express如何解决错误 C2059:语法错误:Visual C++ 2010 Express 中的“__asm”
【发布时间】:2012-07-19 07:46:19
【问题描述】:

包含内联 asm 代码的文件格式为 xyz.c,我使用的是 Visual C++ 2010 Express IDE。我收到标题中提到的错误。任何帮助表示赞赏!谢谢!

我的代码大致是这样的。

#include "xyz.h"

/*
; Multi-line comments
;
*/

__asm{
    Assembly code
}
 /*
; Multi-line comments
;
*/
.
.
.

__asm{
    Assembly code
}
 /*
; Multi-line comments
;
*/

__asm{
    Assembly code
}

【问题讨论】:

  • 您是否要为 x64 编译?
  • 我收回了。 VS Express 没有 64 位编译器。
  • VS Express 系列的编译器是有限的,也许它只是不支持__asm 扩展?

标签: c assembly visual-c++-2010-express


【解决方案1】:

您不能将 asm 代码(或任何其他代码)直接放入全局范围。你必须把它放在一个函数中。

void f()
{
    __asm {
        Some code
    }
}

【讨论】:

  • 我不清楚函数“f”将在哪里被调用..你能更详细地解释一下吗(可能用一些示例骨架代码我怎么能做到这一点?)?万分感谢! :)
  • 在 C 中,所有代码都必须在函数内部。它是常规的 C 代码还是 asm 代码都没有关系。 f() 函数可以像任何其他函数一样被调用。
【解决方案2】:

这个例子对我有用:

#include <windows.h>
#include <iostream>

using namespace std;

int Add(int x, int y){
    asm(
          "addl %1, %0"
          : "=r"(x)
          : "m"(y), "0"(x)
    );
return x;
}


int main ()
{
    int x,y;

    cout<<"Enter first number\n";
    cin>>x; //enter first number
    cout<<"Enter second number\n";
    cin>>y; //enter second number

    cout<<Add(x,y)<<endl;

return 0;

}

您可以看到不同的语法。但是 asm{} 或 __asm__{} 或任何带有 {} 的东西都不适用于 minGW,显然只能用于 Visual Studio。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多