17 lines
361 B
Text
17 lines
361 B
Text
global factorial
|
|
section .text
|
|
|
|
; Input in ECX register
|
|
; Output in EAX register
|
|
factorial:
|
|
mov eax, 1 ; default argument, store 1 in accumulator
|
|
|
|
.base_case:
|
|
test ecx, ecx
|
|
jnz .inductive_case ; return accumulator if n == 0
|
|
ret
|
|
|
|
.inductive_case:
|
|
mul ecx ; accumulator *= n
|
|
dec ecx ; n -= 1
|
|
jmp .base_case ; tail call
|