aboutsummaryrefslogtreecommitdiff
path: root/arch/riscv/user/basics/factorial.S
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2024-12-04 21:42:37 +0100
committerMiquel Sabaté Solà <mikisabate@gmail.com>2024-12-05 16:02:51 +0100
commitf463d544793ff83efd8b95d0bce5ec80fd8170f3 (patch)
tree9dc6cfe5d5d9a5abb9bbe1ba401dc22308c7649e /arch/riscv/user/basics/factorial.S
parentbab3fdfd61b3488e1879bc3e53ac17f41d2e8b67 (diff)
downloadfarga-f463d544793ff83efd8b95d0bce5ec80fd8170f3.tar.gz
farga-f463d544793ff83efd8b95d0bce5ec80fd8170f3.zip
Remove old test code
There is some code that I had there after messing with some of the topics on Uros' blog (https://popovicu.com/), but that became irrelevant after working on some other projects (e.g. fbos) or on other examples. This commit also brings about a major overhaul on the structure of the project. Hopefully it's now at a more ready state. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'arch/riscv/user/basics/factorial.S')
-rw-r--r--arch/riscv/user/basics/factorial.S28
1 files changed, 28 insertions, 0 deletions
diff --git a/arch/riscv/user/basics/factorial.S b/arch/riscv/user/basics/factorial.S
new file mode 100644
index 0000000..7e3df12
--- /dev/null
+++ b/arch/riscv/user/basics/factorial.S
@@ -0,0 +1,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