-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpass1.c
210 lines (173 loc) · 5.21 KB
/
pass1.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
/*
!important
Initially create 2 files input.txt & optab.txt in same directory,
add the input
After compiling pass1.c & executing a.out ,
length.txt, intermediate.txt & symtab.txt
will be created and output will be writed to it and displayed on terminal
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void passOne(char label[10], char opcode[10], char operand[10], char code[10], char mnemonic[3]);
void display();
int main()
{
// for reading from input
char label[10], opcode[10], operand[10];
// for reading from optab
char code[10], mnemonic[3];
// call the function
passOne(label, opcode, operand, code, mnemonic);
return 0;
}
void passOne(char label[10], char opcode[10], char operand[10], char code[10], char mnemonic[3])
{
int locctr, start, length;
FILE *fp1, *fp2, *fp3, *fp4, *fp5; // file pointers
// read mode
fp1 = fopen("input.txt", "r");
fp2 = fopen("optab.txt", "r");
// write mode
fp3 = fopen("symtab.txt", "w");
fp4 = fopen("intermediate.txt", "w");
fp5 = fopen("length.txt", "w");
fscanf(fp1, "%s\t%s\t%s", label, opcode, operand); // read first line
if (strcmp(opcode, "START") == 0) {
// atoi() requires stdlib.h header file , it converts ASCII to integer
start = atoi(operand); // convert operand value from string to integer and assign to start
locctr = start;
fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand); // write to output file (additional tab space as start will not have any locctr)
fscanf(fp1, "%s\t%s\t%s", label, opcode, operand); // read next line
}
else {
locctr = 0;
}
// iterate till end
while (strcmp(opcode, "END") != 0) {
// 1. transfer address and read line to output file
fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);
// 2. make symtab file with values not starting with **
if (strcmp(label, "**") != 0) {
fprintf(fp3, "%s\t%d\n", label, locctr);
}
// 3. read from optab (code and mnemonic value)
fscanf(fp2, "%s\t%s", code, mnemonic);
// 4. traverse till the end of optab file
while (strcmp(code, "END") != 0) {
if (strcmp(opcode, code) == 0) { // if opcode in input matches the one in optab, increment locctr by 3
locctr += 3;
break;
}
// read next line
fscanf(fp2, "%s\t%s", code, mnemonic);
}
// 5. Searching opcode for WORD, RESW, BYTE, RESB keywords and updating locctr
// WORD -> add 3 to locctr
if (strcmp(opcode, "WORD") == 0) {
locctr += 3;
}
// RESW -> add 3*operand to locctr
else if (strcmp(opcode, "RESW") == 0) {
locctr += (3 * (atoi(operand))); // convert operand to integer and multiply with 3
}
// BYTE -> add 1 to locctr
else if (strcmp(opcode, "BYTE") == 0) {
++locctr;
}
// RESB -> add operand to locctr
else if (strcmp(opcode, "RESB") == 0) {
locctr += atoi(operand);
}
// read next line
fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
}
// 6. transfer last line to file
fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);
// 7. Close all files
fclose(fp4);
fclose(fp3);
fclose(fp2);
fclose(fp1);
// 8. display outputs
display();
// 9. calculate length of program
length = locctr - start;
fprintf(fp5, "%d", length);
fclose(fp5);
printf("\nThe length of the code : %d\n", length);
}
void display() {
char str;
FILE *fp1, *fp2, *fp3;
// 1. Input Table
printf("\nThe contents of Input Table :\n\n");
fp1 = fopen("input.txt", "r");
str = fgetc(fp1);
while (str != EOF) {
printf("%c", str);
str = fgetc(fp1);
}
fclose(fp1);
//2. Output Table
printf("\n\nThe contents of Output Table :\n\n");
fp2 = fopen("intermediate.txt", "r");
str = fgetc(fp2);
while (str != EOF) {
printf("%c", str);
str = fgetc(fp2);
}
fclose(fp2);
// 3. Symtable
printf("\n\nThe contents of Symbol Table :\n\n");
fp3 = fopen("symtab.txt", "r");
str = fgetc(fp3);
while (str != EOF) {
printf("%c", str);
str = fgetc(fp3);
}
fclose(fp3);
}
/*
input.txt
---------
** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 2
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
** END **
optab.txt
---------
LDA 03
STA 0f
LDCH 53
STCH 57
END *
-----------------------------
symtab.txt
----------
ALPHA 2012
FIVE 2018
CHARZ 2021
C1 2022
intermediate.txt
----------------
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 2
2018 FIVE WORD 5
2021 CHARZ BYTE C'Z'
2022 C1 RESB 1
2023 ** END **
length.txt
----------
23
*/