Compare two strings in Assembly (Linux x86 & NASM)

Programming, for all ages and all languages.
Post Reply
nicoobe
Posts: 8
Joined: Wed Feb 22, 2012 12:19 am

Compare two strings in Assembly (Linux x86 & NASM)

Post 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
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

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

Post by VolTeK »

Your are comparing addresses,

Look up repe cmpsb.
nicoobe
Posts: 8
Joined: Wed Feb 22, 2012 12:19 am

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

Post by nicoobe »

.
Last edited by nicoobe on Wed Feb 22, 2012 6:30 pm, edited 2 times in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post by Combuster »

nicoobe wrote:Please help me.
No, you're way out of line here.

Go visit a forum for beginners instead.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply