aboutsummaryrefslogtreecommitdiff
path: root/arch/riscv/user/basics/factorial.S
blob: 7e3df12843c816b8a3c45a860c0c958af3e8143c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
.text
.globl factorial
.type factorial, @function

factorial:
	// If input <= 1, just return the given thing.
	addi t0, zero, 1
	bgt a0, t0, else
	jr ra
else:
	// Preserve the argument and the return address before the call.
	addi sp, sp, -16
	sd a0, 0(sp)
	sd ra, 8(sp)

	// Call the same function but with a value of argument-1.
	addi a0, a0, -1
	call factorial

	// Restore back the original argument and the return address. Then multiply
	// the original argument with the returned one from the previous call.
	ld t1, 0(sp)
	ld ra, 8(sp)
	addi sp, sp, 16
	mul a0, t1, a0

	// And return to the caller.
	jr ra