Page 1 of 1

Comparing strings

Posted: Wed Aug 08, 2007 12:56 pm
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


Posted: Wed Aug 08, 2007 1:34 pm
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

Posted: Wed Aug 08, 2007 3:06 pm
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) .

Posted: Wed Aug 08, 2007 3:08 pm
by artrecks
Using long long, wouldn't need my processor to be a 64bit processor?

Posted: Wed Aug 08, 2007 3:10 pm
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.

Re: Comparing strings

Posted: Wed Aug 08, 2007 6:17 pm
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

Posted: Wed Aug 08, 2007 7:28 pm
by artrecks
ok! Thanks very much !

Re: Comparing strings

Posted: Wed Aug 08, 2007 11:22 pm
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