VC中使用内联汇编(转载)

1.使用C中的局部变量和全局变量。

1
2
3
4
5
6
7
8
9
10
	int a = 5,b=6,c;
	__asm
	{
		xor edx,edx	;edx=0
		add edx,a	;edx +=a
		add edx,b	;edx +=b
		add edx,g_nC;使用全局变量
		mov c,edx	;c=edx
	}
	printf("UseParameter : %d\n",c);

2.调用C中函数,stdcall方式和cdecl方式,pascal方式在控制台下没整出来,Delphi也不会就算了,和stdcall差不多,就是入栈方式不一样。

cdecl:从右到左入栈,由调用者清栈,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
__cdecl int cdecl_sub(int a,int b)
{
return a-b;
}
 
void UseCdecl()
{
int nRet=0;
__asm
{
mov eax,6
mov ebx,5
push ebx
push eax
call cdecl_sub
add esp,8        ;调用者维护栈平衡
mov nRet,eax
}
printf("UseCdecl : %d\n",nRet);
}

stdcall:从右到左入栈,函数自己清栈。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
__stdcall int stdcall_sub(int a,int b)
{
	return a-b;
}
void UseStdCall()
{
	int nRet=0;
	__asm
	{
		mov eax,6
		mov ebx,5
		push ebx
		push eax
		call stdcall_sub
		mov nRet,eax
	}
	printf("UseStdCall : %d\n",nRet);
}

嘻嘻,就演示这么多,其他的调用方式也应该类似。

源码下载:

  C_And_ASM (4.3 KiB, 527 hits)

posted on 2012-02-15 11:20  carekee  阅读(541)  评论(0编辑  收藏  举报