/
Trabalho01.c
613 lines (531 loc) · 16.8 KB
/
Trabalho01.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct endereco{
int end, pos; /*0 esquerda 1 direita*/
} endereco;
typedef struct rotulo{
char nome[101];
endereco endereco;
struct rotulo *prox;
} rotulo;
typedef struct palavras{
int num_linha, pal_esq, pal_dir, pal_int;
char inst_esq[3], inst_dir[3];
struct palavras *prox;
} palavra;
typedef struct nome{
char carac[101];
int valor;
struct nome *prox;
} nome;
/* Funcoes de reconhecimento de elementos de linguagem de montagem*/
void hexadecimal(palavra *mapa, FILE *saida);
void diretivas(char *str, endereco *end, palavra *mapa, nome *lista);
endereco muda_Endereco(endereco *end_);
void preencher_mapa(palavra *inicio, endereco *end_, char instrucao[2], char *token);
void OrdenaMapa(palavra *inicio, palavra *novo);
void instrucoes(char str[], endereco *end, palavra *mapa, rotulo *rot);
/* Funcoes de Listas Ligadas para o Mapa de Memoria */
void InicializaMapaMemoria(palavra *inicio);
void InserePalavraMapa(palavra *inicio, int valor, int linha);
void LiberaMapaPalavras(palavra *inicio);
void ImprimePalavras(palavra *inicio);
/* Funcoes de Listas Ligadas para o Mapa de Rotulos */
void InicializaMapaRotulos(rotulo *inicio);
void InsereRotuloFim(rotulo *inicio, char *token, endereco end);
void LiberaRotulos(rotulo *inicio);
void ImprimeRotulos(rotulo *inicio);
endereco ProcuraRotulo(rotulo *inicio, char *token);
/* Funcoes de Listas Ligadas para os nomes setados */
void InicializaNomes(nome *inicio);
void InsereNomeFim(nome *inicio, char *token, int valor);
void LiberaNomes(nome *inicio);
void ImprimeNomes(nome *inicio);
int ProcuraNome(nome *inicio, char *token);
int main(int argc, char *argv[]){
FILE *arq_entrada; /* associado ao arquivo de entrada */
FILE *arq_saida; /* associado ao arquivo de saida */
char delim[] = " \n";/*delimitadores dos tokens*/
char str[1001];/*string referente a cada linha*/
char *token;/*string referente a cada token*/
int tam; /* auxiliar */
palavra mapa_mem; /* mapa de memoria */
rotulo mapa_rot; /* mapa de rotulos */
nome lista_nom; /* lista de nomes */
endereco end = {0,0}; /* ponteiro de endereco */
arq_entrada = fopen(argv[1],"r");/*abre o arquivo para leitura*/
arq_saida = fopen(argv[2],"w");/*abre o arquivo para escrita*/
/* Inicializa mapa dos rotulos */
InicializaMapaRotulos(&mapa_rot);
InicializaMapaMemoria(&mapa_mem);
InicializaNomes(&lista_nom);
/* PRIMEIRA LEITURA DO ARQUIVO - MAPA DE ROTULOS */
while(!feof(arq_entrada)){
/* Carrega o primeiro token da linha de entrada */
token = strtok(fgets(str,1000,arq_entrada),delim);
while(token != NULL){
/* Calcula tamanho do token */
tam = strlen(token);
/* Teste se eh diretiva (primeiro caracter eh '.') */
if(token[0] == '.'){
diretivas(token, &end, &mapa_mem, &lista_nom);
}
/* Teste se eh roluto (ultimo caracter eh ':') */
else if(token[tam-1] == ':'){
InsereRotuloFim(&mapa_rot,token,end);
}
/* Teste se eh comentario (primeiro caracter eh '#') */
else if(token[0] == '#'){
/* Percorre ate o fim da linha */
token = strtok(NULL,"\n");
}
/* TESTAR SE EH NECESSARIO INTERPRETAR INSTRUCOES NA PRIMEIRA LEITURA */
else{
instrucoes(token, end, &mapa_mem, &mapa_rot);
}
token = strtok(NULL, delim);
}
}
/* Ordena mapa de memoria */
OrdenaMapa(&mapa_mem);
/* Volta ao inicio do arquivo e reseta o contador de posicao */
fseek(arq_entrada, 0, SEEK_SET);
end.endereco = 0;
end.pos = 0;
/* SEGUNDA LEITURA DO ARQUIVO - MAPA DE MEMORIA */
while(!feof(arq_entrada)){
/* Carrega o primeiro token da linha de entrada */
token = strtok(fgets(str,1000,arq_entrada),delim);
while(token != NULL){
/* Calcula tamanho do token */
tam = strlen(token);
/* Teste se eh diretiva (primeiro caracter eh '.') */
if(token[0] == '.'){
diretivas(token, &end, &mapa_mem, &lista_nom);
}
/* Teste se eh comentario (primeiro caracter eh '#' */
else if(token[0] == '#'){
/* Percorre ate o fim da linha */
token = strtok(NULL,"\n");
}
/* Se nao eh nenhum desses, eh uma instrucao */
else{
instrucoes(token, end, &mapa_mem, &mapa_rot);
}
token = strtok(NULL, delim);
}
}
/* Escrever arquivo de saida */
hexadecimal(&mapa_mem,arq_saida);
LiberaRotulos(&mapa_rot);
LiberaMapaPalavras(&mapa_mem);
LiberaNomes(&lista_nom);
fclose(arq_entrada);/*fecha o arquivo de entrada*/
fclose(arq_saida);/*fecha o arquivo de saida*/
return 0;
}
void hexadecimal(palavra *mapa, FILE *saida){
char linha[5], pal_int[11], pal_esq[4], pal_dir[4];
palavra *temp;
for(temp = mapa->prox; temp != NULL; temp = temp->prox){
sprintf(&linha[0], "%04X", temp->num_linha);
fprintf(saida,"%s ",linha);
if(temp->pal_int != -1){
sprintf(&pal_int[0], "%010X", temp->pal_int);
fprintf(saida,"%c%c", pal_int[0], pal_int[1]);
fprintf(saida," %c%c%c ", pal_int[2], pal_int[3], pal_int[4]);
fprintf(saida,"%c%c", pal_int[5], pal_int[6]);
fprintf(saida," %c%c%c\n", pal_int[7], pal_int[8], pal_int[9]);
}
else{
fprintf(saida,"%s ", temp->inst_esq);
sprintf(&pal_esq[0], "%03X", temp->pal_esq);
fprintf(saida,"%s ",pal_esq);
fprintf(saida,"%s ", temp->inst_dir);
sprintf(&pal_dir[0], "%03X", temp->pal_dir);
fprintf(saida,"%s\n",pal_dir);
}
}
}
void diretivas(char *str, endereco *end, palavra *mapa, nome *lista){
char delim[] = " ,\n";/*delimitadores dos tokens*/
char *token, aux3[100];
int aux, aux2, i;
if (!strcmp(str, ".org")){
token = strtok(NULL, delim);
(*end).end = (int)strtol(token, NULL, 0);
(*end).pos = 0;
}
else if (!strcmp(str, ".align")){
token = strtok(NULL, delim);
aux = (int)strtol(token, NULL, 0);
(*end).end = (((*end).end/aux) + 1) * aux;
}
else if (!strcmp(str, ".wfill")){
token = strtok(NULL, delim); /* Carrega Num de linhas que devem serem preenchidas */
aux = (int)strtol(token, NULL, 0);
token = strtok(NULL,delim); /* Carrega o Valor q deve ser colocado nas linhas */
if(lista->prox != NULL){
aux2 = ProcuraNome(lista,token); /* Testa se nao eh um nome */
if(aux2 == 1024){
aux2 = (int)strtol(token, NULL, 0);
}
}
/*Adiciona na lista do mapa de memoria N elementos de valor D */
for(i = 0; i < aux; i++){
InserePalavraMapa(mapa, aux2, (*end).end);
(*end).end++;
}
}
else if (!strcmp(str, ".word")){
token = strtok(NULL, delim);
if(lista->prox != NULL){
aux = ProcuraNome(lista,token); /* Testa se nao eh um nome */
if(aux == 1024){
aux = (int)strtol(token, NULL, 0);
}
}
InserePalavraMapa(mapa, aux, (*end).end);
(*end).end++;
}
else if (!strcmp(str, ".set")){
token = strtok(NULL, delim); /* Carrega os caracteres do nome a ser setado */
strcpy(aux3,token);
token = strtok(NULL, delim); /* Carrega valor a ser associado ao nome */
aux = (int)strtol(token, NULL, 0);
/* Insere nomes na lista */
InsereNomeFim(lista,aux3,aux);
}
}
endereco muda_Endereco(endereco *end_){
endereco aux;
aux.end = end_->end;
if(end_->pos == 0){
end_->pos = 1;
}
else if(end_->pos == 1){
end_->end++;
end_->pos = 0;
}
return (*end_);
}
void preencher_mapa(palavra *inicio, endereco *end_, char instrucao[2], char *token){
palavra *novo,*temp;
/* Percorre a lista ate o fim ou ate encontrar a linha a ser preenchida */
temp = inicio;
while(temp->prox!=NULL && temp->num_linha != end_->end){
temp = temp->prox;
}
/* Caso a linha ja exista, deve-se sobrescreve-la */
if(temp->num_linha == end_->end){
if(end_->pos == 1){
temp->pal_dir = (int)strtol(token, NULL, 0);
temp->pal_int = -1;
strcpy(temp->inst_dir,instrucao);
}else{
temp->pal_esq = (int)strtol(token, NULL, 0);
temp->pal_int = -1;
strcpy(temp->inst_esq,instrucao);
}
/* Caso contrario, cria-se uma nova */
}else{
novo = (palavra*)malloc(sizeof(palavra));
novo->num_linha = end_->end;
novo->pal_int = -1;
if(end_->pos == 1){
novo->pal_dir = (int)strtol(token, NULL, 0);
novo->pal_esq = 0;
strcpy(novo->inst_esq,"00");
strcpy(novo->inst_dir,instrucao);
}else{
novo->pal_esq = (int)strtol(token, NULL, 0);
novo->pal_dir = 0;
strcpy(novo->inst_dir,"00");
strcpy(novo->inst_esq,instrucao);
}
novo->prox = NULL;
temp->prox = novo;
}
}
void OrdenaMapa(palavra *inicio){
palavra *temp, *temp2, *aux, *aux2, *pos;
int num, teste;
for(temp = inicio->prox, temp2 = inicio->prox, pos = inicio->prox; temp->prox != NULL; temp = temp->prox){
num = temp->num_linha;
pos = temp;
for(aux = temp; aux->prox != NULL; aux = aux->prox){
teste = (*(aux->prox)).num_linha;
if(teste < num){
num = teste;
pos = aux;
}
}
if(num != temp->num_linha){
aux2 = pos->prox;
if(aux2!= NULL)
pos->prox = aux2->prox;
else
pos->prox = NULL;
if(pos != temp){
aux2->prox = temp->prox;
temp->prox = pos->prox;
pos->prox = temp;
}
else
aux2->prox = temp;
if(temp == inicio->prox)
inicio->prox = aux2;
else
temp2->prox = aux2;
temp = aux2;
}
temp2 = temp;
}
}
//int verifica(char *str){
// char c = str[0];
//if(strcmp(c ,".") == 0){
//eh rotulo, ver como esta as funcoes de rotulo
//}
//else{
//return strtol(str,NULL, 16);
//}
//}
void instrucoes(char str[], endereco *end, palavra *mapa, rotulo *rot){
char *token;
char *aux;
if(strcmp(str, "LDMQ") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"0A","000");
}
else if(strcmp(str, "LDMQM") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"09",token);
}
else if(strcmp(str, "STR") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"21",token);
}
else if(strcmp(str, "LOAD") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"01",token);
}
else if(strcmp(str, "LDN") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"02",token);
}
else if(strcmp(str, "LDABS") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"03",token);
}
else if(strcmp(str, "JMP") == 0){
//verificar esquerda e direita
end = muda_Endereco(end);
token = strtok(str,"(,");
aux = strtok(NULL,")");
if(strcmp(aux,"0:19") == 0){
preencher_mapa(end,mapa,"0D", token);
}
else if(strcmp(aux,"20:39") == 0){
preencher_mapa(end,mapa,"0E", token);
}
}
else if(strcmp(str, "JGEZ") == 0){
//verificar direita e esquerda
end = muda_Endereco(end);
token = strtok(str,"()");
aux = strtok(NULL,")");
if(strcmp(aux,"0:19") == 0){
preencher_mapa(end,mapa,"0F", token);
}
else if(strcmp(aux,"20:39") == 0){
preencher_mapa(end,mapa,"10", token);
}
}
else if(strcmp(str, "ADD") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"05",token);
}
else if(strcmp(str, "ADDABS") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"07",token);
}
else if(strcmp(str, "SUB") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"06",token);
}
else if(strcmp(str, "SUBABS") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"08",token);
}
else if(strcmp(str, "MUL") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"0B",token);
}
else if(strcmp(str, "DIV") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"0C",token);
}
else if(strcmp(str, "LSH") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"14","000");
}
else if(strcmp(str, "RSH") == 0){
end = muda_Endereco(end);
token = strtok(str,"()");
preencher_mapa(end,mapa,"15","000");
}
else if(strcmp(str, "STM") == 0){
//se operando for instrcao a direita ou a esquerda.
end = muda_Endereco(end);
token = strtok(str,"()");
aux = strtok(NULL,")");
if(strcmp(aux,"8:19") == 0){
preencher_mapa(end,mapa,"12", token);
}
else if(strcmp(aux,"28:39") == 0){
preencher_mapa(end,mapa,"13", token);
}
}
}
/*------------------------------- NAO ALTERAR POR ENQUANTO ------------------------------*/
/* IMPLEMENTAÇÃO DAS FUNÇÕES DE LISTA LIGADA MAPA MEMORIA */
void InicializaMapaMemoria(palavra *inicio) {
inicio->pal_esq = -1;
inicio->pal_dir = -1;
inicio->pal_int = -1;
inicio->num_linha = -1;
strcpy(inicio->inst_esq,"00");
strcpy(inicio->inst_dir,"00");
inicio->prox = NULL;
}
void InserePalavraMapa(palavra *inicio, int valor, int linha) {
palavra *novo,*temp,*aux;
int tam, flag = 0;
novo = (palavra*)malloc(sizeof(palavra));
novo->num_linha = linha;
novo->pal_int = valor;
novo->pal_esq = -1;
novo->pal_dir = -1;
strcpy(novo->inst_esq,"\0");
strcpy(novo->inst_dir,"\0");
novo->prox = NULL;
/* Percorre lista ate o fim e adiciona o noh */
temp = inicio;
while(temp->prox != NULL)
temp = temp->prox;
temp->prox = novo;
}
void LiberaMapaPalavras(palavra *inicio) {
palavra *temp;
for(temp = inicio->prox; temp != NULL; temp = temp-> prox){
free(temp);
}
}
void ImprimePalavras(palavra *inicio){
palavra *temp;
for(temp = inicio->prox; temp != NULL; temp = temp->prox){
if(temp->pal_int == -1)
printf("lin %d inst_esq %c%c pal_esq %d inst_dir %c%c pal_dir %d\n", temp->num_linha, temp->inst_esq[0], temp->inst_esq[1], temp->pal_esq, temp->inst_dir[0], temp->inst_dir[1], temp->pal_dir);
else
printf("lin %d pal %d\n", temp->num_linha, temp->pal_int);
}
}
/* IMPLEMENTAÇÃO DAS FUNÇÕES DE LISTA LIGADA DE ROTULOS */
void InicializaMapaRotulos(rotulo *inicio) {
inicio->endereco.end = -1;
inicio->endereco.pos = -1;
inicio->prox = NULL;
}
void InsereRotuloFim(rotulo *inicio, char *token, endereco end) {
rotulo *novo,*temp;
int tam;
novo = (rotulo*)malloc(sizeof(rotulo));
novo->endereco = end;
strcpy(novo->nome,token);
tam = strlen(token);
novo->nome[tam-1] = '\0';
novo->prox=NULL;
temp = inicio;
while(temp->prox!=NULL){
temp = temp->prox;
}
temp->prox = novo;
}
void LiberaRotulos(rotulo *inicio) {
rotulo *temp;
for(temp = inicio->prox; temp != NULL; temp = temp-> prox){
free(temp);
}
}
void ImprimeRotulos(rotulo *inicio){
rotulo *temp;
for(temp = inicio->prox; temp != NULL; temp = temp->prox){
printf("%s na linha %d e posicao %d\n", temp->nome, temp->endereco.end, temp->endereco.pos);
}
}
endereco ProcuraRotulo(rotulo *inicio, char *token){
rotulo *temp;
endereco aux;
for(temp = inicio->prox; temp != NULL; temp = temp->prox){
if (!strcmp(temp->nome, token)){
return temp->endereco;
}
}
aux.end = 1024;
return aux;
}
/* IMPLEMENTAÇÃO DAS FUNÇÕES DE LISTA LIGADA NOMES */
void InicializaNomes(nome *inicio){
inicio->valor = -1;
strcpy(inicio->carac,"00");
inicio->prox = NULL;
}
void InsereNomeFim(nome *inicio, char *token, int valor){
nome *novo,*temp;
int tam;
novo = (nome*)malloc(sizeof(nome));
novo->valor = valor;
strcpy(novo->carac,token);
novo->prox=NULL;
temp = inicio;
while(temp->prox!=NULL){
temp = temp->prox;
}
temp->prox = novo;
}
void LiberaNomes(nome *inicio){
nome *temp;
for(temp = inicio->prox; temp != NULL; temp = temp-> prox){
free(temp);
}
}
void ImprimeNomes(nome *inicio){
nome *temp;
for(temp = inicio->prox; temp != NULL; temp = temp->prox){
printf("%s valor %d\n", temp->carac, temp->valor);
}
}
int ProcuraNome(nome *inicio, char *token){
nome *temp;
for(temp = inicio->prox; temp != NULL; temp = temp->prox){
if (!strcmp(temp->carac, token)){
return temp->valor;
}
}
return 1024;
}