flat assembler
Message board for the users of flat assembler.

Index > Windows > Compare String

Author
Thread Post new topic Reply to topic
Lestat



Joined: 01 Oct 2003
Posts: 2
Lestat
Helou Y'all, i got one problem
How can i compare two strings, n see if they r match.
Im tryin' to make remote Winamp control, over da telnet...

thnx in advance !

Bye
Post 18 Nov 2004, 19:15
View user's profile Send private message MSN Messenger Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
Why haven't you done your homework :@
CMPSB
Compare Strings

Code:
CMPSB                         ; A6                   [8086]
CMPSW                         ; o16 A7               [8086]
CMPSD                         ; o32 A7               [386]

CMPSB compares the byte at [DS:SI] or [DS:ESI] with the byte at [ES:DI] or [ES:EDI], and sets the flags accordingly. It then increments or decrements (depending on the direction flag: increments if the flag is clear, decrements if it is set) SI and DI (or ESI and EDI).
The registers used are SI and DI if the address size is 16 bits, and ESI and EDI if it is 32 bits. If you need to use an address size not equal to the current BITS setting, you can use an explicit a16 or a32 prefix.
The segment register used to load from [SI] or [ESI] can be overridden by using a segment register name as a prefix (for example, ES CMPSB). The use of ES for the load from [DI]  or [EDI] cannot be overridden.
CMPSW and CMPSD work in the same way, but they compare a word or a doubleword instead of a byte, and increment or decrement the addressing registers by 2 or 4 instead of 1.
The REPE and REPNE prefixes (equivalently, REPZ and REPNZ) may be used to repeat the instruction up to CX (or ECX - again, the address size chooses which) times until the first unequal or equal byte is found.
    

Winamp control I understand - but over TELNET? What do you mean?
Think you meant TCP/IP -OR- are you using only a specific port?

Anyway, you would have to run one instance of winamp on your computer continuously and make a server to receive IP connections and parse them. Secondly you'd have to make a client on the other side while the server remains on a static IP so your client can send requests.
I got to say, this is one hard thing to start with. But I admire what you are doing - you should post your progress on this forum.
Cheers
Post 19 Nov 2004, 10:16
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
DEMON



Joined: 01 Aug 2004
Posts: 26
Location: Republic of Belarus
DEMON
WinApi function
Code:
int lstrcmp(
    LPCTSTR lpString1, // address of first string 
    LPCTSTR lpString2    // address of second string 
   );    
Post 19 Nov 2004, 18:46
View user's profile Send private message Reply with quote
DEMON



Joined: 01 Aug 2004
Posts: 26
Location: Republic of Belarus
DEMON
WinApi function
Code:
int lstrcmp(
    LPCTSTR lpString1,    // address of first string 
    LPCTSTR lpString2    // address of second string 
   );    
Post 19 Nov 2004, 18:47
View user's profile Send private message Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
Coded by Hutch:
Code:
proc strcmp,str1,str2
   mov     ecx, str1 
  mov     edx, str2 
cmst: 
    mov     al, [ecx] 
  cmp     al, [edx] 
  jne     no_match 
   add     ecx, 1 
     add     edx, 1 
     test    al, al 
     jne     cmst 
       xor     eax,eax
     ret 

no_match: 
  mov     eax,1
       return 
endp
    

_________________
Code it... That's all...
Post 19 Nov 2004, 20:16
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Code:
macro dos_writestr ._string121
{mov dx,._string121
call dosprint}

org 256

push es

push ds
pop es

mov esi,str3 ; source string
mov edi,str4 ; destination string
mov ecx,255 ; max lenght
call compare_strings_and_write

mov esi,str1 ; source string
mov edi,str2 ; destination string
mov ecx,255 ; max lenght
call compare_strings_and_write

mov esi,str3 ; source string
mov edi,str4 ; destination string
mov ecx,255 ; max lenght
call compare_strings_and_write

pop es
xor ah,ah
int 16h

int 20h

compare_strings_and_write:
pushf
dos_writestr si
dos_writestr di
call cmpstr0
jne .not_equal_label
dos_writestr _equal
jmp .equal_label
.not_equal_label:
dos_writestr _not_equal
.equal_label:
popf
ret

dosprint:
mov ah,9
int 21h
ret

str1: db 'i am string 1',13,10,'$',0
str2: db 'i am string 1',13,10,'$',0

str3: db 'i am string 3',13,10,'$',0
str4: db 'i am string 4',13,10,'$',0

_equal: db 'EQUAL',13,10,'$'
_not_equal: db 'NOT EQUAL',13,10,'$'

cmpstr0: ;compare string ds:(e)si with es:(e)di, (e)cx=max lenght, returns zero if equal (e)
mov edx,edi  ; save source pointer
repnz scasb  ; get string 1 end
mov ecx,edi
sub ecx,edx
mov edi,edx
repe cmpsb   ; compare strings
or ecx,ecx
;jecxz ._equal  ; if not equal then bytes left
;clc
;jmp ._not_equal
;._equal:         ; equal
;stc
;._not_equal:
ret
    

i was thinking something like this,


Last edited by Matrix on 20 Nov 2004, 08:56; edited 6 times in total
Post 19 Nov 2004, 20:28
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
Isn't there a bug?
Your code always shows you NOT equal because even if unequal byte is found, you test it against 0 but it doesn't have to be the case:
str1 db "string",0,46,86,129,...
str2 db "string",0,13,35,...
repe exits when 46<->13 are compared
Two digits can NEVER be zero when, the are different

I can't fix your code - it becomes so complicated. I'd rather suggest my own strcmp routine (DWORD is faster with longer strings):
Code:
    mov esi,str1
    mov edi,str2
    cld ;use it (unless you are REALLY sure)
    jmp .cut_in
  .the_loop:
    add esi,4
    add edi,4
  .cut_in:
    mov eax,[esi]
    mov edx,[edi]
    cmp eax,edx
    je  .the_loop
  .final_tests:
    cmpsb
    jne .dif
    cmp [esi],byte 0
    je  .equ
    jmp .final_tests
  .dif:    ;No, they don't match!
  .equ:    ;Yes, equal they are!

str1 db "Tere, mina olen üks string",0
str2 db "Tere, mina olen üks strin",0
    
Post 19 Nov 2004, 22:06
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
doesn't have to be the case:
str1 db "string",0,46,86,129,...
str2 db "string",0,13,35,...
repe exits when 46<->13 are compared

two string cannot be equal if the zero terminators are not @the same place,
why are you putting 0 @the middle of the 0 terminated string?

my code not working?
sorry, its fixed
Post 20 Nov 2004, 07:26
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
how come compare string is not in main?
Scientica? what do you think?
Post 20 Nov 2004, 08:43
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
Yes, it should have its own place in main.

Matrix: Your code seems to work without errors this time, great code!
Post 20 Nov 2004, 14:52
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Thnx Madis731

btw.: my compare code won't be too slow compared to a single loop one, (cause the source string is mapped into cache during first loop if string is less then about 8 KB) i'll post the next version, but for small ~100 byte strings it counts nearly nothing.
Post 21 Nov 2004, 03:47
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
here you are, maeby a bit faster
ready for testing
Code:
macro dos_writestr ._string121
{mov dx,._string121
call dosprint}

org 256

push es

push ds
pop es

mov esi,str3 ; source string
mov edi,str4 ; destination string
;mov ecx,255 ; max lenght
call compare_strings_and_write

mov esi,str1 ; source string
mov edi,str2 ; destination string
;mov ecx,255 ; max lenght
call compare_strings_and_write

mov esi,str3 ; source string
mov edi,str4 ; destination string
;mov ecx,255 ; max lenght
call compare_strings_and_write

pop es
xor ah,ah
int 16h

int 20h

compare_strings_and_write:
dos_writestr si
dos_writestr di
call cmpstr0
jne .not_equal_label
dos_writestr _equal
jmp .equal_label
.not_equal_label:
dos_writestr _not_equal
.equal_label:
ret

dosprint:
mov ah,9
int 21h
ret

str1: db 'i am string 1',13,10,'$',0
str2: db 'i am string 1',13,10,'$',0

str3: db 'i am string 3',13,10,'$',0
str4: db 'i am string 4',13,10,'$',0

_equal: db 'EQUAL',13,10,'$'
_not_equal: db 'NOT EQUAL',13,10,'$'

cmpstr0b: ;compare string ds:(e)si with es:(e)di, (e)cx=max lenght, returns zero if equal (e)
mov edx,edi  ; save source pointer
repnz scasb  ; get string 1 end
mov ecx,edi
sub ecx,edx
mov edi,edx
repe cmpsb   ; compare strings
or ecx,ecx
ret

cmpstr0: ;compare string ds:(e)si with es:(e)di, returns zero if equal (e)
lodsb
inc di
or al,al
jz .zero
cmp [es:di-1],al
je cmpstr0
.zero: cmp byte [es:di-1],0
ret
    
Post 21 Nov 2004, 08:28
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.