flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
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 |
|||
![]() |
|
DEMON
WinApi function
Code: int lstrcmp( LPCTSTR lpString1, // address of first string LPCTSTR lpString2 // address of second string ); |
|||
![]() |
|
DEMON
WinApi function
Code: int lstrcmp( LPCTSTR lpString1, // address of first string LPCTSTR lpString2 // address of second string ); |
|||
![]() |
|
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... |
|||
![]() |
|
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 |
|||
![]() |
|
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 |
|||
![]() |
|
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 |
|||
![]() |
|
Matrix
how come compare string is not in main?
Scientica? what do you think? |
|||
![]() |
|
Madis731
Yes, it should have its own place in main.
Matrix: Your code seems to work without errors this time, great code! |
|||
![]() |
|
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. |
|||
![]() |
|
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 |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.