【问题标题】:external assembly file in visual studioVisual Studio 中的外部程序集文件
【发布时间】:2016-02-18 12:05:04
【问题描述】:

我搜索并发现我无法在 Visual Studio 的 x64 中使用 __asm。相反,我必须使用外部程序集文件。

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

如何编译它们?

你能一步一步解释吗?

【问题讨论】:

    标签: visual-studio assembly x86 x86-64 masm


    【解决方案1】:

    如何在 Visual Studio 中使用 x64 程序集文件构建混合源 x64 项目

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

    2) 在下一个窗口中选择Win 32 Console Application

    3) 你得到一个确认。点击Next >

    4) 在下一个窗口中,您可以接受默认设置。点击Finish

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

    6) 在下一个窗口中勾选masm(.targets,.props) 并点击OK

    7) 选择Build - Configuration Manager...

    8) 将 Active solution platform 更改为 x64

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

    10) 在下一个窗口中选择 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 运行它。

    应用程序将在新窗口中打开。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2018-12-03
    • 2021-05-13
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多