mov al,0
mov ecx,-1
mov edi,offset string
repnz scasb
neg ecx

Faster than this?

Posted on 2002-05-05 17:08:44 by sch.jnn
yes ;) If your going to use that, then why not:

mov al,0
or ecx,-1
mov edi,offset string
repnz scasb
neg ecx
Posted on 2002-05-05 18:21:10 by bitRAKE
found this one awhile back guys, not taking credit.. but go ahead and try it guys...

@strlen PROC
PUBLIC @strlen

push di
push ds
pop es
mov di, bx

xor ax, ax
mov cx, -1
repne scasb
mov ax, cx
not ax
dec ax
pop di
@strlen ENDP

i forget where i got it... and i have not tried it.. :) so good luck :D
Posted on 2002-05-07 16:37:57 by liaisons
liaisons, thanks - that is the 16-bit version of the one sch.jnn posted.
It finds the length of string ds:bx and returns it in ax.
Posted on 2002-05-07 16:56:17 by bitRAKE
The Thread that Wouldn't Die, coming soon to a theatre near you. ;)
Posted on 2002-05-07 17:19:56 by iblis
The Thread that Wouldn't Die, coming soon to a theatre near you.
As far as I can remember, I'm partly responsible for this because I felt bad no one respond to it and this thread was near the bottom of the page. So I responded to my byte scanner algo then everything came into a chain reaction. :grin: I bet spiderman and starwars:attack of the clones will be in a lot of trouble cause this movie is gonna steal the spotlight. :grin:

Wanna race(using 64 bit registers) when itanium or amd hammer are going to come out. I bet there are new instructions. :grin:

Posted on 2002-05-13 18:34:32 by stryker
Hi all,
Just for your info:



New in Version 1.5:

Optimizations for the Pentium III and Pentium 4 processors
bla, bla bla........."


"Intel? JPEG Library 1.51

The Intel? JPEG Library (IJL) is a software library for application developers that provides high performance JPEG encoding and decoding of full color and grayscale still images.

The IJL was designed for use on Intel? architecture platforms and has been. Additionally, the IJL was developed to take advantage of MMX?, Streaming SIMD Extensions (SSE), and SSE-2 technology if present.....bla bla "

and now "tuned for speed and efficient memory usage" code of strlen from Intel? JPEG Library 1.51:

.text:60049450 mov eax, [ecx]
.text:60049452 mov edx, 7EFEFEFFh
.text:60049457 add edx, eax
.text:60049459 xor eax, 0FFFFFFFFh
.text:6004945C xor eax, edx
.text:6004945E add ecx, 4
.text:60049461 test eax, 81010100h
.text:60049466 jz short loc_60049450
.text:60049468 mov eax, [ecx-4]
.text:6004946B test al, al
.text:6004946D jz short loc_600494A1
.text:6004946F test ah, ah
.text:60049471 jz short loc_60049497
.text:60049473 test eax, 0FF0000h
.text:60049478 jz short loc_6004948D
.text:6004947A test eax, 0FF000000h
.text:6004947F jz short loc_60049483
.text:60049481 jmp short loc_60049450
.text:60049483 ; -- -------------------------------------------------------------------------
.text:60049483 loc_60049483:
.text:60049483 lea eax, [ecx-1]
.text:60049486 mov ecx, [esp+arg_0]
.text:6004948A sub eax, ecx
.text:6004948C retn
.text:6004948D ; ---------------------------------------------------------------------------
.text:6004948D loc_6004948D:
.text:6004948D lea eax, [ecx-2]
.text:60049490 mov ecx, [esp+arg_0]
.text:60049494 sub eax, ecx
.text:60049496 retn
.text:60049497 ; ---------------------------------------------------------------------------
.text:60049497 loc_60049497:
.text:60049497 lea eax, [ecx-3]
.text:6004949A mov ecx, [esp+arg_0]
.text:6004949E sub eax, ecx
.text:600494A0 retn
.text:600494A1 ; ---------------------------------------------------------------------------
.text:600494A1 loc_600494A1:
.text:600494A1 lea eax, [ecx-4]
.text:600494A4 mov ecx, [esp+arg_0]
.text:600494A8 sub eax, ecx
.text:600494AA retn
.text:600494AA strlen endp

It is a trial version. May be the registered version will be different..ha ha ha...
Posted on 2002-05-21 17:53:07 by buliaNaza
buliaNaza : you should send them your version .. :grin:
Posted on 2002-05-22 03:47:19 by eko
this works fine for me

StringLen Proc stdcall public,ptrSTRING1:DWORD;duh
push esi
push ebx
mov esi,ptrSTRING1
xor ebx,ebx
xor eax,eax
;cmp BYTE PTR [esi+eax],0
;jne @F
mov dl,BYTE PTR [esi+eax]
inc eax
cmp dl,0
jne @B
dec eax
pop ebx
pop esi
StringLen EndP

none of the algos on here worked,
i get these up the butt
Posted on 2002-05-25 22:08:50 by Qages
Hi, Qages

StringLen Proc stdcall public,ptrSTRING1:DWORD;duh
;push esi
;push ebx
;mov esi,ptrSTRING1
or eax,-1
;xor ebx,ebx
;xor eax,eax
;cmp BYTE PTR [esi+eax],0
;jne @F
;mov dl,BYTE PTR [esi+eax]
inc eax
;cmp dl,0
cmp BYTE PTR [eax+offset ptrSTRING1], 0
jne @B
;dec eax
;pop ebx
;pop esi
StringLen EndP
Posted on 2002-05-26 11:52:43 by buliaNaza
buliaNaza, ptrSTRING1 is a pointer - you can't reference it
indirectly with index. Your just testing the stack for zero bytes.
Posted on 2002-05-26 12:11:49 by bitRAKE
I do not udnerstand this all. - If you have a string wich contains a ZERO then it reads till the first zero and the other ones get lost ?

Deep Regards Bang-FF!
Posted on 2003-05-31 09:43:43 by Forginforcer

I do not udnerstand this all. - If you have a string wich contains a ZERO then it reads till the first zero and the other ones get lost ?

Deep Regards Bang-FF!


The convention is that if there is a zero, that is the end of the string, there can be nothing after it otherwise it is not a null terminated string it is an array of bytes.
Posted on 2003-05-31 09:53:22 by donkey
Forginforcer, there comes a time when one wishes to know how many bytes are needed for a string terminated by a particular byte (zero in most cases). Often we must discover the length of the string because we are not in control of it (user input, from another program, etc.), but we would like to be in control of it. And rather than assume it is of a certain size, we write an algorithm to determine the exact size. Wanting to do this rather frequently, some have set out to find a fast way to complete the goal.
Posted on 2003-05-31 09:54:50 by bitRAKE
hmmm, im thinking of this string:

**** DB 45,45,45,23,0,0,0,0,0,0,8,8,9,0,0,0,113,0,0,0,0,0,0,0,0,0,5
Posted on 2003-05-31 09:59:35 by Forginforcer
Then this of course is not a null-terminated string. All null-terminated strings end with 0. Thus strlen algo works by searching for 0 in the string.
Posted on 2003-05-31 10:05:34 by roticv
Forginforcer, that is not the purpose of this algo.
Posted on 2003-05-31 10:05:36 by bitRAKE
hmmm but after 23 it will stops, wont't it ?
because after that, there is a zero !
Posted on 2003-05-31 10:11:51 by Forginforcer
Posted on 2003-05-31 10:14:27 by roticv

In reality a string is a peice of cord you use to drive your cat nuts. Inside the computer there are no strings, it is an invented concept that is used to make things a little easier for programmers. In order to make this concept useful there were some rules applied, the first is that strings end with NULL (zero). If there is data after the zero then it is lost, period end of story. The algorithm presented here is to determine the length of a NULL terminated string if you need something else then find something else.

I did a quick search and on 5 seperate occasions I advised you to look for literature on the difference between strings and numbers, obviously you have decided that posting is easier than reading. Please research a little, many of the questions you have been asking are below entry level and have been answered many times in the various tutorials and documentation. The string question is explained in every computer programming book for any level of ability.
Posted on 2003-05-31 10:23:14 by donkey