Skip to content

Instantly share code, notes, and snippets.

@Chris--B
Created January 12, 2013 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chris--B/4519674 to your computer and use it in GitHub Desktop.
Save Chris--B/4519674 to your computer and use it in GitHub Desktop.
int factorial(int n)
{
if(n == 0) {
return 1;
} else {
return n * factorial (n - 1);
}
}
21.file "foo.c"
.text
.globl _factorial
.def _factorial; .scl 2; .type 32; .endef
_factorial:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
cmpl $0, 8(%ebp)
jne L2
movl $1, %eax
jmp L3
L2:
movl 8(%ebp), %eax
decl %eax
movl %eax, (%esp)
call _factorial
imull 8(%ebp), %eax
L3:
leave
ret
21.file "foo.c"
.text
.p2align 2,,3
.globl _factorial
.def _factorial; .scl 2; .type 32; .endef
_factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
testl %edx, %edx
je L4
movl $1, %eax
.p2align 2,,3
L3:
imull %edx, %eax
decl %edx
jne L3
leave
ret
L4:
movl $1, %eax
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment