I want to write an x86 AT&T assembly function that compare a char-array one caracter at a time. If two characters at the same index are unequal it's suposed to return -1, else return 1.

I'm going to call this function in a C code. So the function-call would look like this

int comp(char *s, char *format)

I'm totally new to assembly programming so I could really use som help with this.

Recommended Answers

All 6 Replies

You need to post some sort of an attempt and then I'll help you with it. I'll give you a hint though --
If you index through each string at the same time and cmp each time in a loop as long as cmp keeps coming out to be 0 (since cmp basically subtracts its operands) then the strings are matching.

Thank you for replying.
Well I tried my best but I don't think I did so well.

.globl arrcomp

arrcomp:
	pushl 	%ebp			
	movl 	%esp,%ebp
	movl 	$0,%eax
	
	movl	12(%ebp),%ebx		#parameter 1. 
	movl	 8(%ebp),%edx		#parameter 2.
	
	
loop:
	subl	 (%edx,%ecx,4),%eax	       #parameter1[i]-paramter2[i]
	jnz	b_exit			               #jump to b_exit if %eax == 0

	jmp	loop	

	
a_exit:
	popl 	%ebp
	ret

b_exit:
	addl $-1,%eax
	ret

Hopefully it show in a sense what I'm trying to do. My biggest problem is the array-index. I don't understand how to look up the index in the arrays.

indexing an array is easy. We'll assume I'm referring to a char array.
if the memory address of your array is in ebx
then ebx+0 is the first element in that array correct?
So that means ebx+1 is the second element.
Arrays are just sequentially laid out data in a flat plane in memory.

That was simple enough because a char is only 1 byte. The general rule for indexing simple arrays in assembly is
array + size of each element * position

That might work with intel notation, but with AT&T it's different. But I think I got it in this new version of my code.

.globl arrcomp

arrcomp:
	pushl 	%ebp			
	movl 	%esp,%ebp
	movl 	$0,%ecx			        #Counter
	movl 	$0,%eax
	
	movl	12(%ebp),%ebx		#parameter 2
	movl	 8(%ebp),%edx		#parameter 1.
	
	

loop:
	addl	(%ebx,%ecx,1),%eax	   #Parameter2[i] store in %eax
	subl	(%edx,%ecx,1),%eax	           #parameter1[i]-%eax store in %eax
	jnz	b_exit			                   #jump to exit if %ecx == 0

	addl	$0,%eax
	incl	%ecx
	jmp	loop	

	
a_exit:
	popl 	%ebp
	ret


b_exit:
	addl $-1,%eax
	ret

It does however produse segmentationfault when I try to run it. I tried debugging, but couldn't get anythin usefull out of the dump file.

movl	12(%ebp),%ebx		#parameter 2
	movl	 8(%ebp),%edx		#parameter 1.
	
	

loop:
	addl	(%ebx,%ecx,1),%eax	   #Parameter2[i] store in %eax
	subl	(%edx,%ecx,1),%eax	           #parameter1[i]-%eax store in %eax

Alright so unless I'm going crazy, you load ebx and edx. ecx is what you're using to index and count and it's 0 on the first pass
then you're adding the data located at ebx+ecx*1 with eax
then subtracting edx+ecx*1 with eax to see if it's the same thing or not.
You may want to changethat addl to a movl


You said you ran the program through a debugger?

I got the debuger to work now. You can see the result in the attachment.
The fault happends in the first line of the loop.

I also changed that addl to movl.

Though I would post my C-code aswell.

#include <stdio.h>

extern int arrcomp (char *a, char *format);

int main(void) {
  printf("Summen av arrayen er: %d \n", arrcomp("a,b,c", "a,b,c"));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.