-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasm.c
275 lines (231 loc) · 5.99 KB
/
rasm.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define ARITHMETIC(OP, ID) if (sscanf(instruction, OP" %i %i %i", &arg1, &arg2, &arg3) == 3) {\
if (arg1 > 15) {\
puts("register1 value too big");\
return 0;\
}\
if (arg2 > 15) {\
puts("register2 value too big");\
return 0;\
}\
if (arg2 > 15) {\
puts("register3 value too big");\
return 0;\
}\
result[0] = ID; \
result[1] = arg1; \
result[2] = arg2; \
result[3] = arg3; \
return 1; \
}\
typedef struct {
char name[16];
int action;
} Label;
int labelValue(Label labels[], int label_count, char* label)
{
for (int index = 0; index < label_count; index++) {
if (strcmp(labels[index].name, label) == 0) {
return labels[index].action;
}
}
return -1;
}
int translateInstruction(char* instruction, Label labels[], int label_count, uint8_t result[])
{
int arg1;
int arg2;
int arg3;
char sarg[16];
if (strcmp(instruction, "hlt") == 0) {
result[0] = 0;
result[1] = 0;
result[2] = 0;
result[3] = 0;
return 1;
}
if (sscanf(instruction, "srv %i %i", &arg1, &arg2) == 2) {
if (arg1 > 15) {
puts("register value too big");
return 0;
}
if (arg2 > 255) {
puts("value too big");
return 0;
}
result[0] = 1;
result[1] = arg1;
result[2] = arg2 / 16;
result[3] = arg2 % 16;
return 1;
}
ARITHMETIC("add", 2)
ARITHMETIC("sub", 3)
ARITHMETIC("mul", 4)
ARITHMETIC("div", 5)
ARITHMETIC("oor", 6)
ARITHMETIC("and", 7)
ARITHMETIC("xor", 8)
if (sscanf(instruction, "jmp %i", &arg1) == 1) {
if (arg1 > 63) {
puts("position value too big");
return 0;
}
result[0] = 9;
result[1] = arg1 / 16;
result[2] = arg1 % 16;
result[3] = 0;
return 1;
}
if (sscanf(instruction, "jmp _%15s", sarg) == 1) {
arg1 = labelValue(labels, label_count, sarg);
if (arg1 == -1) {
printf("Label %s doesnt exist\n", sarg);
return 0;
}
result[0] = 9;
result[1] = arg1 / 16;
result[2] = arg1 % 16;
result[3] = 0;
return 1;
}
if (sscanf(instruction, "jrz %i %i", &arg1, &arg2) == 2) {
if (arg1 > 15) {
puts("register value too big");
return 0;
}
if (arg2 > 63) {
puts("position value too big");
return 0;
}
result[0] = 10;
result[1] = arg1;
result[2] = arg2 / 16;
result[3] = arg2 % 16;
return 1;
}
if (sscanf(instruction, "jrz %i _%15s", &arg1, sarg) == 2) {
if (arg1 > 15) {
puts("register value too big");
return 0;
}
arg2 = labelValue(labels, label_count, sarg);
if (arg2 == -1) {
printf("Label %s doesnt exist\n", sarg);
return 0;
}
result[0] = 10;
result[1] = arg1;
result[2] = arg2 / 16;
result[3] = arg2 % 16;
return 1;
}
if (sscanf(instruction, "set %i %i", &arg1, &arg2) == 2) {
if (arg1 > 15) {
puts("position value too big");
return 0;
}
if (arg2 > 15) {
puts("register value too big");
return 0;
}
result[0] = 11;
result[1] = arg1;
result[2] = arg2;
result[3] = 0;
return 1;
}
if (sscanf(instruction, "get %i %i", &arg1, &arg2) == 2) {
if (arg1 > 15) {
puts("position value too big");
return 0;
}
if (arg2 > 15) {
puts("register value too big");
return 0;
}
result[0] = 12;
result[1] = arg1;
result[2] = arg2;
result[3] = 0;
return 1;
}
if (sscanf(instruction, "sys %i %i", &arg1, &arg2) == 2) {
if (arg1 > 15) {
puts("position value too big");
return 0;
}
if (arg2 > 15) {
puts("register value too big");
return 0;
}
result[0] = 13;
result[1] = arg1;
result[2] = arg2;
result[3] = 0;
return 1;
}
ARITHMETIC("equ", 14)
return 0;
}
void translate(FILE* from, FILE* to)
{
char line[64];
int action = 0;
Label labels[16] = {};
int label_count = 0;
uint8_t instruction[4] = {};
int i;
char lines[64][64];
int line_count = 0;
while ((i = fscanf(from, "\n%[^\n]s\n", line)) == 1) { // i have no idea
if (line[0] == 0) {
continue;
}
if (line[0] == '#') {
continue;
}
if (line[0] == '_') {
labels[label_count].action = line_count;
if (sscanf(line, "_%15s:", labels[label_count].name) != 1) {
puts("label not syntacticaly correct");
return;
}
int index;
char* name = labels[label_count].name;
for (index = 0; name[index]; index++) {}
name[index - 1] = 0; // trimming last char
label_count++;
continue;
}
strcpy(lines[line_count], line);
line_count++;
}
for (; action < line_count; action++) {
if (!translateInstruction(lines[action], labels, label_count, instruction)) {
printf("error at action %i", action);
return;
}
//printf("%i %i %i %i\n", instruction[0], instruction[1], instruction[2], instruction[3]);
fwrite(&instruction, sizeof(uint8_t), 4, to);
}
}
int main(int argc, char** argv)
{
if (argc < 3) {
return -1;
}
FILE* f = fopen(argv[1], "r");
FILE* t = fopen(argv[2], "wb");
if (f == NULL || t == NULL) {
puts("file not found");
return -1;
}
translate(f, t);
fclose(f);
fclose(t);
return 0;
}