Page 1 of 1
Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 7:44 pm
by nicoobe
Hello,
I need help debuging my code. Basically it compares str1 with str2. If str1 is equal to str2 it prints on screen "Good". If they are not equal it prints "Bad".
My problem is that the result I get when 2 strings are equal is:
Good
Bad
abcdeabcde
And when they are diferent:
Bad
abcdeabbde
Here I leave my code:
Code: Select all
section .data
msg1: db 'Good',10 ; msg1
msg2: db 'Bad',10 ; msg2
str1: db 'abcde' ; str1
str2: db 'abcde' ; str2
lenmsg1: equ $-msg1 ; length msg1
lenmsg2: equ $-msg2 ; length msg2
lenstr1: equ $-str1 ; length str1
lenstr2: equ $-str2 ; length str2
section .text
global _start
_start:
mov esi,str1
mov edi,str2
mov ecx,lenstr2
cld
repe cmpsb
jecxz good
; If bad
mov eax,4
mov ebx,1
mov ecx,msg2
mov edx,lenmsg2
int 80h
jmp exit
good:
mov eax,4
mov ebx,1
mov ecx,msg1
mov edx,lenmsg1
int 80h
exit:
mov eax,1
mov ebx,0
int 80h
Thanks for reading.
Re: Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 7:46 pm
by nicoobe
I know it seems i make 2 equal topics but they are diferent. The code and the help I am asking for is diferent.
Re: Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 7:56 pm
by bubach
How about terminating the string with a zero? It's pretty obvious that the function for printing doesn't know when to stop and just keeps going.
EDIT: Oh my bad. Noticed now that you use "lenmsg1: equ $-msg1 ; length msg1"... But since it calculates length based on start of string to current position ($) this should be located DIRECTLY after the string in question otherwise it will hold the length of everything from the start of that string down to where you put it.
Last Bug
Posted: Wed Feb 22, 2012 8:14 pm
by nicoobe
Thanks you very much. Now my code is almost perfect.
The only problem is that my program is not comparing the last char of the strings.
For example:
If STR1 = 'ABCDE' and STR2 = 'ABCDD', my program would say that both strings are equal.
The rest works fine.
How can I fix that bug?
Re: Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 8:35 pm
by VolTeK
nicoobe wrote: mov ecx,lenstr2
cld
repe cmpsb
jecxz good
Glad it works. A forewarning however,
-Do NOT ask us to debug your code to help find out whats wrong. I do not believe in leaving a troublesome animal at someone else's door and hoping they will take care of it (posting code and asking for help). This is usually a problem in the programming skills portion of the developer, and it is skills that are in need of practice. Combuster had referred to you a link to better your assembler skills, follow it.
-And do NOT post double topics to raise your chance of getting a better answer. I know its the same question, both code compare, and both code had problems.
As for your next question, it sounds like a counter error, or finding the string termination to early. This is another example of what assembler practice can do for you. Saves time, and questions.
Re: Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 8:39 pm
by nicoobe
Thanks for the help. But I can still not found a solution for the problem I mention before.
Maybe I could write:
But I don't think is a very elegant solution.
Any idea?
Re: Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 8:42 pm
by VolTeK
Compile it and see. If not, run it through a debugger and check the values your self.
Emu8086 is a nice one.
Re: Compare Two Strings with Assembly (Linux x86 & NASM)
Posted: Wed Feb 22, 2012 8:53 pm
by nicoobe
Yes, that solve my problem.
Thank you very much.
Here is my final code working correctly:
Code: Select all
section .data
msg1: db 'Good',10 ; msg1
lenmsg1: equ $-msg1 ; length msg1
msg2: db 'Bad',10 ; msg2
lenmsg2: equ $-msg2 ; length msg2
str1: db 'abcde' ; str1
lenstr1: equ $-str1 ; length str1
str2: db 'abcde' ; str2
lenstr2: equ $-str2 ; length str2
section .text
global _start
_start:
mov esi,str1
mov edi,str2
mov ecx,lenstr2+1
cld
repe cmpsb
jecxz good
; If bad
mov eax,4
mov ebx,1
mov ecx,msg2
mov edx,lenmsg2
int 80h
jmp exit
good:
mov eax,4
mov ebx,1
mov ecx,msg1
mov edx,lenmsg1
int 80h
exit:
mov eax,1
mov ebx,0
int 80h
PD: Im sorry for making 2 topics
I wont do it again