aboutsummaryrefslogtreecommitdiff
path: root/src/libc
diff options
context:
space:
mode:
Diffstat (limited to 'src/libc')
-rw-r--r--src/libc/math.c32
-rw-r--r--src/libc/math.h9
-rw-r--r--src/libc/stdio.c25
-rw-r--r--src/libc/stdio.h21
-rw-r--r--src/libc/string.c29
-rw-r--r--src/libc/string.h9
6 files changed, 125 insertions, 0 deletions
diff --git a/src/libc/math.c b/src/libc/math.c
new file mode 100644
index 0000000..80b1d3a
--- /dev/null
+++ b/src/libc/math.c
@@ -0,0 +1,32 @@
+#include "math.h"
+
+int pow(int x,int n){
+ if(n<0)
+ return -1;
+ else if(n==0)
+ return 1;
+ else if(n==1)
+ return x;
+ int ret=x;
+ for(int i=0;i<(n-1);i++)
+ ret*=x;
+ return ret;
+}
+
+int max(int x,int y){
+ if(x>y)
+ return x;
+ return y;
+}
+
+int min(int x,int y){
+ if(x<y)
+ return x;
+ return y;
+}
+
+int abs(int x){
+ if(x<0)
+ return -x;
+ return x;
+} \ No newline at end of file
diff --git a/src/libc/math.h b/src/libc/math.h
new file mode 100644
index 0000000..4cc37a9
--- /dev/null
+++ b/src/libc/math.h
@@ -0,0 +1,9 @@
+#ifndef MATH_H
+#define MATH_H
+
+int pow(int x,int n);
+int max(int x,int y);
+int min(int x,int y);
+int abs(int x);
+
+#endif \ No newline at end of file
diff --git a/src/libc/stdio.c b/src/libc/stdio.c
new file mode 100644
index 0000000..6e0063b
--- /dev/null
+++ b/src/libc/stdio.c
@@ -0,0 +1,25 @@
+#include "stdio.h"
+#include "string.h"
+
+extern VIDEO_STATE VS;
+
+void print(char *str){
+ int i=0;
+ while(str[i]!='\0'){
+ putchar(str[i]);
+ i++;
+ }
+}
+
+void printc(char* str,VIDEO_COLORS c){
+ VIDEO_COLORS backup=VS.fg;
+ VS.fg=c;
+ print(str);
+ VS.fg=backup;
+}
+
+void printi(int i){
+ char str[12];
+ itoa(i,str);
+ print(str);
+} \ No newline at end of file
diff --git a/src/libc/stdio.h b/src/libc/stdio.h
new file mode 100644
index 0000000..d4bd6b6
--- /dev/null
+++ b/src/libc/stdio.h
@@ -0,0 +1,21 @@
+#ifndef STDIO_H
+#define STDIO_H
+
+#include "drivers/framebuffer.h"
+
+/**
+ * Print a char* in the framebuffer
+ */
+void print(char*);
+
+/**
+ * Print a char in the framebuffer
+ */
+void printc(char*,VIDEO_COLORS c);
+
+/**
+ * Print an integer using itoa()
+ */
+void printi(int i);
+
+#endif \ No newline at end of file
diff --git a/src/libc/string.c b/src/libc/string.c
new file mode 100644
index 0000000..93a9e63
--- /dev/null
+++ b/src/libc/string.c
@@ -0,0 +1,29 @@
+#include "string.h"
+#include "math.h"
+
+void itoa(int i, char *a){
+ // Check if lower than 0
+ char neg=0;
+ if(i<0){
+ neg=1;
+ i=-i;
+ a[0]='-';
+ }
+
+ // Count number of digits
+ int len=1;
+ while(i/pow(10,len)>=1)
+ {
+ len++;
+ }
+
+ // Build string
+ int max_pow=len-1;
+ for(int j=0;j<=max_pow;j++){
+ int cur_pow=pow(10,max_pow-j);
+ char digit=i/cur_pow;
+ a[j+neg]='0'+digit;
+ i=i-digit*cur_pow; // Remove first digits (most significant)
+ }
+ a[len+neg]='\0';
+} \ No newline at end of file
diff --git a/src/libc/string.h b/src/libc/string.h
new file mode 100644
index 0000000..c3d3614
--- /dev/null
+++ b/src/libc/string.h
@@ -0,0 +1,9 @@
+#ifndef STRING_H
+#define STRING_H
+
+/**
+ * Convert int to char
+ */
+void itoa(int i, char *a);
+
+#endif \ No newline at end of file