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
|
// shared syscall stubs + tiny helpers for revel ring 3 programs
static long sys_write(const char* buffer, unsigned long length)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(1L), "D"(buffer), "S"(length) : "memory");
return result;
}
static long sys_read(void)
{
long result;
__asm__ volatile("int $0x80" : "=a"(result) : "a"(2L) : "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 void put(const char* string)
{
unsigned long length = 0;
while (string[length])
length++;
sys_write(string, length);
}
static int read_line(char* buffer, int max_length)
{
int length = 0;
for (;;)
{
char character = (char)sys_read();
if (character == '\n')
break;
if (character == '\b')
{
if (length > 0)
{
length--;
sys_write("\b \b", 3);
}
continue;
}
if (length < max_length - 1)
{
buffer[length++] = character;
sys_write(&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(buffer, length);
}
|