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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:55:35  来源:igfitidea点击:

Compiling assembly in Visual Studio

windowsvisual-studio-2010assembly

提问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开始就不一样了

  1. Right-click Project, Build customizations, tick "masm".
  2. Right-click the .asmfile, Properties, change Item Typeto "Microsoft Macro Assembler".
  1. 右键单击ProjectBuild customs,勾选“ masm”。
  2. 右键单击.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 .asmto the list of source files.

要将汇编程序文件集成到 Visual Studio 项目中,请创建一个常规 C/C++ 项目(命令行或 GUI),然后将一个以 结尾的文件添加.asm到源文件列表中。

To specify clear as the entry point, follow these instructions:

要将 clear 指定为入口点,请按照以下说明操作:

  1. Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.

  2. Click the Linker folder.

  3. Click the Advanced property page.

  4. Modify the Entry Point property.

  1. 打开项目的属性页对话框。有关详细信息,请参阅设置 Visual C++ 项目属性。

  2. 单击链接器文件夹。

  3. 单击高级属性页。

  4. 修改入口点属性。

(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 汇编源代码:

  1. "Excluded From Build" to "No"

  2. "Item Type" to "Custom Build Tool"

  3. Hit Apply

  4. Custom Build Tool -> General -> Command Line:

    c:\nasm\nasm -f win64 my_asm.asm

  5. Custom Build Tool -> General -> Outputs:

    my_asm.obj

  6. call the function like this:

    extern "C" int foo(void); // written in assembly!

  1. “从构建中排除”到“否”

  2. “项目类型”到“自定义构建工具”

  3. 点击应用

  4. 自定义构建工具 -> 常规 -> 命令行:

    c:\nasm\nasm -f win64 my_asm.asm

  5. 自定义构建工具 -> 常规 -> 输出:

    my_asm.obj

  6. 像这样调用函数:

    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教程:

http://cs.lmu.edu/~ray/notes/nasmtutorial/

http://cs.lmu.edu/~ray/notes/nasmtutorial/

回答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 Mainin C/C++. You can specify the start symbol by specifying in your ENDdirective. 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 文件链接起来。