Skip to content

Commit 8681607

Browse files
committed
Use enum for syscalls.
1 parent aa50a1a commit 8681607

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

kernel_syscalls.c

+21-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "stdlib/syscalls.h"
77

88
uint32_t sys_write_to_screen(char* s) {
9-
fprintf(LOG, "sys_write_to_screen(%s)\n", s);
109
fprintf(SCREEN, s);
1110
return 0;
1211
}
@@ -38,22 +37,35 @@ uint32_t sys_register_input_handler(InputHandler handler) {
3837
return 0;
3938
}
4039

40+
char* syscall_name(Syscall syscall) {
41+
switch (syscall) {
42+
case (WRITE_TO_SCREEN):
43+
return "WRITE_TO_SCREEN";
44+
case (COUNT_FILES):
45+
return "COUNT_FILES";
46+
case (LIST_FILES):
47+
return "LIST_FILES";
48+
case (REGISTER_INPUT_HANDLER):
49+
return "REGISTER_INPUT_HANDLER";
50+
}
51+
}
52+
4153
uint32_t handle_syscall(struct cpu_state* cpu) {
42-
uint32_t syscall_num = cpu->eax;
54+
Syscall syscall = (Syscall) cpu->eax;
4355

44-
fprintf(LOG, "--------------------\nSYSCALL (%i)\n", syscall_num);
56+
fprintf(LOG, "--------------------\nSYSCALL (%i - %s)\n", syscall, syscall_name(syscall));
4557

46-
switch (syscall_num) {
47-
case (1):
58+
switch (syscall) {
59+
case (WRITE_TO_SCREEN):
4860
return sys_write_to_screen((char*) cpu->ebx);
49-
case (2):
61+
case (COUNT_FILES):
5062
return sys_count_files();
51-
case (3):
63+
case (LIST_FILES):
5264
return sys_list_files((struct file_t *) cpu->ebx);
53-
case (4):
65+
case (REGISTER_INPUT_HANDLER):
5466
return sys_register_input_handler((InputHandler) cpu->ebx);
5567
default:
56-
fprintf(LOG, "Unknown syscall: %x\n", syscall_num);
68+
fprintf(LOG, "Unknown syscall: %x\n", syscall);
5769
while(1){}
5870
}
5971

stdlib/syscalls.h

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
#include "types.h"
55
#include "filesystem.h"
66

7+
enum _Syscall {
8+
WRITE_TO_SCREEN,
9+
COUNT_FILES,
10+
LIST_FILES,
11+
REGISTER_INPUT_HANDLER
12+
};
13+
typedef enum _Syscall Syscall;
14+
715
void write_to_screen(char* s);
816
uint32_t count_files();
917
void list_files(struct file_t buffer[]);

stdlib/syscalls.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ global write_to_screen
44
; stack: [esp + 4] string
55
; [esp ] return address
66
write_to_screen:
7-
mov eax, 1
7+
mov eax, 0
88
mov ebx, [esp+4]
99
int 0x80
1010
ret ; return to the calling function
@@ -13,7 +13,7 @@ global count_files
1313
; count_files
1414
; stack: [esp ] return address
1515
count_files:
16-
mov eax, 2
16+
mov eax, 1
1717
int 0x80
1818
ret ; return to the calling function
1919

@@ -22,7 +22,7 @@ global list_files
2222
; stack: [esp + 4] address of buffer
2323
; [esp ] return address
2424
list_files:
25-
mov eax, 3
25+
mov eax, 2
2626
mov ebx, [esp+4]
2727
int 0x80
2828
ret ; return to the calling function
@@ -32,7 +32,7 @@ global register_input_handler
3232
; stack: [esp + 4] address of handler
3333
; [esp ] return address
3434
register_input_handler:
35-
mov eax, 4
35+
mov eax, 3
3636
mov ebx, [esp+4]
3737
int 0x80
3838
ret ; return to the calling function

0 commit comments

Comments
 (0)