即使在启用Microsoft宏汇编程序后,Visual Studio 2013中的程序集也无法构建

时间:2015-01-07 20:43:47

标签: assembly visual-studio-2013

我试图运行一个非常基本的汇编文件来做一些数学运算并打印输出,没有什么挑战性。我已经按照here等地方给出的步骤进行了操作,但我的构建仍然失败,并且每一行都有关于语法的错误。错误如:

1>c:\users\damian\documents\visual studio 2013\projects\test345\test345\source.asm(22): error C2061: syntax error : identifier 'dword'
1>c:\users\damian\documents\visual studio 2013\projects\test345\test345\source.asm(24): error C2061: syntax error : identifier 'add'
1>c:\users\damian\documents\visual studio 2013\projects\test345\test345\source.asm(27): error C2061: syntax error : identifier 'pop'
1>c:\users\damian\documents\visual studio 2013\projects\test345\test345\source.asm(12): error C2061: syntax error : identifier 'main'

我尝试运行的代码是here。我尝试过从cpp转换到c编译,我尝试在链接器中设置一个入口点,并且我尝试右键单击项目 - >构建依赖关系 - >构建自定义和检查masm,但没有一个完全没有任何区别。还有其他我想念的东西吗?

1 个答案:

答案 0 :(得分:1)

您尝试汇编的代码使用NASM语法。您需要将Visual Studio配置为使用NASM。

1)安装NASM并将其添加到PATH环境变量的路径中 2)右键单击您的asm文件,然后选择Properties-> General,然后为Item Type字段选择Custom Build Tool
3)单击“应用” 4)在命令行字段的Custom Build Tool页面集nasm -f win32 -o "$(ProjectDir)$(IntDir)%(Filename).obj" "%(FullPath)"上 5)将Outputs字段设置为$(IntermediateOutputPath)%(Filename).obj

这将使NASM将您的汇编源文件组装成Visual Studio兼容的目标文件 我们还没有完成,你需要对汇编文件进行一些更改,然后才能使用MSVC的链接器链接它。
1)MSVC的链接器要求您的函数以下划线开头,因此main变为_main
2)声明导入API时的命名约定也不同。因此extern printf成为extern __imp__printf
3)对导入的API的调用指令也不同。 call printf变为call [__imp__printf]。 printf的地址将存储在一个导入表条目中,我们的指令取消引用它以找到printf的地址并调用它。

尝试将其链接也会导致错误(error LNK2001: unresolved external symbol _mainCRTStartup)。我击败它的方式是包含一个带有虚函数的c文件,它不执行任何操作。这样,CRT启动存根就会被链接起来。 (如果有更好的方法,请在评论中提出建议。)

相关问题