汇编编程 - WinAsm 与 Visual Studio 2017 [英] Assembly programming - WinAsm vs Visual Studio 2017

查看:32
本文介绍了汇编编程 - WinAsm 与 Visual Studio 2017的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是来问你一些关于 VS2017 的.
过去,我曾将 WinAsm 用于 MASM,但从未遇到过问题.

I'm here to ask you some stuff about VS2017.
In the past I had used WinAsm for MASM and I never got problems with it.

但是,当我尝试在 VS2017 中使用 MASM 做某事时,我总是会遇到问题和其他东西...
我已经在整个互联网上检查了如何为 MASM 设置 VS",但没有任何帮助,因为我总是遇到麻烦......

However, when I'm trying to do something with MASM in VS2017, I always gonna get problems and stuff...
I've checked the whole internet about "how to set up VS for MASM", but nothing has helped me as I'm always getting troubles...

有没有什么方法可以让 Visual Studio 2017 for MASM32/64bit 没有任何头痛?

Is there any way to use Visual Studio 2017 for MASM32/64bit without any kind of headache?

谁能给我设置 VS2017 进行汇编编程的终极指南?

Can someone give me the ultimate guide to set up VS2017 for assembly programming?

非常感谢您,对于我的英语不好表示抱歉.

Thanks you very much and sorry for my weak english.

推荐答案

如何使用独立的 x64/x86 程序集文件构建 x64/x86 项目

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

1) Start Visual Studio (Community) 2017 and choose FILE - New - Project.

2) 在下一个窗口中选择Empty Project.

2) In the next window choose Empty Project.

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

3) Make sure, that the project is highlighted in the Solution Explorer and and choose PROJECT - Build Customizations....

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

4) In the next window tick masm(.targets,.props) and click on OK.

5) 从菜单中选择 PROJECT - Add New Item.

5) Choose PROJECT - Add New Item from the menu.

6) 在下一个窗口中选择 C++File(.cpp) 和 - IMPORTANT! - 给它一个带有 .asm 扩展.点击添加.

6) In the next window choose C++File(.cpp) and - IMPORTANT! - give it a name with an .asm extension. Click on Add.

7) 现在您可以用内容填充文件了.

7) Now you can fill the file with content.

Source.asm:

EXTERN GetStdHandle : PROC
EXTERN WriteFile    : PROC
EXTERN ExitProcess  : PROC

.DATA?
    hFile           QWORD ?
    BytesWritten    DWORD ?

.DATA
    hello   BYTE 'Hello world!', 13, 10

.CODE
main PROC
    ; https://blogs.msdn.microsoft.com/oldnewthing/20160623-00/?p=93735
    sub rsp, 40                 ; Shadow space (4 * 8) & 1 parameter (8 bytes)
    ; https://docs.microsoft.com/en-us/cpp/build/stack-allocation
    and spl, -16                ; Align to 16

    ; https://msdn.microsoft.com/library/windows/desktop/ms683231.aspx
    mov ecx, -11                ; DWORD         nStdHandle = STD_OUTPUT_HANDLE
    call GetStdHandle           ; Call WinApi
    mov hFile, rax              ; Save returned handle

    ; https://msdn.microsoft.com/library/windows/desktop/aa365747.aspx
    mov rcx, hFile              ; HANDLE        hFile (here: Stdout)
    lea rdx, hello              ; LPCVOID       lpBuffer
    lea r9, BytesWritten        ; LPDWORD       lpNumberOfBytesWritten
    mov r8d, LENGTHOF hello     ; DWORD         nNumberOfBytesToWrite
    mov qword ptr [rsp+32], 0   ; LPOVERLAPPED  lpOverlapped = NULL
    call WriteFile              ; Call WinAPI

exit:
    ; https://msdn.microsoft.com/library/windows/desktop/ms682658.aspx
    xor ecx, ecx                ; Set RCX to null for return value
    call ExitProcess            ; Call WinAPI to exit
main ENDP

end

这是一个 64 位控制台应用程序,从过程 main 开始.

This is a 64-bit Console application that starts at the procedure main.

8) 将 Solution Platforms 更改为 x64

8) Change the Solution Platforms to x64

9) 选择 PROJECT - Properties.

10) 在属性"窗口中,您必须完成两个链接器选项:

10) In the Properties window you have to complete two linker options:

  • 入口点:main
  • 子系统:控制台 (/SUBSYSTEM:CONSOLE)

在左侧选择Configuration Properties - Linker - All Options ,同时更改两个选项,然后单击OK.

Choose at the left side Configuration Properties - Linker - All Options , change both options at once and click OK.

11) 使用 CTRL-F5 构建并运行 .exe.应用程序将在新窗口中打开.

11) Build and run the .exe with CTRL-F5. The application will be opened in a new window.

现在用 32 位控制台应用程序覆盖 Source.asm:

Now overwrite Source.asm with a 32-bit Console application:

.MODEL flat, stdcall

; https://docs.microsoft.com/en-us/cpp/assembler/masm/proto
GetStdHandle PROTO STDCALL,     ; https://docs.microsoft.com/en-us/windows/console/getstdhandle
    nStdHandle: SDWORD
WriteFile PROTO STDCALL,        ; https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile
    hFile: DWORD,                       ; output handle
    lpBuffer: PTR BYTE,                 ; pointer to buffer
    nNumberOfBytesToWrite: DWORD,       ; size of buffer
    lpNumberOfBytesWritten: PTR DWORD,  ; num bytes written
    lpOverlapped: PTR DWORD             ; ptr to asynchronous info
ExitProcess PROTO STDCALL,      ; https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-exitprocess
    dwExitCode: DWORD                   ; return code

.DATA                   ; https://docs.microsoft.com/en-us/cpp/assembler/masm/dot-data
    Hallo db "Hello world!",13,10

.DATA?                  ; https://docs.microsoft.com/en-us/cpp/assembler/masm/dot-data-q
    lpNrOfChars dd ?

.CODE                   ; https://docs.microsoft.com/en-us/cpp/assembler/masm/dot-code
main PROC               ; docs.microsoft.com/en-us/cpp/assembler/masm/proc
    invoke GetStdHandle, -11            ; -> StdOut-Handle into EAX
    invoke WriteFile, eax, OFFSET Hallo, LENGTHOF Hallo, OFFSET lpNrOfChars, 0
    invoke ExitProcess, 0
main ENDP

END main                ; https://docs.microsoft.com/en-us/cpp/assembler/masm/end-masm

Solution Platforms改为x86(上面的第8号)并用SubSystem:Console (/SUBSYSTEM:CONSOLE)(以上第 10 条).您不得设置入口点,因为 ml32 需要 END 指令(END main)之后的入口点.使用 CTRL-F5 构建并运行 .exe.

Change the Solution Platforms to x86 (No. 8 above) and complete the project properties with SubSystem: Console (/SUBSYSTEM:CONSOLE) (No. 10 above). You must not set the Entry point, because ml32 expects the entry point after the END directive (END main). Build and run the .exe with CTRL-F5.

这篇关于汇编编程 - WinAsm 与 Visual Studio 2017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆