-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass2.c
More file actions
105 lines (83 loc) · 3.68 KB
/
pass2.c
File metadata and controls
105 lines (83 loc) · 3.68 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(){
FILE *interFile,*lenFile,*optab,*symtab;
char address[10],label[10],opcode[10],operand[10],len[5],mnemonic[10],mnemonic_val[5],symbol[10],symbol_val[10];
int opcode_found,counter=0,ignore=0,constant=0,sym_found;
interFile=fopen("interFile.txt","r");
lenFile=fopen("lenFile.txt","r");
fscanf(interFile,"%s\t%s\t%s\t%s",address,label,opcode,operand);
fscanf(lenFile,"%s",len);
if(strcmp(opcode,"START")==0){
printf("H^%s^%s^%s",label,operand,len);
fscanf(interFile,"%s\t%s\t%s\t%s",address,label,opcode,operand);
}
while (!feof(interFile))
{
if(strcmp(opcode,"END")==0){
printf("\nE^%.6d",atoi(operand));
exit(1);
}
optab=fopen("optab.txt","r");
opcode_found=0;
while(!feof(optab)){
fscanf(optab,"%s\t%s",mnemonic,mnemonic_val);
if(strcmp(mnemonic,opcode)==0){
opcode_found= 1;
break;
}
}
fclose(optab);
if(opcode_found==0){
ignore=0;
constant=0;
if(strcmp(opcode,"WORD")==0 || strcmp(opcode,"BYTE")==0){
strcpy(mnemonic_val,operand);
opcode_found=1;
constant=1;
counter++;
}else if(strcmp(opcode,"RESB")==0 || strcmp(opcode,"RESW")==0){
ignore=1;
}else{
printf("invalid opcode found!!");
exit(1);
}
}
if(ignore==0){
if(constant==0){
symtab=fopen("symtab.txt","r");
sym_found=0;
while(!feof(symtab)){
fscanf(symtab,"%s\t%s",symbol,symbol_val);
if(strcmp(operand,symbol)==0){
strcat(mnemonic_val,symbol_val);
sym_found=1;
counter++;
break;
}
}
if(sym_found==0){
printf("invalid symbol found: %s",operand);
exit(1);
}
fclose(symtab);
}
if(counter>10)
counter=1;
if(counter==1){
printf("\nT^%s^1E",address);
}
printf("^%s",mnemonic_val);
}
fscanf(interFile,"%s\t%s\t%s\t%s",address,label,opcode,operand);
if(strcmp(opcode,"END")==0){
printf("\nE^%s",operand);
exit(1);
}
}
fclose(interFile);
fclose(symtab);
fclose(optab);
fclose(lenFile);
}