-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
200 lines (179 loc) · 2.73 KB
/
io.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "header.h"
/*int rpu(u32 x)
{
char c;
if (x)
{
c = table[x % BASE];
rpu(x / BASE);
putc(c);
}
}
int rpu16(u16 x)
{
char c;
if (x)
{
c = table[x % BASE];
rpu16(x / BASE);
putc(c);
}
}
int rpl(u32 x)
{
char c;
if (x)
{
c = table[x % BASE];
rpl(x / BASE);
putc(c);
}
}
int printu(u32 x)
{
if (x==0)
putc('0');
else
BASE = 10;
rpu(x);
}
int printd(int x) //prints an integer
{
if (x == 0)
putc('0');
else
BASE = 10;
rpu16(x);
}
int printl(u32 x)
{
if (x == 0)
putc('0');
else
BASE = 10;
rpl(x);
}
int printo(u32 x) //prints x in OCTal
{
if (x == 0 )
putc(' ');
else
BASE = 8;
rpu(x);
}
int printx(u32 x) //prints x in hex
{
if (x == 0 )
putc('0');
else
BASE = 16;
rpu(x);
}
void printf(char *fmt, ...)
{
//u16 *ip
int *ip = (u16 *)&fmt + 1;
int i = 0;
char *cp = fmt;
//asm("mov ebp, FP");
//ip = FP + 3;
for(i = 0; fmt[i]; i++)
{
if (fmt[i] == '%')
{
i++;
switch(fmt[i])
{
case'c': putc(*ip); ip++;break;
case's': prints(*ip); ip++;break;
case'u': printu(*ip); ip++;break;
case'd': printd(*ip); ip++;break;
case'l': printl(*(u32*)ip++); ip++;break;
case'o': printo(*ip); ip++;break;
case'x': printx(*ip); ip++;break;
default: putc(fmt[i]);ip++;break;
}
}
else
{
putc(fmt[i]);
}
}
}
int prints(char *s)
{
int i = 0;
while(s[i])
{
putc(s[i]);
i++;
}
}
//returns a string using the assembly function getc()
int gets(char s[64])
{
int i = 0;
char c = '0';
// put carrige return and newline on front of string
// s[0] = '\n';
// s[1] = '\r';
// i = 2;
do // iterate over user keystrokes until newline is encounterd
{
c = getc();
s[i] = c;
putc(s[i]);// print out the char entered for the user to see
i++;
} while (c != 13 && i < 63);
//add \n
s[i] = '\n';
s[i+1] = '\r';
s[i+2] = 0;
}*/
int strtoint(char *str)
{
int integer = 0, i = 0, mul = 0;
//traverse to the end of the string
while(str[i] != '\n')
{i++; }
i -= 2;
while(isDigit(str[i]) != -1)
{
integer += power(10, mul) * isDigit(str[i]);
mul++;
i--;
}
return integer;
}
int isDigit(char c)
{
switch(c)
{
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
default: return -1;
}
}
int power(int x, int y)
{
int i = 0, ret = 0;
if (y == 0)
{
return 1;
}
ret = x;
for(i = 1; i < y; i++)
{
ret = ret * x;
}
//printf("power(): returning %d \n\r", ret);
return ret;
}