Re: improve strlen



Claudio,

Don't be overawed by compilers, assembler coding is not restricted to
the architecture of a C compiler. The following code is a modification
of Agner Fog's DWORD string length routine that aligns the start and
tests the length 4 bytes at a time. It has no stack frame and conforms
to the normal register preservation rules under windows so it preserves
ESI and EDI but trashes the rest.

You will still need to convert it to AT&T syntax but it should have the
legs on any byte scanner around.

fn_004010A4:

push edi
push esi
mov eax, [esp+0Ch]
mov ecx, eax
add ecx, 3
and ecx, 0FFFFFFFCh
sub ecx, eax
mov esi, ecx
jz lbl2
sub eax, 1

lbl0:
add eax, 1
cmp BYTE PTR [eax], 0
jz lbl1
sub ecx, 1
jns lbl0
jmp lbl2

lbl1:
sub eax, [esp+0Ch]
jmp lbl5

lbl2:
lea edx, [eax+3]
nop

lbl3:
mov edi, [eax]
add eax, 4
lea ecx, [edi-1010101h]
not edi
and ecx, edi
and ecx, 80808080h
jz lbl3
test ecx, 8080h
jnz lbl4
shr ecx, 10h
add eax, 2

lbl4:
shl cl, 1
sbb eax, edx
add eax, esi

lbl5:
pop esi
pop edi
ret 4

Regards,

hutch at movsd dot com

.



Relevant Pages