vs2022环境x64 C/C++和汇编混编
vs64位程序不支持__asm内嵌汇编,需要单独编写汇编源文件
示例如下
1、新建空的win32项目,新建main.cpp,示例代码如下
#include <Windows.h>
extern "C" void __stdcall asm_func(const char* lpText);
extern "C" UINT GetMsgBoxType()
{
return MB_YESNOCANCEL;
}
int main()
{
asm_func("Hello world!");
return 0;
}2、新建asm64.asm汇编源文件,示例代码如下
.data msgCaption db 'Message box text',0 .code align 16 extern GetMsgBoxType : proc extern MessageBoxA : proc extern __imp_MessageBoxA : qword asm_func proc ; RCX = address for the string for the message box sub rsp, 28h ; shadow stack only [n]8 size lea rdx, [msgCaption] mov r8, rcx call GetMsgBoxType mov r9, rax xor rcx, rcx ;call [__imp_MessageBoxA] call MessageBoxA add rsp, 28h ; restoring shadow stack ret asm_func endp end
3、编译器配置,选择x64,debug或者release都可以,
3.1 右键项目 --> 生成依赖项 --> 生成自定义 --> 勾选masm
3.2 右键汇编源文件 --> 属性 --> 常规 --> 项类型 --> Microsoft Macro Assembier
4、直接生成即可
原文地址:https://blog.csdn.net/qq_29176323/article/details/129145326