您的位置:首页 > 编程语言 > VB

昨晚用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
/*===============================================================================================================*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: