0_x32_x64 VS 汇编和C混合开发配置

1 x32汇编开发

32位的时候注意 ,无论是 直接编写汇编函数 还是内联汇编,都是支持的

#include <stdio.h>
#include<windows.h>

const char* testBuffer = { "i am a big" };
const char * formatPrint = { "结果是 : %d" };
void testAsm32(int x1,int x2)
 //汇编函数
{
	__asm
	{
		int 3
		;;;; push ebp 注意 默认函数(除非是 nake )已经开了栈帧  不用去管理
		;;;; mov ebp, esp
		pushad
		mov eax, x1
		add eax, x2
		push eax
		push formatPrint
		call printf
		add esp, 8;			printf 是cdecl 调用者平栈
		popad
		;;;; pop ebp
	}
	return;
}

int main()
{
	testAsm32(1,2);
	//内联汇编
    __asm
	{
		push ebx
		mov ebx,testBuffer
		push ebx
		call printf
		add esp,4
		pop ebx
	}
	system("pause");
}

2 x64汇编开发 (c\cpp和asm 相互调用)

注意,vs 64bit 不支持内联汇编,需要在asm 文件中编写汇编函数 然后在c语言中调用;也可以在汇编中调用c\cpp文件中的函数。

配置环境:

  • 设置项目依赖项:

把下面这个勾选上:(注意 在此操作之前的 asm文件,不然之前的asm文件还是有问题)
在这里插入图片描述
在这里插入图片描述

.cpp文件(c++函数注意申明extern “C”,且想在asm文件中调用 c\cpp 自定义函数,需要将该函数生命位 _cdecl 调用方式)

#include<stdio.h>
#include<windows.h>
extern "C" void _cdecl testFunc();
extern "C" void _cdecl testPrint()
{
	printf("hello iam fine\n");
}
int main()
{
	testFunc();
	printf("hello world\n");
	system("pause");
}

.asm文件


;!64 位没有 .model 宏指令,无法设置内存模型和生成的代码调用

extern testPrint:proc

PUBLIC testFunc ;申明函数

_TEXT SEGMENT
testFunc proc
	int 3			
	push rax
	mov rax,testPrint
	call rax
	pop rax
	ret
testFunc endp
END

`

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值