Re: length of a string?
From: Bonj (benjtaylor)
Date: 12/15/04
- Next message: Vincent Fatica: "Re: QueryPerformanceFrequency"
- Previous message: Bonj: "Re: Visual studio 2003 .NET versus its own Command Prompt"
- In reply to: Bonj: "Re: length of a string?"
- Next in thread: Bonj: "Re: length of a string?"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 15 Dec 2004 00:18:39 -0000
sorry i've now read your post fully and read your caveat about unicode -
sorry, didn't mean to be glib
"Bonj" <Bonj@discussions.microsoft.com> wrote in message
news:86DC129E-9441-48F2-8DAA-5E891DEC1E8B@microsoft.com...
> I'll try and understand it... but in the meantime I'll show you my own
> efforts.
> It beats the runtime - does my algorithm beat yours though, that's the
> question.... :-)
> Maybe you can tell what I'm trying to do / spot flaws?
> (And also - I've written unicode AND MBCS versions. So there.)
>
> So my algorithm is up head to head with Relvinians... :o))
>
> Also one other question... does issuing "dec" remove the need to issue
> "cmp"? From what I can see it does...
>
> size_t fastwcslen(LPCWSTR widecharstring)
> {
> __asm
> {
> xor eax, eax
> mov ebx, widecharstring
> align 16
> compare:
> mov edx, [ebx]
> cmp dx, 0
> jz endhere
> inc eax
> shr edx, 16
> cmp dx, 0
> jz endhere
> inc eax
> add ebx, 4
> jmp compare
> endhere:
> }
> }
>
> size_t faststrlen(LPCSTR charstring)
> {
> __asm
> {
> xor eax, eax
> mov ebx, charstring
> align 16
> compare:
> mov edx, [ebx] ;load the next 4 bytes into edx
>
> cmp dl, 0 ;compare the first byte
> jz endhere
> inc eax ;1st of 4 isn't NULL-> so inc retval
> shr edx, 8 ;gets the second of 4 bytes in dl
> cmp dl, 0
> jz endhere
> inc eax ;2nd of 4 isn't NULL-> inc retval again
> shr edx, 8 ;gets 3rd of 4 bytes into dl
> cmp dl, 0
> jz endhere
> inc eax ;3rd of 4 isn't NULL-> inc retval again
> shr edx, 8
> cmp dl, 0
> jz endhere
> inc eax ;gone 4 bytes without a 0...
> add ebx, 4 ;...so need to load the next 4 bytes (from [ebx])
> jmp compare
> endhere:
> }
> }
>
> "Relvinian" wrote:
>
>> Bonj wrote:
>> > Is there a quick way in assembly language of finding the first null
>> > character
>> > of a string?
>> > Basically I think I know how to do it, *if* it's no choice but to
>> > examine
>> > every single byte until there's one that's zero. But is there any
>> > method of
>> > doing some fancy xor operation with one of the mm# registers?
>> >
>> >
>>
>> Bonj,
>>
>> This is a quick non -mmx, sse, sse2, sse3 assembly routine to find the
>> length of a string (for ANSI only). If you are trying to find the length
>> of strings for wide characters, this routine will NOT work.
>>
>> There are MMX, etc routines are there if you like to use them. Just
>> GOOGLE for something along the lines of "+asm +example +string +length
>> +fast"
>>
>> This is MASM syntax.
>>
>> TITLE StrLen.asm
>>
>> ..586
>> ..model flat ; memory model
>> option casemap:none ; case sensitive
>>
>> ..code
>> align 16
>>
>> ;
>> ----------------------------------------------------------------------------
>> ; prototype:
>> ; int __stdcall StringLengthA(const char* string);
>> ;
>> ; Description:
>> ; ANSI version to determine length (count) in a null-terminated string
>> ;
>> ; Return value:
>> ; EAX contains the length of the string
>> ;
>> ----------------------------------------------------------------------------
>>
>> ;
>> ---------------------------------------------------------------------------
>> ; variable offests into ESP upon function entering and adjustment of the
>> ; stack for local variables and register preservation
>> ;
>> ===========================================================================
>> ; ESP + 4 = String parameter
>>
>> _StringLengthA@4 proc near
>> mov eax, [esp + 4]
>> mov ecx, eax
>> sub eax, 1
>>
>> DoAlign:
>> ; keep checking the string for a NULL terminator until the string
>> ; offset ptr is on a 4-byte boundary.
>> add eax, 1
>> test eax, 3
>> jz short FindLength
>>
>> cmp byte ptr [eax], 0
>> jne short DoAlign
>> jmp short CalcOffset
>>
>> align 4
>> FindLength:
>> mov edx, [eax]
>> add eax, 4
>>
>> test dl, dl
>> jz short LowByteNull
>>
>> test dh, dh
>> jz short HighByteNull
>>
>> test edx, 0ff0000h
>> jz short HighWordNull
>>
>> test edx, 0ff000000h
>> jnz short FindLength
>>
>> ; Found NULL terminator on the high word high byte
>> sub eax, 1
>> jmp short CalcOffset
>>
>> HighWordNull:
>> ; Found NULL terminator on the high word low byte
>> sub eax, 2
>> jmp short CalcOffset
>>
>> HighByteNull:
>> ; Found NULL terminator on the low word high byte
>> sub eax, 3
>> jmp short CalcOffset
>>
>> LowByteNull:
>> ; Found NULL terminator on the low word low byte
>> sub eax, 4
>>
>> CalcOffset:
>> sub eax, ecx
>> ret 4
>> _StringLengthA@4 endp
>>
>> end
>>
- Next message: Vincent Fatica: "Re: QueryPerformanceFrequency"
- Previous message: Bonj: "Re: Visual studio 2003 .NET versus its own Command Prompt"
- In reply to: Bonj: "Re: length of a string?"
- Next in thread: Bonj: "Re: length of a string?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|