I have two functions in nasm that need optimizing, and I'm not much of an assembly man myself. I hardly got them working correctly!

let me give you some background information on these functions, all parameters are pointers. The last parameter is a double pointer to return data to the caller. The string is a unicode BSTR. The searchchar is a unicode byte. Any help optimizing is appreciated!

here is the first function


%define string [esp+08h]
%define searchchar [esp+0Ch]
%define return [esp+10h]

[BITS 32]
mov edi, dword string
mov edx, dword return

mov ecx, [edi-4]
mov ax, word searchchar

repne scasw
jne ExitMethod

sub edi, dword string
shr edi, 1

mov dword [edx], edi

ExitMethod:
xor eax, eax ;hresult success = 0
ret 10h


here is the second function


%define string [esp+08h]
%define searchchar [esp+0Ch]
%define return [esp+10h]

[BITS 32]
mov edi, dword string
mov edx, dword return

mov ecx, [edi-4]
mov ax, word searchchar

std
add edi, ecx

repne scasw
cld
jne ExitMethod

sub edi, dword string
shr edi, 1
add edi, 2

mov dword [edx], edi

ExitMethod:
xor eax, eax ;hresult success = 0
ret 10h


Also, do you have any idea why when the search is done, in the second function, edi is backed up 2 words? I fixed it by adding two to edi... thanks for any information!
Posted on 2003-09-25 17:47:26 by theministered
1. There are tons of optimization related to string handling in this forum. You only need to put right search keywords. Esp., scas is one of the instructions that may need to be replaced if one cares about speed. Off the top of my head, I remember lingo's posting about similar routine, with well-thought-out character search part.

2. Simple. Because you use repne scasw. edi points to the next character (in your case, a WORD) to the matched character. For better and complete explanation, see Intel manual vol 2.
Posted on 2003-09-25 18:05:59 by Starless
in the second example, shouldn't edi be backed up only one word and NOT two? Also, in the first example edi isn't one word ahead, it's right on the dot.
Posted on 2003-09-25 18:13:09 by theministered
OK, a little more explanation:
You are using Pascal strings, and the first character of the string has the index value of 1.

Now, if you were using C strings, you would have backed up 1 character (WORD in your code) in the first code, too, because C strings are 0-based arrays. Your first code works because you expect 1-based arrays.

In the second code, 0-based array requires to back up 1 character (WORD in your code), but now that you are using 1-based array, you have to add 1 to 0-based array index. Now you get your number.
Posted on 2003-09-25 18:59:55 by Starless