hazel/user/shell.c

110 lines
1.9 KiB
C
Raw Permalink Normal View History

2024-07-28 10:11:15 -04:00
#include "../lib/strcmp.h"
static const char test[] = "Hello\n";
int strlen(const char *str) {
const char *s;
for (s = str; *s; s++)
;
return s - str;
}
void putc(const char c) {
asm volatile (
"movl $1, %%eax\n\t"
"movl $1, %%edi\n\t"
"movl %0, %%esi\n\t"
"movl $1, %%edx\n\t"
"int $0x80"
:
: "r"(&c)
: "%eax", "%edi", "%esi", "%edx"
);
}
2024-07-28 10:11:15 -04:00
void puts(const char *str) {
asm volatile (
"movl $1, %%eax\n\t"
"movl $1, %%edi\n\t"
"movl %0, %%esi\n\t"
"movl %1, %%edx\n\t"
2024-07-28 10:11:15 -04:00
"int $0x80"
:
: "r"(str), "r"(strlen(str))
: "%eax", "%edi", "%esi", "%edx"
2024-07-28 10:11:15 -04:00
);
}
void read(int fd, void *buf, int size) {
asm volatile (
"movl $0, %%eax\n\t"
"int $0x80"
:
: "D"(fd), "d"(size), "S"(buf)
: "eax"
);
}
void reboot() {
asm volatile (
"movl $2, %%eax\n\t"
"int $0x80"
:
:
: "eax"
);
}
uint64_t uptime() {
uint32_t low = 0;
uint32_t high = 0;
asm volatile (
"movl $3, %%eax\n\t"
"movl %0, %%esi\n\t"
"movl %1, %%edi\n\t"
"int $0x80"
:
: "r" (&low), "r" (&high)
: "eax", "esi"
);
return (uint64_t)low | ((uint64_t)high << 32);
}
2024-07-28 10:11:15 -04:00
extern char *buf;
void print_int(int num){
if (num < 0)
{
putc('-');
num = -num;
}
if (num > 9) print_int(num/10);
putc(('0'+ (num%10)));
}
2024-07-28 10:11:15 -04:00
void _start() {
char buf1[256] = {0};
//puts("Welcome to the Hazel user shell!\n");
puts(test);
2024-07-28 10:11:15 -04:00
while (1) {
puts("$ ");
2024-07-28 10:11:15 -04:00
read(0, buf1, 256);
if (!strcmp(buf1, "SKIBIDI")) {
puts("dop dop yes yes!\n");
} else if (!strcmp(buf1, "REBOOT")) {
reboot();
} else if (!strcmp(buf1, "UPTIME")) {
uint64_t ticks = uptime();
print_int(ticks);
puts("\n");
}
else {
puts("unknown command!\n");
}
2024-07-28 10:11:15 -04:00
}
}