Start New Discussion within our Software Development Community

You can become crazy with this problem.
Lets see this other quick solution to compare strings.


Remember that strings are a sequence of bytes.
home : h(first byte),o(second byte),...,e(last byte),0 a tag to identify the end of the string.

Suppose you have a string inside a register : %esi and you want to compare only the first character with another one :


zero: .ascii "0"

leal zero,%edi

cmpsb #this command compares the first byte(the first character) of the strings inside the registers ESI,EDI.

#Some examples of what you can do now :

je label1 #jump to label 1 if the first characters of the strings inside ESI,EDI are equal

jne label2 #jump to label 2 if the first characters of the strings inside ESI,EDI are not equal

jl label3 #jump to label 3 if the first character of the string inside ESI is lower (ASCII) than the first character of the string inside EDI.


#Now if you want to compare the second character of each string you can simply add 1 to the registers and they will point to the their second character, example:

addl $1,%esi
addl $1,%edi

#Its nice if you want to make a loop...


http://faydoc.tripod.com/cpu/cmpsb.htm

http://www.asciitable.com/

Or you could do

mov cx,5 ;Compare first 5 bytes 
mov di,string1 
mov si,string2 
cld
repe cmpsb 
je location1 ;If strings are 100% equal, go to this location

Much easier, much more efficient..
repe = repeat while equal, so you don't have to worry about it slowing down because of how many strings you are comparing.

This article has been dead for over six months. Start a new discussion instead.