Skip to content

Instantly share code, notes, and snippets.

@fujidig
Last active March 17, 2016 09:17
Show Gist options
  • Save fujidig/d9992bd0fe940ea8c1eb to your computer and use it in GitHub Desktop.
Save fujidig/d9992bd0fe940ea8c1eb to your computer and use it in GitHub Desktop.
BITS 32
segment .text
global _factorial
_factorial:
push ebp
mov ebp, esp
push ebx
mov ebx, [ebp+8]
cmp ebx, 0
jnz .l1
mov eax, 1
pop ebx
pop ebp
ret
.l1:
mov eax, ebx
dec eax
push eax
call _factorial
add esp, 4
imul eax, ebx
pop ebx
pop ebp
ret
testfact.exe: testfact.obj factorial.obj
link testfact.obj factorial.obj
testfact.obj: testfact.c
cl /c testfact.c
factorial.obj: factorial.asm
nasm -f win32 factorial.asm
#include <stdio.h>
int factorial(int n);
int main()
{
printf("%d\n", factorial(10));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment