aboutsummaryrefslogtreecommitdiff
path: root/basics/main.c
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2024-08-12 14:50:07 +0000
committerMiquel Sabaté Solà <mikisabate@gmail.com>2024-12-05 16:02:17 +0100
commit8e968078e6aee470a02bed7728100b2df1877e90 (patch)
tree8f64d8a0e8d8e7a9329a8239fd3c57ce217d5ff7 /basics/main.c
parentc173df46a0a12fe52fdf13323f967e9117462096 (diff)
downloadfarga-8e968078e6aee470a02bed7728100b2df1877e90.tar.gz
farga-8e968078e6aee470a02bed7728100b2df1877e90.zip
basics: Add some examples on RISC-V assembly
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'basics/main.c')
-rw-r--r--basics/main.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/basics/main.c b/basics/main.c
new file mode 100644
index 0000000..207fee2
--- /dev/null
+++ b/basics/main.c
@@ -0,0 +1,70 @@
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+/*
+ * Returns the factorial for the given unsigned 64-bit integer.
+ *
+ * Implemented in factorial.S
+ */
+int factorial(uint64_t n);
+
+void test_factorial()
+{
+ assert(factorial(0) == 0);
+ assert(factorial(1) == 1);
+ assert(factorial(2) == 2);
+ assert(factorial(3) == 6);
+ assert(factorial(4) == 24);
+
+ printf("factorial:\tOK\n");
+}
+
+/*
+ * Returns a pointer with the same given string but reversed. Note that the
+ * string cannot be in read-only space since the reversal is done in-place.
+ *
+ * Implemented in string.S
+ */
+char * reverse_string(char *str);
+
+void test_reverse_string()
+{
+ assert(reverse_string(NULL) == NULL);
+ assert(reverse_string("") == "");
+
+ char s1[] = "This is a string.";
+ assert(strcmp(reverse_string(s1), ".gnirts a si sihT") == 0);
+
+ char s2[] = ".";
+ assert(strcmp(reverse_string(s2), ".") == 0);
+
+ printf("reverse_string:\tOK\n");
+}
+
+/*
+ * Returns true if the given string is a palyndrome, false otherwise.
+ *
+ * Implemented in string.S
+ */
+bool is_palyndrome(char *str);
+
+void test_is_palyndrome()
+{
+ assert(is_palyndrome(NULL) == 0);
+ assert(is_palyndrome("") == 0);
+ assert(is_palyndrome("aba") == 1);
+ assert(is_palyndrome("aaa") == 1);
+ assert(is_palyndrome("This is a a si sihT") == 1);
+
+ printf("is_palyndrome:\tOK\n");
+}
+
+int main()
+{
+ test_factorial();
+ test_reverse_string();
+ test_is_palyndrome();
+}