summaryrefslogtreecommitdiff
path: root/riscv/array_search.s
diff options
context:
space:
mode:
Diffstat (limited to 'riscv/array_search.s')
-rw-r--r--riscv/array_search.s39
1 files changed, 39 insertions, 0 deletions
diff --git a/riscv/array_search.s b/riscv/array_search.s
new file mode 100644
index 0000000..bae9800
--- /dev/null
+++ b/riscv/array_search.s
@@ -0,0 +1,39 @@
+.section .data
+arr: .word 3, 5, 9, 33, 10, 59, 22, 95, 23, 24
+
+.section .text
+.globl _start
+
+
+#
+# a5 - Holds the current array index address
+# a1 - The loop counter
+# t4 - total elements in the array
+# t1 - the index - actually used to calculate the next
+# item, always the array member size, can be a loop invariant
+# t2 - holds the new index address, I'm pretty sure this can use a5 again
+get_largest_number:
+ la a5, arr
+ lw a0, (a5)
+
+ li a1, 1
+ li t4, 10
+ slli t1, a1, 2
+
+for:
+ bge a1, t4, for_exit
+ add a5, a5, t1
+ lw t3, (a5)
+ blt t3, a0, skip
+ mv a0, t3
+skip:
+ addi a1, a1, 1
+ j for
+
+for_exit:
+ ret
+
+_start:
+ call get_largest_number
+ li a7, 93
+ ecall