I have wrote an GetWord that will provide me an word depending on its id,
if some didnt understandmy english ill try to explain:
string = "Win32 Asm is simple"
GetWord,addr string,addr dest,1
dest = "Asm"
GetWord,addr string,addr dest,2
dest = "is"
...
the problem it is that my code is big and i think slow because of my not soo good asm expirience, but its working :), I hope any1 could help me optimizing it, or giving me another code.


GetWord proc src:DWORD,dst:DWORD,wordID:DWORD
invoke lstrlen,src
mov ebx,eax
inc ebx ; anti if src is zero lenght

xor ecx,ecx
mov esi,src
dec esi
mov edi,dst

cmp wordID, 0
jne _00
inc esi
jmp _01

_00:
inc esi ; this loop provides the word in esi
dec ebx
jz _02
cmp byte ptr [esi],020h
jne _00
inc ecx
cmp ecx, wordID
jne _00

_01:
mov al, byte ptr [esi] ; this copyes to edi and puts zero-terminated
mov byte ptr [edi], al
inc esi
inc edi

cmp byte ptr [esi],020h
je _02
cmp byte ptr [esi],00h
jne _01

_02:

mov byte ptr [edi],0
ret
GetWord endp


Thank you

edit:
I have put another anti-crash right after lstrlen
Posted on 2004-01-13 10:55:34 by zabnik
Originally posted by zabnik


mov al, byte ptr [esi] ; this copyes to edi and puts zero-terminated
mov byte ptr [edi], al
inc esi
inc edi


by taking a quick look, that whole sequence can be replaced by


movsb

the copied byte will not in al, but obviously you don't need that
Posted on 2004-01-13 13:46:47 by hartyl
Here is a version using lodsb/stosb .. it's shorter (42 bytes including prologue/epilogue), but probably also slower :)

GetWord proc uses esi edi src:DWORD,dst:DWORD,wordID:DWORD


mov esi, src
mov ecx, wordID

next_word:
mov edi, dst

skip_ws:
lodsb
cmp al, 20h
je skip_ws

copy_word:
stosb

test al, al
jz done

lodsb

cmp al, 20h
ja copy_word

dec esi

dec ecx
jns next_word

xor eax, eax
stosb

done:
ret

GetWord endp
Posted on 2004-01-14 04:13:44 by Jibz
Slight change to Jibz's algo:
GetWord proc src:DWORD,dst:DWORD,wordID:DWORD


mov edx, src

next_word:
mov ecx, dst

skip_ws:
mov al, [edx]
inc edx
cmp al, 20h
je skip_ws

copy_word:
test al, al
jz done

mov [ecx], al
inc ecx

mov al, [edx]
inc edx

cmp al, 20h
ja copy_word

; dec edx ; why copy delimiter?

dec wordID
jns next_word

done:
mov byte ptr [ecx], 0
ret

GetWord endp
Posted on 2004-01-14 09:44:19 by bitRAKE
Nice changes!
;   dec edx ; why copy delimiter?

To allow wordID to be greater than the number of words available. Try getting the second word from the two byte string 'A',0.
Posted on 2004-01-14 14:12:06 by Jibz
So many smart people here, Thank you very much :alright:
Posted on 2004-01-17 15:25:08 by zabnik
GetWord proc src:DWORD,dst:DWORD,wordID:DWORD


mov edx, src
mov ecx, dst
skip_ws:
mov al, [edx]
inc edx
skip_ws2:
test al,al
je done
cmp al, 20h
jbe skip_ws

dec wordID
je copy_word
skip_word:
mov al, [edx]
inc edx
cmp al, 20h
jbe skip_ws2
jmp skip_word

copy_word:
mov al, [edx]
inc edx
mov [ecx], al
done:
inc ecx
cmp al, 20h
ja copy_word
mov byte ptr [ecx-1], 0
ret

GetWord endp
I played a little with a different method - not copying all the words until the correct one is reached. Instead just skip the words, copying just the selected one. I imagine it being faster, but I didn't test the reality of it.
Posted on 2004-01-17 17:09:25 by bitRAKE