Page 1 of 1

Compare two strings in Assembly (Linux x86 & NASM)

Posted: Wed Feb 22, 2012 4:39 pm
by nicoobe
Hello,

I'm doing a program whose objective is to compare two values ​​(Strings). One value is entered by the user, the other is already declared. I have trouble comparing the two strings.

I leave my code below:

Code: Select all

section .data
	string1		db		'abcde'		; I want to compare this string with the one entered by the user.
	msg1		db		'Input: '
	msg2		db		'Good'
	msg3		db		'Bad'
	
	len1		db		$-msg1
	len2		db		$-msg2
	len3		db		$-msg3
	
section .bss
	input		resb		5
	
section .text
	global _start
	
	_start:
		
		; Print msg1
		mov ebx,1
		mov ecx,msg1
		mov edx,len1
		mov eax,4
		int 80h
		
		; Get input
		mov ebx,0
		mov ecx,input
		mov edx,5
		mov eax,3
		int 80h
		
		; Compare Input with string1
		cmp string1,input		; HERE IS THE PROBLEM!
		jz good
		jmp bad
		
	good:
		; Print msg2
		mov ebx,1
		mov ecx,msg2
		mov edx,len2
		mov eax,4
		int 80h
		jmp exit
		
	bad:
		; Print msg3
		mov ebx,1
		mov ecx,msg3
		mov edx,len3
		mov eax,4
		int 80h
		
	exit:
		mov eax,1
		mov ebx,0
		int 80h
PD: I am using Linux as OS and NASM as assembler.

Thanks you very much

Re: Compare two strings in Assembly (Linux x86 & NASM)

Posted: Wed Feb 22, 2012 4:54 pm
by VolTeK
Your are comparing addresses,

Look up repe cmpsb.

Re: Compare two strings in Assembly (Linux x86 & NASM)

Posted: Wed Feb 22, 2012 5:47 pm
by nicoobe
.

Re: Compare two strings in Assembly (Linux x86 & NASM)

Posted: Wed Feb 22, 2012 6:02 pm
by Combuster
nicoobe wrote:Please help me.
No, you're way out of line here.

Go visit a forum for beginners instead.