This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

problems with assembly/C mixing


This is probably my own fault, but maybe someone can help.  I'm trying to
figure out how C/assembly linkage works, so I wrote a test program
that figures a factorial in assembly, and called it from C.  The
interesting thing about the C program is that I'm not displaying the
factorial, but instead returning it as the exit status code (it's a long
story).  Anyway, if you build it with

gcc test2.c fact2.s -o test2

and run

./test2

you get a segmentation fault.  But, if you change return(factorial(4)) to
exit(factorial(4)), the program runs, and echo $? displays 24.  Anyway,
does anyone have any ideas why this is happening, or what I'm doing wrong?
Any help would be appreciated.  Also, I'm not a subscriber to the list, 
so please include my email in the reply.

Thanks!

Jonathan Bartlett

-- 
PGP information is available at http://members.wri.com/johnnyb/about/
	#PURPOSE - Given a number, this program computes the
	#          factorial.  For example, the factorial of
	#          3 is 3 * 2 * 1, or 6.  The factorial of
	#          4 is 4 * 3 * 2 * 1, or 24, and so on.
	#

	.section .text
	.align 4
	.globl factorial

	.type factorial,@function
factorial:
	pushl %ebp        #standard function stuff
	movl  %esp, %ebp  #standard function stuff

	movl  8(%ebp), %eax
	cmpl  $1, %eax
	je end_factorial
	decl  %eax
	pushl %eax
	call  factorial
	popl  %ebx
	movl  %eax, %ebx
	movl  8(%ebp), %eax
	imul  %ebx, %eax
end_factorial:
	leave 
	ret
	

long factorial(long s);

int main()
{
	return(factorial(4));
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]