summaryrefslogtreecommitdiff
path: root/riscv/pow.s
diff options
context:
space:
mode:
Diffstat (limited to 'riscv/pow.s')
-rw-r--r--riscv/pow.s39
1 files changed, 39 insertions, 0 deletions
diff --git a/riscv/pow.s b/riscv/pow.s
new file mode 100644
index 0000000..8aa339d
--- /dev/null
+++ b/riscv/pow.s
@@ -0,0 +1,39 @@
+# Calculate a^b. Where: a0=a, a1=b
+#
+# Also, this is a good example of how numeric labels can be used
+#
+# Label 1: can be defined and redefined everywhere, and we can use
+# a suffix when referencing it, to specify if we want to reference
+# the previous or the next definition of the label, according to the
+# current position. 'b' for before 'f' for after.
+#
+# Ex:
+#
+# beqz a1, 1f # refers to the symbol 1 defined after this instruction
+#
+
+.globl _start
+.section .text
+pow:
+ mv a2, a0
+ li a0, 1
+
+1:
+ beqz a1, 1f
+ mul a0, a0, a2
+ addi a1, a1, -1
+ j 1b
+
+1:
+ ret
+
+_start:
+ li a0, 2
+ li a1, 3
+ jal pow
+
+ # Setup exit ecall
+ # Return value is already in a0
+
+ li a7, 93
+ ecall