forked from aromalsanthosh/System-Softwarwe-Lab-S5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglepass.c
230 lines (209 loc) · 6.58 KB
/
singlepass.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// itoa() manual implementation as its not ANSI Standard
//start of itoa block
// Function to swap two numbers
void swap(char *x, char *y) {
char t = *x; *x = *y; *y = t;
}
// Function to reverse `buffer[i…j]`
char* reverse(char *buffer, int i, int j)
{
while (i < j) {
swap(&buffer[i++], &buffer[j--]);
}
return buffer;
}
// Iterative function to implement `itoa()` function in C
char* itoa(int value, char* buffer, int base)
{
// invalid input
if (base < 2 || base > 32) {
return buffer;
}
// consider the absolute value of the number
int n = abs(value);
int i = 0;
while (n)
{
int r = n % base;
if (r >= 10) {
buffer[i++] = 65 + (r - 10);
}
else {
buffer[i++] = 48 + r;
}
n = n / base;
}
// if the number is 0
if (i == 0) {
buffer[i++] = '0';
}
// If the base is 10 and the value is negative, the resulting string
// is preceded with a minus sign (-)
// With any other base, value is always considered unsigned
if (value < 0 && base == 10) {
buffer[i++] = '-';
}
buffer[i] = '\0'; // null terminate string
// reverse the string and return it
return reverse(buffer, 0, i - 1);
}
//end of itoa block
void main()
{
char opcode[10], operand[10], label[10], a[10], ad[10], symbol[10], ch;
char code[10][10], code1[10][10] = {"33", "44", "53", "57"};
char mnemonic[10][10] = {"START", "LDA", "STA", "LDCH", "STCH", "END"};
char mnemonic1[10][10] = {"LDA", "STA", "LDCH", "STCH"};
int locctr, start, length, i = 0, j = 0, k, l = 0;
int st, diff, address, add, len, actual_len, finaddr, prevaddr;
FILE *fp1, *fp2, *fp3, *fp4, *fp5, *fp6, *fp7;
fp1 = fopen("input.txt", "r");
fp2 = fopen("symtab.txt", "w");
fp3 = fopen("intermediate.txt", "w");
fscanf(fp1, "%s%s%s", label, opcode, operand);
if (strcmp(opcode, "START") == 0)
{
start = atoi(operand);
locctr = start;
fprintf(fp3, "%s\t%s\t%s\n", label, opcode, operand);
fscanf(fp1, "%s%s%s", label, opcode, operand);
}
else
locctr = 0;
while (strcmp(opcode, "END") != 0)
{
fprintf(fp3, "%d", locctr);
if (strcmp(label, "**") != 0)
fprintf(fp2, "%s\t%d\n", label, locctr);
strcpy(code[i], mnemonic[j]);
while (strcmp(mnemonic[j], "END") != 0)
{
if (strcmp(opcode, mnemonic[j]) == 0)
{
locctr += 3;
break;
}
strcpy(code[i], mnemonic[j]);
j++;
}
if (strcmp(opcode, "WORD") == 0)
locctr += 3;
else if (strcmp(opcode, "RESW") == 0)
locctr += (3 * (atoi(operand)));
else if (strcmp(opcode, "RESB") == 0)
locctr += (atoi(operand));
else if (strcmp(opcode, "BYTE") == 0)
++locctr;
fprintf(fp3, "\t%s\t%s\t%s\n", label, opcode, operand);
fscanf(fp1, "%s%s%s", label, opcode, operand);
}
fprintf(fp3, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);
length = locctr - start;
fclose(fp3);
fclose(fp2);
fclose(fp1);
printf("\n\nThe contents of Input file:\n\n");
fp1 = fopen("input.txt", "r");
ch = fgetc(fp1);
while (ch != EOF)
{
printf("%c", ch);
ch = fgetc(fp1);
}
printf("\n\nLength of the input program is %d.", length);
printf("\n\nThe contents of Symbol Table:\n\n");
fp2 = fopen("symtab.txt", "r");
ch = fgetc(fp2);
while (ch != EOF)
{
printf("%c", ch);
ch = fgetc(fp2);
}
fclose(fp2);
fclose(fp1);
fp4 = fopen("output.txt", "w");
fp5 = fopen("symtab.txt", "r");
fp6 = fopen("intermediate.txt", "r");
fp7 = fopen("objcode.txt", "w");
fscanf(fp6, "%s%s%s", label, opcode, operand);
while (strcmp(opcode, "END") != 0)
{
prevaddr = address;
fscanf(fp6, "%d%s%s%s", &address, label, opcode, operand);
}
finaddr = address;
fclose(fp6);
fp6 = fopen("intermediate.txt", "r");
fscanf(fp6, "%s%s%s", label, opcode, operand);
if (strcmp(opcode, "START") == 0)
{
fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand);
fprintf(fp7, "H^%s^00%s^00%d\n", label, operand, finaddr);
fscanf(fp6, "%d%s%s%s", &address, label, opcode, operand);
st = address;
diff = prevaddr - st;
fprintf(fp7, "T^00%d^%d", address, diff);
}
while (strcmp(opcode, "END") != 0)
{
if (strcmp(opcode, "BYTE") == 0)
{
fprintf(fp4, "%d\t%s\t%s\t%s\t", address, label, opcode, operand);
len = strlen(operand);
actual_len = len - 3;
fprintf(fp7, "^");
for (k = 2; k < (actual_len + 2); k++)
{
itoa(operand[k], ad, 16);
fprintf(fp4, "%s", ad);
fprintf(fp7, "%s", ad);
}
fprintf(fp4, "\n");
}
else if (strcmp(opcode, "WORD") == 0)
{
len = strlen(operand);
itoa(atoi(operand), a, 10);
fprintf(fp4, "%d\t%s\t%s\t%s\t00000%s\n", address, label, opcode, operand, a);
fprintf(fp7, "^00000%s", a);
}
else if ((strcmp(opcode, "RESB") == 0) || (strcmp(opcode, "RESW") == 0))
fprintf(fp4, "%d\t%s\t%s\t%s\n", address, label, opcode, operand);
else
{
while (strcmp(opcode, mnemonic1[l]) != 0)
l++;
if (strcmp(operand, "COPY") == 0)
fprintf(fp4, "%d\t%s\t%s\t%s\t%s0000\n", address, label, opcode, operand, code1[l]);
else
{
rewind(fp5);
fscanf(fp5, "%s%d", symbol, &add);
while (strcmp(operand, symbol) != 0)
fscanf(fp5, "%s%d", symbol, &add);
fprintf(fp4, "%d\t%s\t%s\t%s\t%s%d\n", address, label, opcode, operand, code1[l], add);
fprintf(fp7, "^%s%d", code1[l], add);
}
}
fscanf(fp6, "%d%s%s%s", &address, label, opcode, operand);
}
fprintf(fp4, "%d\t%s\t%s\t%s\n", address, label, opcode, operand);
fprintf(fp7, "\nE^00%d", st);
printf("\nObject Program has been generated.");
fclose(fp7);
fclose(fp6);
fclose(fp5);
fclose(fp4);
printf("\n\nObject Program:\n\n");
fp7 = fopen("objcode.txt", "r");
ch = fgetc(fp7);
while (ch != EOF)
{
printf("%c", ch);
ch = fgetc(fp7);
}
fclose(fp7);
}