昨晚用MASM寫了個內存比較的函數(DLL供VC/VB調用)
2007-01-20 12:30
381 查看
用MASM編寫的兩段內存比較函數。提供源碼及SDK和VB6示範代碼。不知哪位高手能打造個更高效的方法? :) 請send: stormworks@163.com, 共討論之. thanks very much.
; 兩段內存比較函數。
MMemCmpi proc uses esi edi ebx pDst:DWORD, pSrc:DWORD, nLength:DWORD
mov esi, DWORD ptr [pSrc]
mov edi, DWORD ptr [pDst]
; argument check
test esi, esi
jz @Exit
test edi, edi
jz @Exit
cld
mov ecx, DWORD ptr [nLength]
mov ebx, ecx
shr ecx, 2
repz cmpsd
JNZ @Exit
mov ecx, edx
and ecx, 3
repz cmpsb
JZ @Same
@Exit:
XOR EAX, EAX
NOT EAX
ret
@Same:
XOR EAX, EAX
ret
MMemCmpi endp
演示:如果pSrc,pDst兩段內存相同就返回0,否則為非零。如下測試代碼:
SDK 代碼:
/*===============================================================================================================*/
#include "windows.h"
#include "tchar.h"
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif
EXPORT BOOL MMemCmpi( LPVOID pDst, LPVOID pSrc, UINT nLength );
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
RECT rc1, rc2;
BOOL fResult = 0;
TCHAR szText[MAX_PATH] = TEXT("/0");
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
SetRect(&rc1, 5, 6, 7, 8);
SetRect(&rc2, 5, 6, 7, 8);
fResult = MMemCmpi( &rc1, &rc2, sizeof(rc1) );
wsprintf( szText, TEXT("test equal to: %i/n"), fResult );
MessageBox( NULL, szText, _T(""), MB_OK );
rc1.left = 200;
fResult = MMemCmpi( &rc1, &rc2, sizeof(rc1) );
wsprintf( szText, TEXT("not equal to: %i/n"), fResult );
MessageBox( NULL, szText, _T(""), MB_OK );
return 0;
}
/*===============================================================================================================*/
VB6代碼:
/*===============================================================================================================*/
'Rectangle
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function MMemCmpi Lib "Utils.dll" _
(ByVal pDst As Long, _
ByVal pSrc As Long, _
ByVal nLength As Long) As Long
public sub Main()
dim rc1 as RECT
dim rc2 as RECT
SetRect(rc1, 5, 6, 7, 8);
SetRect(rc2, 5, 6, 7, 8);
MsgBox "test the equal to: " & MMemCmpi( varptr(rc1), varptr(rc2), lenb(rc1) )
rc1.left = 200
MsgBox "test the not equal to: " & MMemCmpi( varptr(rc1), varptr(rc2), lenb(rc1) )
end sub
/*===============================================================================================================*/
; 兩段內存比較函數。
MMemCmpi proc uses esi edi ebx pDst:DWORD, pSrc:DWORD, nLength:DWORD
mov esi, DWORD ptr [pSrc]
mov edi, DWORD ptr [pDst]
; argument check
test esi, esi
jz @Exit
test edi, edi
jz @Exit
cld
mov ecx, DWORD ptr [nLength]
mov ebx, ecx
shr ecx, 2
repz cmpsd
JNZ @Exit
mov ecx, edx
and ecx, 3
repz cmpsb
JZ @Same
@Exit:
XOR EAX, EAX
NOT EAX
ret
@Same:
XOR EAX, EAX
ret
MMemCmpi endp
演示:如果pSrc,pDst兩段內存相同就返回0,否則為非零。如下測試代碼:
SDK 代碼:
/*===============================================================================================================*/
#include "windows.h"
#include "tchar.h"
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif
EXPORT BOOL MMemCmpi( LPVOID pDst, LPVOID pSrc, UINT nLength );
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
RECT rc1, rc2;
BOOL fResult = 0;
TCHAR szText[MAX_PATH] = TEXT("/0");
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
SetRect(&rc1, 5, 6, 7, 8);
SetRect(&rc2, 5, 6, 7, 8);
fResult = MMemCmpi( &rc1, &rc2, sizeof(rc1) );
wsprintf( szText, TEXT("test equal to: %i/n"), fResult );
MessageBox( NULL, szText, _T(""), MB_OK );
rc1.left = 200;
fResult = MMemCmpi( &rc1, &rc2, sizeof(rc1) );
wsprintf( szText, TEXT("not equal to: %i/n"), fResult );
MessageBox( NULL, szText, _T(""), MB_OK );
return 0;
}
/*===============================================================================================================*/
VB6代碼:
/*===============================================================================================================*/
'Rectangle
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function MMemCmpi Lib "Utils.dll" _
(ByVal pDst As Long, _
ByVal pSrc As Long, _
ByVal nLength As Long) As Long
public sub Main()
dim rc1 as RECT
dim rc2 as RECT
SetRect(rc1, 5, 6, 7, 8);
SetRect(rc2, 5, 6, 7, 8);
MsgBox "test the equal to: " & MMemCmpi( varptr(rc1), varptr(rc2), lenb(rc1) )
rc1.left = 200
MsgBox "test the not equal to: " & MMemCmpi( varptr(rc1), varptr(rc2), lenb(rc1) )
end sub
/*===============================================================================================================*/
相关文章推荐
- js存/讀取cookie函數
- expriment : 在MASM工程中使用Unicode字符串
- 使用MASM-标号、变量和数据结构(2)
- 昨晚研究了一下node如何实现三国杀
- [MASM]程序中可用的寄存器
- 开始使用MASM
- 重命名存儲過程、函數、視圖、觸發器請慎重
- 想趁现在尽可能多尽可能详细地记录下我昨晚的梦
- [转]WMSYS.WM_CONCAT 函數的用法
- 常用 win32函數 GetWindowText&HWMD GetDlgItem
- 用VS2010构建MASM的编程环境
- OpenCV2马拉松第12圈——直方图比較
- 现有一些开源ESB总线的比較
- AABB包围盒、OBB包围盒、包围球的比較
- masm下几种常见函数调用方式
- masm中list文件和宏的一些常用编译调试查看方法
- 关于昨晚的梦[2007-05-07 12:12:06]
- 64位win7使用debug(Masm所有子程序)的方法:
- Java NIO与IO的差别和比較
- 昨晚做了一个噩梦!吓死我啦!吓死我了