windows 在 Visual Studio 中编译程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4548763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling assembly in Visual Studio
提问by bobobobo
How do you compile assembly code using Visual Studio?
如何使用 Visual Studio 编译汇编代码?
I want to compile and run an assembly source file in Visual Studio 2010.
我想在Visual Studio 2010 中编译和运行程序集源文件。
I've created a Visual C++ project and inserted some assembly code in a file code.asm
:
我创建了一个 Visual C++ 项目并在文件中插入了一些汇编代码code.asm
:
.586 ;Target processor. Use instructions for Pentium class machines
.MODEL FLAT, C ;Use the flat memory model. Use C calling conventions
.STACK ;Define a stack segment of 1KB (Not required for this example)
.DATA ;Create a near data segment. Local variables are declared after
;this directive (Not required for this example)
.CODE ;Indicates the start of a code segment.
clear PROC
xor eax, eax
xor ebx, ebx
ret
clear ENDP
END
However the problem is when you try and compile this, you get:
然而问题是当你尝试编译它时,你会得到:
LINK : error LNK2001: unresolved external symbol _mainCRTStartup
I did go and enable the build customization masm.targets
(right click project > Build Customizations..), but to no avail.
我确实去了并启用了构建自定义masm.targets
(右键单击项目>构建自定义..),但无济于事。
回答by Hans Passant
Sounds to me like the custom build rules for .asm files isn't enabled. Right-click the project, Custom Build Rules, tick "Microsoft Macro Assembler". With the "END clear" directive and disabling incremental linking I'm getting a clean build.
听起来像 .asm 文件的自定义构建规则未启用。右键单击项目,自定义构建规则,勾选“Microsoft Macro Assembler”。使用“END clear”指令并禁用增量链接,我得到了一个干净的构建。
It's different starting from VS2010:
从VS2010开始就不一样了:
- Right-click Project, Build customizations, tick "masm".
- Right-click the
.asm
file, Properties, change Item Typeto "Microsoft Macro Assembler".
- 右键单击Project,Build customs,勾选“ masm”。
- 右键单击
.asm
文件Properties,将Item Type更改为“ Microsoft Macro Assembler”。
回答by bobobobo
Command line:
命令行:
Compile the code with:
使用以下代码编译代码:
ml /c /Cx /coff code.asm
You get code.obj as the output.
你得到 code.obj 作为输出。
Link with:
链接:
link code.obj /SUBSYSTEM:console /out:go.exe /entry:clear
You can now run go.exe.
您现在可以运行 go.exe。
Alternatively, do it all in one go with:
或者,一次性完成所有操作:
ml /Cx /coff code.asm /link /SUBSYSTEM:console /link /entry:clear
Visual Studio (not solved)
Visual Studio(未解决)
回答by Martin v. L?wis
Visual Studio includes the MASMmacro assembler. Smaller fragments of assembler code are often written in inline assemblyin a C or C++ program.
Visual Studio 包括MASM宏汇编器。较小的汇编代码片段通常在 C 或 C++ 程序中用内联汇编编写。
To integrate an assembler file in a Visual Studio project, create a regular C/C++ project (command line or GUI), and just add a file ending in .asm
to the list of source files.
要将汇编程序文件集成到 Visual Studio 项目中,请创建一个常规 C/C++ 项目(命令行或 GUI),然后将一个以 结尾的文件添加.asm
到源文件列表中。
To specify clear as the entry point, follow these instructions:
要将 clear 指定为入口点,请按照以下说明操作:
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Advanced property page.
Modify the Entry Point property.
打开项目的属性页对话框。有关详细信息,请参阅设置 Visual C++ 项目属性。
单击链接器文件夹。
单击高级属性页。
修改入口点属性。
(It was taken from the Visual Studio documentation.)
(它取自Visual Studio 文档。)
I can confirm Hans Passant's instruction. In addition, according to this article, if you first add the "build customizations" masm checkbox, and then add the file, it will automatically be recognized as an assembler file. Furthermore, not specifying the entry point name in the END directive, but instead specifying it in the project settings also works for me.
我可以确认 Hans Passant 的指示。另外,根据这篇文章,如果您先添加“构建自定义”masm 复选框,然后添加文件,它会自动识别为汇编程序文件。此外,不在 END 指令中指定入口点名称,而是在项目设置中指定它也适用于我。
回答by sailfish009
here is how to compile nasm assembly source code with vs20xx:
下面是如何使用 vs20xx 编译 nasm 汇编源代码:
"Excluded From Build" to "No"
"Item Type" to "Custom Build Tool"
Hit Apply
Custom Build Tool -> General -> Command Line:
c:\nasm\nasm -f win64 my_asm.asm
Custom Build Tool -> General -> Outputs:
my_asm.obj
call the function like this:
extern "C" int foo(void); // written in assembly!
“从构建中排除”到“否”
“项目类型”到“自定义构建工具”
点击应用
自定义构建工具 -> 常规 -> 命令行:
c:\nasm\nasm -f win64 my_asm.asm
自定义构建工具 -> 常规 -> 输出:
my_asm.obj
像这样调用函数:
extern "C" int foo(void); // 用汇编编写!
https://www.cs.uaf.edu/2017/fall/cs301/reference/nasm_vs/
https://www.cs.uaf.edu/2017/fall/cs301/reference/nasm_vs/
nasm tutorial:
nasm教程:
回答by Madhur Ahuja
The problem is that your assembly code is just a function. To compile and link, you need to have a start procedure just like Main
in C/C++. You can specify the start symbol by specifying in your END
directive. Like:
问题是你的汇编代码只是一个函数。要编译和链接,您需要有一个启动过程,就像Main
在 C/C++ 中一样。您可以通过在END
指令中指定来指定开始符号。喜欢:
END clear
Or if you want, you can link the .obj file generated with the C/C++ generated .obj one.
或者,如果需要,您可以将生成的 .obj 文件与 C/C++ 生成的 .obj 文件链接起来。