September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,11 @@
global factorial
section .text
; Input in ECX register (greater than 0!)
; Output in EAX register
factorial:
mov eax, 1
.factor:
mul ecx
loop .factor
ret

View file

@ -0,0 +1,20 @@
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

View file

@ -0,0 +1,17 @@
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