Hey guys, im a newbie around both this forum and Assembly. so im gonna be learning too many things from you guys.
just to start with, heres the StrLength function i coded the other day. hope its not as cranky as i think it is

Note : Coded in MASM32


      .386
      .model flat, stdcall
      option casemap :none
   
      includelib \masm32\lib\masm32.lib
      includelib \masm32\lib\kernel32.lib
      include \masm32\include\kernel32.inc


    .data
      TheStr db "I Rock",0
      TheStrLength dd 0
    .code

JMP start

StrLength PROC InString:DWORD
  XOR ESI , ESI
 
  @@DoLoop:
    MOV EAX , DWORD PTR
    MOV AL , BYTE PTR
    ADD ESI , 1
    CMP AL , 0b
    JNE @@DoLoop
  DEC ESI
  MOV EAX , DWORD PTR ESI
  RET
StrLength ENDP

start:
  INVOKE StrLength,OFFSET TheStr ; EAX is now 6 which is the length of the %TheStr%
  PUSH 0b
  CALL ExitProcess
end start
Posted on 2005-10-22 16:52:41 by XCHG
Hi XCHG and welcome to the forum.

Just some notes about optimization...

When testing for zero value in a register it's better to use test al, al instead of cmp al, 0. test is faster in this case.
Also you could save some bytes by using different registers for index and test (i noticed that you don't preserve esi, so destroying edx as well wont be a problem i guess):

xor    eax, eax 
@@DoLoop:
mov    esi dword ptr
mov    dl, byte ptr
add    eax, 1
test  dl, dl
jne  @@DoLoop:
dec    eax
ret
Posted on 2005-10-22 17:17:17 by arafel
XCHG,

IMHO you have to read before write:  :)

for example here:
http://web.textfiles.com/ezines/APJ/apj_3.txt
and here:
http://www.asmcommunity.net/board/index.php?topic=4058.0

Regards,
Lingo
Posted on 2005-10-22 17:55:28 by lingo12
There is a very simple but fast way to get the lenght of a string:

strlen proc uses esi src:dword
mov esi, src
mov ecx, -1
cld
xor eax, eax
repz scasb
sub eax, ecx
sub eax, 2
ret
strlen endp

For big strings and with the direction flag clear the repz is the fastest way to found a character.
Posted on 2005-10-22 20:45:54 by Eduardo Schardong
That's not true.  ;)
Posted on 2005-10-22 22:20:13 by roticv
The strLen optimization party ended a while ago.
Just search the board for strlen and you'll find more than enough
benchmarking and code.
http://www.asmcommunity.net/board/index.php?topic=21565.0
Posted on 2005-10-22 23:32:10 by r22