C语言
主页 > 软件编程 > C语言 >

vs2022 x64 C/C++和汇编混编(案例代码)

2023-02-26 | 佚名 | 点击:

vs2022环境x64 C/C++和汇编混编

vs64位程序不支持__asm内嵌汇编,需要单独编写汇编源文件

示例如下

1、新建空的win32项目,新建main.cpp,示例代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#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汇编源文件,示例代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

.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
相关文章
最新更新