Add recursive factorial

This commit is contained in:
bl0ckeduser 2011-10-07 17:55:18 -04:00
parent fcd0a1f7e2
commit 5f669a78ce

29
programs/fact.vm Normal file
View file

@ -0,0 +1,29 @@
# recursive factorial subroutine
fact:
push eax # save caller's EAX, ECX
push ecx
push ebp # call mechanism
mov ebp, esp
mov ebx, 1 # default value = 1
cmp eax, 1 # n > 1 ?
jle end_fact # no; leave with default = 1
mov ecx, eax # yes; value = n*fact(n-1)
dec eax
call fact
mul ebx, ecx
end_fact:
pop ebp # restore everything; leave
pop ecx
pop eax
ret
# print n! for 0<n<10
start:
mov eax, 0
loop: inc eax
call fact
prn ebx
cmp eax, 10
jl loop