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
`