完全控制: __declspec(naked) int XXX(你的参数列表...) { __asm { push ebp;如果你用到了这些寄存器的任何一个,都要对其进行单独的保护 push ebx push esi push edi mov eax,结果;返回结果放eax,或edx:eax。根据不同的数据类型而言 pop edi pop esi pop ebx pop ebp ret 0 } } 局部插入: int XXX(int example你的参数列表...) { __asm { mov eax,example } return 0; }
use ASM and CPP together: 1. create an ASM file ;;;;;;;;;;;;; asmsrc.asm: .386 .model flat, stdcall option casemap :none .codemyasmproc proc dw1:DWORD,dw2:DWORD mov eax,dw1 add eax,dw2 ret myasmproc endp end ;;;;;;;;;;;;end of asmsrc.asm2. create a VC project name: useasm, type console application, A "Hello World" application3. move the asm file to your project directory, then in VC project menu->Add to Project...->Files... Files of type change to "all files", then you can select the asmsrc.asm, and click OK4.in workspace window, FileView tab, select asmsrc.asm, right click to select "settings..." menu, custom build tab, put the following in commands edit box : d:\masm32\bin\ml.exe /nologo /coff /Zf /c /Sa $(InputName).asm put the following in Outputs edit box: $(InputName).obj5.edit your useasm.cpp as the following: //////////////////////useasm.cpp/////////////////////////////// #include "stdafx.h" #include <windows.h> extern "C" int __stdcall myasmproc(DWORD d1,DWORD d2); int main(int argc, char* argv[]) { printf("test of using cpp and asm together, if it works, it is done by masterz,otherwise I don't know who write this^_^\n"); int ret=myasmproc(22,33); printf("ASM result:%d\n",ret); return 0; }//////////////////////end of useasm.cpp///////////////////////////////6. build the project and run it, it works.notes: I assume you have installed masm32V8(you can get it from http://www.movsd.com/masmdl.htm) at D:\masm32
__declspec(naked)
int XXX(你的参数列表...)
{
__asm
{
push ebp;如果你用到了这些寄存器的任何一个,都要对其进行单独的保护
push ebx
push esi
push edi mov eax,结果;返回结果放eax,或edx:eax。根据不同的数据类型而言
pop edi
pop esi
pop ebx
pop ebp
ret 0
}
}
局部插入:
int XXX(int example你的参数列表...)
{
__asm
{
mov eax,example
}
return 0;
}
比如:
push ax //正确
PUSH AX //错误
为了效率?你用asm写的挣回来的那一点点效率,被与整体相比真是太可怜了。
为了cool? 别闹了,masm的.if .while invoke等语法在这里根本不能用。上面的例子你也看到了,没用一次还要pushpush,popopoopopopop......应用程序说到底是调用api,汇编,就像过时的老教授,靠边点。
为了底层?好像是个理由,不过写wdm的话,汇编还是没有用。
个人认为,pc 上的编程,vxd上用汇编还是有点用的。再不然就是写操作系统了。
{
汇编语句
}
1. create an ASM file
;;;;;;;;;;;;; asmsrc.asm:
.386
.model flat, stdcall
option casemap :none
.codemyasmproc proc dw1:DWORD,dw2:DWORD
mov eax,dw1
add eax,dw2
ret
myasmproc endp
end
;;;;;;;;;;;;end of asmsrc.asm2. create a VC project name: useasm, type console application, A "Hello World" application3. move the asm file to your project directory, then in VC project menu->Add to Project...->Files...
Files of type change to "all files", then you can select the asmsrc.asm, and click OK4.in workspace window, FileView tab, select asmsrc.asm, right click to select "settings..." menu, custom build tab, put the following in commands edit box :
d:\masm32\bin\ml.exe /nologo /coff /Zf /c /Sa $(InputName).asm
put the following in Outputs edit box:
$(InputName).obj5.edit your useasm.cpp as the following:
//////////////////////useasm.cpp///////////////////////////////
#include "stdafx.h"
#include <windows.h>
extern "C" int __stdcall myasmproc(DWORD d1,DWORD d2);
int main(int argc, char* argv[])
{
printf("test of using cpp and asm together, if it works, it is done by masterz,otherwise I don't know who write this^_^\n");
int ret=myasmproc(22,33);
printf("ASM result:%d\n",ret);
return 0;
}//////////////////////end of useasm.cpp///////////////////////////////6. build the project and run it, it works.notes: I assume you have installed masm32V8(you can get it from http://www.movsd.com/masmdl.htm) at D:\masm32