20 lines
377 B
Text
20 lines
377 B
Text
global fact
|
|
section .text
|
|
|
|
; Input and output in EAX register
|
|
fact:
|
|
cmp eax, 1
|
|
je .done ; if eax == 1 goto done
|
|
|
|
; inductive case
|
|
push eax ; save n (ie. what EAX is)
|
|
dec eax ; n - 1
|
|
call fact ; fact(n - 1)
|
|
pop ebx ; fetch old n
|
|
mul ebx ; multiplies EAX with EBX, ie. n * fac(n - 1)
|
|
ret
|
|
|
|
.done:
|
|
; base case: return 1
|
|
mov eax, 1
|
|
ret
|