-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloworld.c
122 lines (104 loc) · 2.12 KB
/
helloworld.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
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
#include "types.h"
void exit(uint32_t exitcode) {
asm volatile ("xor %%eax, %%eax\n"
"int $0x80" :: "c"(exitcode) : "eax");
}
uint32_t fork() {
uint32_t pid;
asm volatile ("int $0x80" :"=b"(pid):"a"(1));
return pid;
}
void sleep(uint32_t ms) {
ms /= 10;
asm volatile ("int $0x80" ::"a"(2), "c"(ms));
}
void itoa (char *buf, int base, int d)
{
char *p = buf;
char *p1, *p2;
unsigned long ud = d;
int divisor = 10;
/* If %d is specified and D is minus, put `-' in the head. */
if (base == 'd' && d < 0)
{
*p++ = '-';
buf++;
ud = -d;
}
else if (base == 'x')
divisor = 16;
/* Divide UD by DIVISOR until UD == 0. */
do
{
int remainder = ud % divisor;
*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
}
while (ud /= divisor);
/* Terminate BUF. */
*p = 0;
/* Reverse BUF. */
p1 = buf;
p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
}
void printf (const char *format, ...) {
char **arg = (char **) &format;
int c;
char outbuf[255];
int pos = 0;
char buf[20];
arg++;
while ((c = *format++) != 0)
{
if (c != '%')
outbuf[pos++] = c;
else
{
char *p;
c = *format++;
switch (c)
{
case 'd':
case 'u':
case 'x':
itoa (buf, c, *((int *) arg++));
p = buf;
goto string;
break;
case 's':
p = *arg++;
if (! p)
p = "(null)";
string:
while (*p)
outbuf[pos++] = *p++;
break;
default:
outbuf[pos++] = (*((int *) arg++));
break;
}
}
}
outbuf[pos] = 0;
asm volatile ("int $0x80" :: "a"(3), "c"(outbuf));
}
int main() {
if(fork())
return 0;
uint32_t pid = fork();
volatile uint32_t seconds = 0;
//printnum(99999);
while(true) {
sleep(1000);
printf("I am %s and i am %d seconds in\n", (pid==0?"the child":"the parent"), ++seconds);
//printnum(++seconds);
}
return 0;
}