just a question about some custom hash algorithm.

i made 2 complete different Proc's
one normal and a version that is using mmx
i should be simple, but this algorithm is driving me nuts @!#  :shock:

mybe a asm diehard can point me to the right direction ?
i would be very happy to see what i am doing wrong   :roll:

the output from both the proc's gives me 8E244C8179D31E12
but it should be                                      8E245D9679D31E12

source html page with the sample avi file is
http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes

Thanks in advance for anyone who want to look at it.
jpam

HashFormat1  db "%.8X%.8X",0
HashFormat2  db "%I64x",0

Calc_Hash proc uses esi ebx edx pFile:dword

LOCAL hFile:dword, fSize:dword, NBR:dword, pMem:dword
LOCAL Result[8]:byte

invoke CreateFile,pFile,GENERIC_ALL,0,0,OPEN_EXISTING,0,0
mov hFile,eax
cmp eax,INVALID_HANDLE_VALUE
jz @Error

invoke GetFileSize,hFile,0
mov fSize,eax
push eax

invoke GlobalAlloc,GPTR,131072
mov pMem,eax

invoke ReadFile,hFile,pMem,65536,addr NBR,NULL
sub fSize,65536
add pMem,65536
invoke SetFilePointer,hFile,fSize,NULL,FILE_BEGIN
invoke ReadFile,hFile,pMem,65536,addr NBR,NULL
invoke CloseHandle,hFile

sub pMem,65536
mov esi,pMem
mov ecx,131072
pop eax
mov edx,eax

@@:
add edx,
add ebx,
add esi,8
sub ecx,8
jnz @B

push edx
push ebx
invoke wsprintf,addr Result,addr HashFormat1
PrintString Result
pop eax
pop eax
jmp @F

@Error:
szText error7,"CreateFile error"
invoke MessageBox,0,addr error7,addr error7,MB_OK
@@:

invoke GlobalFree,pMem
ret

Calc_Hash endp


Calc_Hash proc pFile:dword

LOCAL hFile:dword, NBR:dword
LOCAL fSize:dword,count:dword
LOCAL uint64:qword,tmp64:qword

invoke CreateFile,pFile,GENERIC_ALL,0,0,OPEN_EXISTING,0,0
mov hFile,eax
cmp eax,INVALID_HANDLE_VALUE
jz @Error

invoke GetFileSize,hFile,0
movd mm0,eax
mov fSize,eax
mov count,0

@@:
invoke ReadFile,hFile,addr uint64,8,addr NBR,NULL
paddd mm0,qword ptr
inc count
cmp count,8192
jnz @B

sub fSize,65536
invoke SetFilePointer,hFile,fSize,NULL,FILE_BEGIN
mov count,0

@@:
invoke ReadFile,hFile,addr uint64,8,addr NBR,NULL
paddd mm0,qword ptr
inc count
cmp count,8192
jnz @B

movd eax,mm0
push eax
punpckhdq mm0,mm0
movd eax,mm0
push eax

invoke wsprintf,addr tmp64,addr HashFormat1
PrintString tmp64
jmp @F

@Error:
szText error8,"CreateFile error"
invoke MessageBox,0,addr error8,addr error8,MB_OK
@@:

invoke CloseHandle,hFile
ret

Calc_Hash endp


Posted on 2010-09-06 13:21:53 by jpam
Problem solved !

Thanks to Tedd
Upper 4 bytes must be add with carry

correct version:

Calc_Hash proc uses esi ebx edx pFile:dword, pBuf:dword

LOCAL hFile:dword, fSize:dword, NBR:dword, pMem:dword

invoke CreateFile,pFile,GENERIC_ALL,0,0,OPEN_EXISTING,0,0
mov hFile,eax
cmp eax,INVALID_HANDLE_VALUE
jz @Error

invoke SetFilePointer,hFile,0,NULL,FILE_END
mov fSize,eax
push eax

invoke GlobalAlloc,GPTR,131072
mov pMem,eax

invoke SetFilePointer,hFile,0,NULL,FILE_BEGIN
invoke ReadFile,hFile,pMem,65536,addr NBR,NULL

sub fSize,65536
add pMem,65536

invoke SetFilePointer,hFile,fSize,NULL,FILE_BEGIN
invoke ReadFile,hFile,pMem,65536,addr NBR,NULL

sub pMem,65536
mov esi,pMem
mov ecx,131072
pop eax
mov edx,eax
push eax

@@:
add edx,
adc ebx,
add esi,8
sub ecx,8
jnz @B

push edx
push ebx
invoke wsprintf,pBuf,addr HashFormat
pop eax
pop eax

invoke CloseHandle,hFile
invoke GlobalFree,pMem
pop ecx

@Error: ; If error eax returns (INVALID_HANDLE_VALUE)

; Hash value is copied to pBuf
; eax returns Movie Filesize

ret

Calc_Hash endp
Posted on 2010-09-08 17:22:48 by jpam