Hi everyone,
I'm trying to create a search routine that looks for a string through itemdata that is tied to each entry in a ListBox. I think scasb and cmpsb fit my needs, but I can't get it to work correctly, so I'm not so sure. This is for Win32. I've searched for scasb and cmpsb, and it seems the examples I find are for DOS. Is that my problem?
Have a look at the routine. I'll comment on what everything is along the way.
DoFind proc uses edi ebx esi
local numEntries:dword
invoke SendDlgItemMessage,g_hWnd,lstEntries,LB_GETCOUNT,0,0
mov numEntries,eax
xor ebx,ebx
.while ebx!=numEntries
invoke SendDlgItemMessage,g_hWnd,lstEntries,LB_GETITEMDATA,ebx,0
cld ; Clearing the direction flag after the API call in case it changes it?
xor ecx,ecx
mov cx,[eax] ; The itemdata in the ListBox is a pointer from HeapAlloc that starts with a 16-bit integer that specifies the length of the text immediately after it. I am able to use it without problems when selecting an item in the ListBox and populate an Edit control accordingly.
sub ecx,findStrLen ; Subtracting the length of the search string because I only initially scan for the first character
add eax,2 ; Move the pointer to the start of the text.
mov edi,eax ; For scasb
mov edx,findStrPtr ; Load first character into al
mov al,[edx] ; For scasb
repnz scasb ; Find a match
.if ZERO?
mov esi,findStrPtr ; Match found. Point to the search string for cmpsb.
inc esi ; First character already checked. Don't need to check it again.
mov ecx,findStrLen ; Search the remainder of the string.
dec ecx ; minus the first character.
repz cmpsb ; While it's a match, continue checking subsequent characters.
.if ZERO?
; Found the string
sub edi,findStrLen ; Debugging purposes. Trying to see if the string was really a match.
invoke MessageBoxA,0,edi,0,0
invoke SendDlgItemMessage,g_hWnd,lstEntries,LB_SETCURSEL,ebx,0 ; Select the item in the ListBox
.break
.endif
.endif
inc ebx
.endw
ret
DoFind endp
Thank you!