-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
48 lines (38 loc) · 1.11 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#define WRITE_SYSCALL_NUM "4"
#define OPEN_SYSCALL_NUM "5"
#define O_RDONLY 0x00000000 /* open for reading only */
#define O_WRONLY 0x00000001 /* open for writing only */
int write(int fd, char *str, int len)
{
int error;
asm volatile("xor r1, r1, r1\n\t"
"ori r1, r1, "WRITE_SYSCALL_NUM"\n\t"
"mv r2, %[fd]\n\t"
"mv r3, %[str]\n\t"
"mv r4, %[len]\n\t"
"scall\n\t"
"mv %[error], r1"
: [error] "=&r" (error) : [fd] "r" (fd), [str] "r" (str), [len] "r" (len) : "r1", "r2", "r3", "r4");
return error;
}
int open(const char *str, int mode)
{
int fd;
asm volatile("xor r1, r1, r1\n\t"
"ori r1, r1, "OPEN_SYSCALL_NUM"\n\t"
"mv r2, %[str]\n\t"
"mv r3, %[mode]\n\t"
"scall\n\t"
"mv %[fd], r1"
: [fd] "=&r" (fd) : [str] "r" (str), [mode] "r" (mode) : "r1", "r2", "r3");
return fd;
}
int main(void)
{
int error, fd_stdin, fd_stdout, fd_stderr;
fd_stdin = open("/dev/console", O_RDONLY);
fd_stdout = open("/dev/console", O_WRONLY);
fd_stderr = open("/dev/console", O_WRONLY);
error = write(fd_stdout, "hello, world!\n", 14);
return 0;
}