forked from kalpitjain0407/SIC-Assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
118 lines (97 loc) · 2.66 KB
/
reader.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
struct OPTAB Read_OpCodes() {
char regs[REGISTERS][2][10];
char codes[OPCODE_SIZE][4][10];
int i=0, j=0;
FILE *fp;
struct OPTAB optab;
fp = fopen("instruction", "r");
if(fp==NULL) {
printf("File not Found.");
return optab;
}
// Fill in the register codes
i=0;j=0;
do {
if(j==2){
i++;
j=0;
}
fscanf(fp, "%s", regs[i][j]);
j++;
}while(strcmp(regs[i][0], "OPCODE")!=0);
// Fill in the OpCodes
i=0;j=0;
do {
if(j==4){
i++;
j=0;
}
fscanf(fp, "%s", codes[i][j]);
j++;
}while(strcmp(codes[i-1][0], "END")!=0);
memcpy(optab.reg, regs, sizeof(regs));
memcpy(optab.code, codes, sizeof(codes));
return optab;
}
struct IPCODE Read_IP_Code() {
char line[MAX_IP_Lines][IP_COLUMNS][100];
int i=0, j=0, max_cols = 3;
FILE *fp;
struct IPCODE ipcode;
fp = fopen("input", "r");
if(fp==NULL) {
printf("File not Found.");
return ipcode;
}
i=0;j=0;
do {
if(line[i][0][strlen(line[i][0])-1] == ':') max_cols = 3;
else max_cols = 2;
if(j==max_cols) {
i++;
j=0;
}
fscanf(fp, "%s", line[i][j]);
j++;
}while(strcmp(line[i-1][0], "END")!=0);
for(int k=0;k<i;k++) {
if(line[k][0][strlen(line[k][0])-1] != ':') {
memcpy(line[k][2], line[k][1], sizeof(line[k][1]));
memcpy(line[k][1], line[k][0], sizeof(line[k][0]));
memset(line[k][0], 0, sizeof(line[k][0]));
}
}
memcpy(ipcode.lines, line, sizeof(line));
return ipcode;
}
void Read_INTMD_File(struct INTMD *intmdFile, struct IPCODE ipcode, int total_lines) {
int i=0, j;
FILE *fp;
fp = fopen("intermediate.txt", "r");
if(fp==NULL) {
printf("File not Found.");
return;
}
i=0;j=0;
do {
char addr[5];
char label[100];
char mnemonic[100];
char operand[100];
fscanf(fp, "%s", addr);
memcpy(intmdFile[j].addr, addr, sizeof(addr));
if(strlen(ipcode.lines[j][0])>0){
fscanf(fp, "%s", label);
memcpy(intmdFile[j].label, label, sizeof(label));
} else {
memset(intmdFile[j].label, 0, sizeof(intmdFile[j].label));
// break;
}
fscanf(fp, "%s", mnemonic);
memcpy(intmdFile[j].mnemonic, mnemonic, sizeof(mnemonic));
fscanf(fp, "%s", operand);
memcpy(intmdFile[j].operand, operand, sizeof(operand));
j++;
} while(j<=total_lines+1);
return;
}