-
Notifications
You must be signed in to change notification settings - Fork 1
/
stdio.c
53 lines (46 loc) · 1.06 KB
/
stdio.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
49
50
51
52
#include "toy_crt.h"
int crt_io_init()
{
return 1;
}
int write(int fd, const void *buffer, size_t size)
{
int ret = 0;
__asm__ volatile ("movq $1, %%rax \n\t"
"movq %1, %%rdi \n\t"
"movq %2, %%rsi \n\t"
"movq %3, %%rdx \n\t"
"syscall \n\t"
:"=m"(ret)
:"m"(fd), "m"(buffer), "m"(size));
return ret;
}
int read(int fd, void *buffer, size_t size)
{
int ret = 0;
__asm__ volatile ("movq $0, %%rax \n\t"
"movq %1, %%rdi \n\t"
"movq %2, %%rsi \n\t"
"movq %3, %%rdx \n\t"
"syscall \n\t"
:"=m"(ret)
:"m"(fd), "m"(buffer), "m"(size));
return ret;
}
void putchar(char c)
{
write(1, &c, 1);
}
void puts(const char *str)
{
size_t len = strlen(str);
write(1, str, len);
putchar('\n');
}
int getchar()
{
char c;
if (read(0, &c, 1) == 1)
return 1;
return 0;
}