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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define O_WRONLY 1
#define O_CREAT 0x40
#define O_TRUNC 0x200
#define O_APPEND 0x400
static long sys_write(long fd, const char* buffer, unsigned long length)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(1L), "D"(fd), "S"(buffer), "d"(length) : "memory");
return result;
}
static long sys_read(long fd, char* buffer, unsigned long length)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(2L), "D"(fd), "S"(buffer), "d"(length) : "memory");
return result;
}
static long sys_open(const char* path, long flags)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(7L), "D"(path), "S"(flags) : "memory");
return result;
}
static long sys_close(long fd)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(8L), "D"(fd) : "memory");
return result;
}
static long sys_lseek(long fd, long offset, long whence)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(9L), "D"(fd), "S"(offset), "d"(whence) : "memory");
return result;
}
static void sys_exit(long code)
{
__asm__ volatile("int $0x80" : : "a"(0L), "D"(code) : "memory");
for (;;);
}
static long sys_fs_size(const char* name)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(4L), "D"(name) : "memory");
return result;
}
static long sys_fs_read(const char* name, unsigned long offset, char* buffer, unsigned long length)
{
long result;
__asm__ volatile("int $0x80"
: "=a"(result)
: "a"(5L), "D"(name), "S"(offset), "d"(buffer), "c"(length)
: "memory");
return result;
}
static long sys_fs_write(const char* name, const char* buffer, unsigned long length)
{
long result;
__asm__ volatile("int $0x80"
: "=a"(result)
: "a"(6L), "D"(name), "S"(buffer), "d"(length)
: "memory");
return result;
}
static void put(const char* string)
{
unsigned long length = 0;
while (string[length])
length++;
sys_write(1, string, length);
}
static int get_char(void)
{
char c;
if (sys_read(0, &c, 1) <= 0)
return -1;
return (unsigned char)c;
}
static int read_line(char* buffer, int max_length)
{
int length = 0;
for (;;)
{
char character;
if (sys_read(0, &character, 1) <= 0)
break;
if (character == '\n')
break;
if (character == '\b')
{
if (length > 0)
{
length--;
sys_write(1, "\b \b", 3);
}
continue;
}
if (length < max_length - 1)
{
buffer[length++] = character;
sys_write(1, &character, 1);
}
}
buffer[length] = 0;
return length;
}
static void put_int(long value)
{
char buffer[21];
int length = 0;
int negative = value < 0;
unsigned long magnitude = negative ? (unsigned long)(-value) : (unsigned long)value;
if (magnitude == 0)
buffer[length++] = '0';
while (magnitude > 0)
{
buffer[length++] = (char)('0' + magnitude % 10);
magnitude /= 10;
}
if (negative)
buffer[length++] = '-';
for (int left = 0, right = length - 1; left < right; left++, right--)
{
char swap = buffer[left];
buffer[left] = buffer[right];
buffer[right] = swap;
}
sys_write(1, buffer, length);
}
|