aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2024-11-20 15:30:44 +0000
committerMiquel Sabaté Solà <mikisabate@gmail.com>2024-12-05 16:02:50 +0100
commit9a3324f50679177401eecdedcf80f25ddcdd9042 (patch)
treea1a324eee4aab58e3eff30f43099f726c39b3e4d
parentd15d2ecae1faaaaed3251f64fa998b8d325c5c92 (diff)
downloadfarga-9a3324f50679177401eecdedcf80f25ddcdd9042.tar.gz
farga-9a3324f50679177401eecdedcf80f25ddcdd9042.zip
usr: Add a strcmp example
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--arch/riscv/usr/basics.c20
-rw-r--r--arch/riscv/usr/string.S18
2 files changed, 38 insertions, 0 deletions
diff --git a/arch/riscv/usr/basics.c b/arch/riscv/usr/basics.c
index 9b2267e..1d822c3 100644
--- a/arch/riscv/usr/basics.c
+++ b/arch/riscv/usr/basics.c
@@ -132,6 +132,25 @@ void test_atomic_add(void)
printf("atomic_add:\t\tOK\n");
}
+extern int mstrcmp(const char *s1, const char *s2);
+
+void test_mstrcmp(void)
+{
+ char *strings[] = {
+ "hello", "", "iello", "helloa", "contammusaaquellhomedegranarditquetantissimerra",
+ };
+
+ for (uint64_t i = 0; i < 1000000000; i++) {
+ assert(mstrcmp(strings[0], strings[0]) == 0);
+ assert(mstrcmp(strings[0], strings[1]) > 0);
+ assert(mstrcmp(strings[0], strings[2]) < 0);
+ assert(mstrcmp(strings[0], strings[3]) < 0);
+ assert(mstrcmp(strings[0], strings[4]) > 0);
+ }
+
+ printf("mstrcmp:\t\tOK\n");
+}
+
int main()
{
test_factorial();
@@ -139,4 +158,5 @@ int main()
test_is_palindrome();
test_greater_than_ten();
test_atomic_add();
+ test_mstrcmp();
}
diff --git a/arch/riscv/usr/string.S b/arch/riscv/usr/string.S
index 0f3bcc1..3878074 100644
--- a/arch/riscv/usr/string.S
+++ b/arch/riscv/usr/string.S
@@ -91,3 +91,21 @@ palindrome_yes:
palindrome_no:
li a0, 0
jr ra
+
+.globl mstrcmp
+.type mstrcmp, @function
+
+// int mstrcmp(const char *, const char *);
+mstrcmp:
+1:
+ lbu t0, 0(a0)
+ lbu t1, 0(a1)
+ bne t0, t1, 2f
+ addi a0, a0, 1
+ addi a1, a1, 1
+ bnez t0, 1b
+ li a0, 0
+ ret
+2:
+ sub a0, t0, t1
+ ret