vs2022 x64 C/C++和匯編混編(案例代碼)

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、直接生成即可

在這裡插入圖片描述

到此這篇關於vs2022 x64 C/C++和匯編混編的文章就介紹到這瞭,更多相關vs2022 x64 匯編內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: