-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler v1.c
464 lines (403 loc) · 14.1 KB
/
assembler v1.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//random edit
typedef struct s_command {
char command_type;
char symbol[20];
char dest[4];
char comp[8];
char jump[4];
char instruction[17];
} command;
typedef struct symbols{
char label[50];
int number;
} symb;
typedef struct t_symbol{
symb symbol_table[2000];
int length;
int variable;
} table;
//SEARCH SYMBOL
//ricerca simbolo nella symbol table
int search_symbol (char* symbol, symb* s){
for (;strcmp(s->label, "END_OF_SYMBOL_TABLE") != 0; s++){ //finchè non arrivo alla fine del array
if (strcmp(s->label, symbol) == 0) //Se label corrisponde allora ritorna numero
return s->number;
}
return -1; //ritorna num negativo (non ho trovato la stringa)
}
//ADD SYMBOL
//aggiunge i simboli alla symbol table
void add_symbol (char* nospace, int num, symb* s){
if (nospace[0]=='('){ //if per permettere il riutilizzo della funzione
nospace++;
//calcolo la lunghezza per pulire adeguatamente la stringa
int length = strlen(nospace);
while(nospace[length - 1] != ')')
length--;
length--;
strncpy(s->label, nospace, length); //copio la label senza ')' applico -3 perché alla pos: strlen(nospace)-1 == '\0', strlen(nospace)-2 == '\n', strlen(nospace)-3 == ')'
}
else
strcpy(s->label, nospace); //copio label
s->number = num; //copio il numero
}
//INIZIALIZE SYMBOL TABLE
//inizializza la symbol table
symb* inizialize_symbol_table(symb* st){
add_symbol ("R0", 0, st++);
add_symbol ("R1", 1, st++);
add_symbol ("R2", 2, st++);
add_symbol ("R3", 3, st++);
add_symbol ("R4", 4, st++);
add_symbol ("R5", 5, st++);
add_symbol ("R6", 6, st++);
add_symbol ("R7", 7, st++);
add_symbol ("R8", 8, st++);
add_symbol ("R9", 9, st++);
add_symbol ("R10", 10, st++);
add_symbol ("R11", 11, st++);
add_symbol ("R12", 12, st++);
add_symbol ("R13", 13, st++);
add_symbol ("R14", 14, st++);
add_symbol ("R15", 15, st++);
add_symbol ("SCREEN", 16384, st++);
add_symbol ("KBD", 24576, st++);
add_symbol ("SP", 0, st++);
add_symbol ("LCL", 1, st++);
add_symbol ("ARG", 2, st++);
add_symbol ("THIS", 3, st++);
add_symbol ("THAT", 4, st++);
return st;
}
//REMOVE SPACES
//rimuove gli spazi
void remove_space(char *d, const char *s) {
for (; *s; ++s) {
if (*s != ' ' && *s != '\r')
*d++ = *s;
}
*d = *s;
}
//REMOVE COMMENTS
//rimuove i commenti
void remove_comment(char str[200]) {
char c = '/';
int i=0;
while (i < 20) {
if (str[i] == c)
str[i] = '\0';
i = i + 1;
}
}
//TRADUCI COMP
//traduce in bits le operazioni
void traduci_comp(char* nospace, command* current_parse){
//0
if(nospace[0]=='0')
strcpy(current_parse->comp,"0101010");
//1
if(nospace[0]=='1')
strcpy(current_parse->comp,"0111111");
//-
if(nospace[0]=='-') {
if (nospace[1]=='1')
strcpy(current_parse->comp,"0111010");
else if (nospace[1]=='A')
strcpy(current_parse->comp,"0110011");
else if (nospace[1]=='D')
strcpy(current_parse->comp,"0001111");
else if (nospace[1]=='M')
strcpy(current_parse->comp,"1110011");
}
//A
if(nospace[0]=='A'){
if(nospace[1]=='+') {
if(nospace[2]=='1')
strcpy(current_parse->comp,"0110111");
}
else if (nospace[1]=='-'){
if (nospace[2]=='1')
strcpy(current_parse->comp,"0110010");
else if (nospace[2]=='D')
strcpy(current_parse->comp,"0000011");
}
else
strcpy(current_parse->comp,"0110000");
}
//D
if(nospace[0]=='D'){
if(nospace[1]=='+'){
if(nospace[2]=='1'){
strcpy(current_parse->comp,"0011111");
}
if(nospace[2]=='A'){
strcpy(current_parse->comp,"0000010");
}
if(nospace[2]=='M'){
strcpy(current_parse->comp,"1000010");
}
}
else if(nospace[1]=='-'){
if(nospace[2]=='1'){
strcpy(current_parse->comp,"0001110");
}
if(nospace[2]=='A'){
strcpy(current_parse->comp,"0000011");
}
if(nospace[2]=='M'){
strcpy(current_parse->comp,"1010011");
}
}
else if(nospace[1]=='&'){
if(nospace[2]=='A'){
strcpy(current_parse->comp,"0000000");
}
if(nospace[2]=='M'){
strcpy(current_parse->comp,"1000000");
}
}
else if(nospace[1]=='|'){
if(nospace[2]=='A'){
strcpy(current_parse->comp,"0010101");
}
if(nospace[2]=='M'){
strcpy(current_parse->comp,"1010101");
}
}
else {
strcpy(current_parse->comp,"0001100");
}
}
//!
if(nospace[0]=='!'){
if(nospace[1]=='D'){
strcpy(current_parse->comp,"0001101");
}
if(nospace[1]=='A'){
strcpy(current_parse->comp,"0110001");
}
if(nospace[1]=='M'){
strcpy(current_parse->comp,"1110001");
}
}
//M
if(nospace[0]=='M'){
if(nospace[1]=='+'){
if(nospace[2]=='1'){
strcpy(current_parse->comp,"1110111");
}
}
else if(nospace[1]=='-'){
if(nospace[2]=='1'){
strcpy(current_parse->comp,"1110010");
}
if(nospace[2]=='D'){
strcpy(current_parse->comp,"1000111");
}
}
else {
strcpy(current_parse->comp,"1110000");
}
}
}
//TRADUCI DEST
//traduce in bits i destinatari
void traduci_dest(char* nospace, command* current_parse){
int pos = 0;
if(nospace[pos]=='A'){
current_parse->dest[0] = '1';
pos++;
}
if(nospace[pos]=='M'){
current_parse->dest[2] = '1';
pos++;
}
if(nospace[pos]=='D')
current_parse->dest[1] = '1';
}
//TRADUCI JUMP
//traduzione in bits dei jump TODO:ottimizzazione
void traduci_jump(char* nospace, command* current_parse){
if(nospace[0] == 'J'){
if(nospace[1]=='G'){
if(nospace[2]=='T'){
strcpy(current_parse->jump,"001");
}
else {
strcpy(current_parse->jump,"011");
}
}
else if(nospace[1] == 'E'){
strcpy(current_parse->jump,"010");
}
else if(nospace[1] == 'L'){
if(nospace[2] == 'E'){
strcpy(current_parse->jump,"110");
} else {
strcpy(current_parse->jump,"100");
}
}
else if(nospace[1] == 'M'){
strcpy(current_parse->jump,"111");
}
else {
strcpy(current_parse->jump,"101");
}
}
}
//ISIN
//controllo presenza carattere in una stringa
int isin(char* c, const char d){
while(*c!='\0'){
if( *c==d) return 1;
else c++;
}
return 0;
}
//TRASLATE C
//assemblaggio c-istr CHANGE
void translate(char* nospace, command* current_parse){
int dj = isin(nospace, '=') + isin(nospace, ';') * 10; //se d -> 1; se j -> 10; se d&j -> 11; CHANGE
//comp sempre presente
char y[4]={'1','1','1','\0'}; //i 3 bit identificativi della c-instr
strcpy(current_parse->dest,"000"); //considero inizialmente NULL
strcpy(current_parse->jump,"000"); //considero inizialmente NULL
//CHANGE
if(dj%10) { //Se dest presente
traduci_dest(nospace,current_parse); //traduzione dest
while(*nospace!='=')
nospace++; //mi sposto fino alla parte comp
nospace++;
}
traduci_comp(nospace,current_parse); //traduzione comp
if(dj/10) { //Se jump presente
while(*nospace!=';')
nospace++; //mi sposto fino alla parte jump
nospace++;
traduci_jump(nospace,current_parse); //traduco jump
}
//assemblo l'istruzione
strcat(current_parse->instruction, y);
strcat(current_parse->instruction, current_parse->comp);
strcat(current_parse->instruction, current_parse->dest);
strcat(current_parse->instruction, current_parse->jump);
current_parse->instruction[16]='\0'; //pulisco la stringa
}
//TRANSLATE A
//traduzione in A-instr
void translateA(FILE *fileout, command* p, table* t){
char x[17]; //variabile temporanea in cui salvo l'istruzione binaria
int b=0; //booleano
if(!(p->symbol[0]>='0' && p->symbol[0]<='9')) //segnalo se è numero o simbolo (1 = simbolo, 0 = numero)
b=1;
int n = -1;
if (b==0)
n = atoi(p->symbol); //se numero
else{
n = search_symbol(p->symbol, t->symbol_table); //se simbolo
//Se variabile
if (n == -1){
add_symbol (p->symbol, t->variable, &t->symbol_table[t->length]); //aggiunta variabile
n = t->variable; //trovo il numero corrispondente
t->variable++;
t->length++;
add_symbol("END_OF_SYMBOL_TABLE", -1, &t->symbol_table[t->length]);
}
}
//calcolo valore binario e salvo in x
int i = 15;
while (n > 0)
{
if (n % 2 == 0)
x[i] = '0';
else
x[i] = '1';
n = n / 2;
i--;
}
//aggiungo gli zeri
while(i>0){
x[i] = '0';
i--;
}
x[i]='0';
x[16] = '\0'; //pulisco stringa
strcpy(p->instruction, x); //copio nell'istruzione vera e propria
}
//FILLER
//serve per comprendere se un comando è A-instr, C-instr o Etichetta e agisce di conseguenza.
command* filler(FILE *fileout, char* nospace, command* current_parse, table* t){
//capisco quale dellle 3 possibilità è
if (nospace[0]=='@')
current_parse->command_type = 'A'; //Tipo A
else
current_parse->command_type = 'C'; //Tipo C
//applico
if (current_parse->command_type=='A') {
strncpy(current_parse->symbol, ++nospace, strlen(nospace) - 2); //copio l'istr. senza il @ (prima incremento poi copio)
translateA(fileout, current_parse, t); //traduco in comando A
}
else if (current_parse->command_type=='C')
translate(nospace,current_parse); //traduco in comando C
}
//PARSER
//sequenza di istruzioni generali per l'assemblaggio del istruzione in binario
command* parser(FILE *fileout, char* str, table* t) {
char nospace[200]; //istruzione hack
command *current_parse = malloc(sizeof(command)); //istanzio uno spazio pari a 'command'
command *trash = malloc(sizeof(command)); //istanzio una nuova variabile in caso sia un comando inutile
//TODO: vedere se si può evitare l'utilizzo di trash utilizzando una unica variaibile
trash->instruction[0]='c'; //Assegno a trash 'c' per identificarlo come commento
remove_space(nospace, str); //rimuove gli spazi
remove_comment(nospace); //rimuove i commenti
if (nospace[1]=='\0'||nospace[1]=='\n'||nospace[0]=='(') //se l'istr è un commento o riga vuota ritorno trash CHANGE
return trash;
filler(fileout, nospace, current_parse, t); //Vero e proprio assemblaggio del comando hack
return current_parse; //ritorno l'istruzione binaria
}
//MAIN
int main(int argc, char **argv) {
FILE *filein, *fileout; //file di input e output
command *current; //puntatore del comando in questione
char instr[200]={'\0'}; //riga in questione
table st; //symbol table TODO: deve diventare una lista per occupare meno spazio in memoria
//TODO: implementare la lista
//IMPORTANTE: L'array DEVE essere maggiore rispetto alla quantità di label del filein, altrimenti scrittura sporca
symb *s = st.symbol_table;
filein = fopen(argv[1], "r"); //assegnazione del file di input
fileout = fopen(argv[2], "w"); //assegnazione del file di output
s = inizialize_symbol_table(s);
st.length = 23;
st.variable = 16;
int riga = 0;
char nospace[200];
while(fgets(instr, sizeof(instr), filein)!=NULL){ //while ci sono righe di grandezza massima 'instr' nel file 'filein'. Se ci sono assegnarle alla variabile (instr).
remove_space(nospace, instr); //rimuove gli spazi
remove_comment(nospace); //rimuove i commenti
if (nospace[0]=='('){
add_symbol(instr, riga, s);
s++; //sposto alla cella successiva
st.length++; //incremento contatore lunghezza array
}
if (nospace[0]!='\0' && nospace[0]!='\n' && nospace[0]!='(') //se l'istr è un commento o riga vuota ritorno trash CHANGE
riga++;
}
add_symbol("END_OF_SYMBOL_TABLE", -1, s);
fseek (filein, 0, SEEK_SET); //sposto l'offset al punto di inizio del file
//SECONDA PASSATA
//while con fgets e le righe in una lista
while(fgets(instr, sizeof(instr), filein)!=NULL){ //while ci sono righe di grandezza massima 'instr' nel file 'filein'. Se ci sono assegnarle alla variabile (instr).
current = parser(fileout, instr, &st); //traduco l'istruzione in binario
if (current->instruction[0]!='c'){ //controllo che l'istruzione non sia un commento
current->instruction[16] = '\0'; //pulisco da memoria sporca
//printf("Binary: %s\n", current->instruction);
fprintf(fileout, "%s\n", current->instruction); //scrivo l'istruzione nel file di output
}
}
fclose(filein); //chiudo file di input
fclose(fileout); //chiudo file di output
return (0);
}