Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SAEinSDSU

macrumors newbie
Original poster
Feb 17, 2010
12
0
Hello I am writing some simple c/assembly programs for my college course and have run into an unknown compile error using Xcode to build and run. It builds successfully but says "GDB: program received signal: 'EXC_BAD_ACCESS'" I am not sure what that means...maybe i am returning a wrong value from my assembly function? to the main C program...any debug help would be much appreciated.

Here is my C main funciton and Assembly _isOdd(function to tell whether number is odd or even).
Code:
int extern _AisOdd;
int main(void) {
	int k; 
	k = AisOdd(5); 
	printf("\n%d",k);
	k = AisOdd(6); 
	printf("\n%d",k);
	return EXIT_SUCCESS;
}

.globl _AisOdd

_AisOdd:
//push base pointer of the calling function
		pushl	%ebp
		movl	%esp, %ebp
		movl	$1, 12(%ebp)		//declare int variable t = 1
		movl	8(%ebp), %eax		//declare int variable x
		movl	%eax, %ebx
		sarl	$1, %ebx			
		movl	%ebx, %ecx
		sall	$1,	%ecx
		cmpl	%ecx, %eax
		JNE		leave
		movl	$0, 12(%ebp)
leave:
		popl	%ebp
		ret
P.S. using i386 architecture. Thanks so much
 
Hello I am writing some simple c/assembly programs for my college course and have run into an unknown compile error using Xcode to build and run. It builds successfully but says "GDB: program received signal: 'EXC_BAD_ACCESS'" I am not sure what that means...maybe i am returning a wrong value from my assembly function? to the main C program...any debug help would be much appreciated.

That's not a compile error. The program is GDB, which is the debugger. In the sequence of Build and Run, the error is occurring at Run, not Build.
 
Hello I am writing some simple c/assembly programs for my college course and have run into an unknown compile error using Xcode to build and run. It builds successfully but says "GDB: program received signal: 'EXC_BAD_ACCESS'" I am not sure what that means...maybe i am returning a wrong value from my assembly function? to the main C program...any debug help would be much appreciated.

Here is my C main funciton and Assembly _isOdd(function to tell whether number is odd or even).

int extern _AisOdd;
int main(void) {
int k;
k = AisOdd(5);
printf("\n%d",k);
k = AisOdd(6);
printf("\n%d",k);
return EXIT_SUCCESS;
}

.globl _AisOdd

_AisOdd:
//push base pointer of the calling function
pushl %ebp
movl %esp, %ebp
movl $1, 12(%ebp) //declare int variable t = 1
movl 8(%ebp), %eax //declare int variable x
movl %eax, %ebx
sarl $1, %ebx
movl %ebx, %ecx
sall $1, %ecx
cmpl %ecx, %eax
JNE leave
movl $0, 12(%ebp)
leave:
popl %ebp
ret

P.S. using i386 architecture. Thanks so much

1. You are defining an extern variable named "_AIsOdd" of type int, not a function named "AIsOdd" taking an int argument of type int and returning int. Your compiler should give you a warning, but that is not the problem.

2. Do some "cheating": Write a function just that the one you want, but in C. Put it into its own file. Change XCode to "Release" settings and "Show Assembly Code" in the Build menu. Compare with your code.

3. After the push ebp / move ebp,esp, what does the stack frame of your function look like?

0(esp) ->
4(esp) ->
8(esp) ->

and the one that is killing you:

12(esp) -> ?????????
 
Ha! I think we're in the same class.

Anyway, I think the problem might be that you're not putting "1" or "0" in the eax register before leaving the function. It has to be in the eax register to be passed on to the main function.

Have you figured out how to step into your assembly code with Xcode? I can't figure it out. It'll step into the compiler generated assembly just fine, but when it reaches the function, it'll call to the function and then not step into it. I can't figure it out so I have to debug by trial and error.

Hope this helps
Kyle.

Edit: After working on the assignment for a while I've encountered this error a few times. It happens when you are reading or writing to an invalid part of the memory. For example, in your code, you are writing the return value to 12(%ebp) in an attempt to return it to the main function. You can't pass arguments this way. It has to go through %eax.

I think I've got this whole assembly business down!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.