visual studio 中的外部程序集文件

external assembly file in visual studio

我搜索了一下,发现我无法在 visual studio 中使用 x64 中的 __asm。相反,我必须使用外部程序集文件。

如何将外部程序集文件添加到我的 win32 控制台项目?

如何编译它们?

你能一步一步解释一下吗?

如何在 Visual Studio:

中使用 x64 程序集文件构建混合源 x64 项目

1) 启动 Visual Studio(社区)2015 并选择 FILE - New - Project

2) 在接下来的window中选择Win 32 Console Application

3) 您会得到确认。单击 Next >

4) 在接下来的window中您可以接受默认设置。单击 Finish

5) 确保项目在解决方案资源管理器中突出显示,然后从菜单中选择 PROJECT - Build Customizations...

6) 在接下来的 window 中勾选 masm(.targets,.props) 然后点击 OK.

7) 选择Build - Configuration Manager...

8) 将 Active solution platform 更改为 x64

9) 创建 callee.asm: PROJECT - Add New Item.

10) 在下一个 window 中选择 C++File(.cpp) 和 - 重要! - 给它一个带有 .asm 扩展名的名称。单击 Add

10) 现在检查 .asm 文件是否具有正确的属性。在解决方案资源管理器中右键单击该文件并选择 Properties.

11) 在 属性 页面中,您至少应该看到:

Excluded From Build    (empty) or No
Item Type              Microsoft Macro Assembler

Command Line 下确保选择 ml64.exe 作为汇编器。

单击 OK

12) 现在可以用内容填充文件了。

ConsoleApplication1.cpp:

#include <iostream>
using namespace std;

extern "C" void hello_from_asm();

int main()
{
    cout << "Hello from CPP" << endl;
    hello_from_asm();
    return 0;
}

callee.asm:

PUBLIC hello_from_asm
EXTERN puts:PROC

.data

    hello1 db "Hello from ASM.",0

.code

hello_from_asm PROC
    push rbp
    mov rbp, rsp
    sub rsp, 32                 ; Shadow Space
    and spl, -16                ; Align stack at 16

    lea rcx, hello1
    call puts

    leave                       ; Restore stack (rsp) & frame pointer (rbp)
    ret
hello_from_asm ENDP

END

13) 构建 .exe

和 运行 它与 CTRL-F5.

应用程序将在新 window 中打开。