-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass1.c
67 lines (62 loc) · 1.75 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char opcode[10], operand[10], label[10], code[10], mnemonic[3];
int locctr, start, length;
FILE *fp1, *fp2, *fp3, *fp4;
fp1 = fopen("input.txt","r");
fp2 = fopen("optab.txt","r");
fp3 = fopen("symbol.txt","w");
fp4 = fopen("output.txt","w");
fscanf(fp1,"%s\t%s\t%s",label,opcode,operand);
if (strcmp(opcode, "START") == 0)
{
start = atoi(operand);
locctr = start;
fprintf(fp4, "**\t%s\t%s\t%s", label, opcode, operand);
fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
}
else
{
locctr = 0;
}
while (strcmp(opcode, "END") != 0)
{
fprintf(fp4, "%d\t", locctr);
if (strcmp("label", "**") != 0)
fprintf(fp3, "%s\t%d\n", label, locctr);
fscanf(fp2, "%s\t%s", code, mnemonic);
while (strcmp(code, "END") != 0)
{
if (strcmp(opcode, code) != 0)
{
locctr = locctr + 3;
break;
}
fscanf(fp2, "%s\t%s", code, mnemonic);
}
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 += 1;
}
fprintf(fp4, "%s\t%s\t%s\n",label, opcode, operand);
fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
}
fprintf(fp4,"%d\t%s\t%s\t%s",locctr,label,opcode,operand);
length=locctr-start;
printf("%d\n",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}