Comparing strings

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
artrecks
Posts: 23
Joined: Wed Jul 11, 2007 8:24 pm

Comparing strings

Post by artrecks »

Is this a good way of comparing strings with a default size ( of 12 bytes )

Code: Select all


; ASM CODE
; NASM SINTAX
; 

           cld
           mov cx,11		       ; from 0 to 11
           mov edi,[string1]	     ; first string
           mov esi,[string2]	     ; second string

           rep cmpsb 		     
           jne error	      ; error

string1 db "One string!!"  ; 12 bytes
string2 db "Another  str"  ; 12 bytes

User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Post by hailstorm »

I guess it is. You could even after check [esi] and [edi] after a mismatch to see which string is the smallest / biggest.
B.t.w. You have to load the offset of string1 and string2. You are now loading it with their first four characters as an address. I don't think that'll work. :mrgreen:

But I don't think this is a real os-dev question. More for general programming and such. :-k
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I'd start by finding the size of both strings and working from there.

For instance, if both are aligned to an 8-byte boundary to perform the copy I can use an "unsigned long long" - effectively speeding up the operation 8 times.

The same can apply to such things as string comparison as well. The only function that I can think of off the top of my head that you can't do this in is strlen 8) .
artrecks
Posts: 23
Joined: Wed Jul 11, 2007 8:24 pm

Post by artrecks »

Using long long, wouldn't need my processor to be a 64bit processor?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

No, 32-bit processors support unsigned long long. 64-bit processors just use 64-bit variables to address stuff (so your pointers would be 8 bytes instead of 4).

Edit: There are other massive differences between 32-bit and 64-bit, I just only mentioned the relevant one.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Comparing strings

Post by Dex »

You should use something like this

Code: Select all


; ASM CODE
; NASM SINTAX
; 

           cld
           mov ecx,11		       ; from 0 to 11
           mov edi,string1	     ; first string
           mov esi,string2	     ; second string

           repe cmpsb 		     
           jne error	      ; error
           ;some code here

string1 db "One string!!"  ; 12 bytes
string2 db "Another  str"  ; 12 bytes

Note the ECX and REPE.
The using cx in pmode for loops or rep etc, is a common misstake, that can give a nasty outcome, if ecx was holding say 0x800000 before this code
artrecks
Posts: 23
Joined: Wed Jul 11, 2007 8:24 pm

Post by artrecks »

ok! Thanks very much !
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Comparing strings

Post by Brendan »

Hi,
artrecks wrote:Is this a good way of comparing strings with a default size ( of 12 bytes )
"REP CMPSB" and "REP CMPSD" are slow as the CPU does some internal setup and there's exit penalties.

If the strings are always 12 bytes, then the fastest way may be something like:

Code: Select all

    mov eax,[esi]
    mov ebx,[esi+4]
    mov ecx,[esi+8]

    xor eax,[edi]
    xor ebx,[edi+4]
    xor ecx,[edi+8]

    or eax,ebx
    or eax,ecx
    jne .different
Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply