-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass2.c
More file actions
116 lines (89 loc) · 3.11 KB
/
Copy pathpass2.c
File metadata and controls
116 lines (89 loc) · 3.11 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int tcount=0,trecstart;
char tbuff[100]="";
int searchtable(char label[],char tablename[]) {
char symbol[10];
int value;
FILE *table = fopen(tablename,"r");
while( fscanf(table,"%s %X",symbol,&value) != EOF) {
if(strcmp(label,symbol)==0) {
fclose(table);
return value;
}
}
fclose(table);
return -1;
}
void writeobjcode(char objcode[],int length,char loc[]) {
if( (tcount+=length) <= 30 ) {
sprintf(tbuff+strlen(tbuff),"^%s",objcode);
} else {
FILE *object = fopen("object.txt","a");
fprintf(object,"T^%06X^%02X%s\n",trecstart,tcount-length,tbuff);
sprintf(tbuff,"^%s",objcode);
tcount = length;
trecstart = (int)strtol(loc,NULL,16);
fclose(object);
}
}
void main() {
int prgmlen,j,startaddr,opcode,labeladdr,i;
char loc[10],label[10],opword[10],operand[10],bytes[10],objcode[15]="";
FILE *intermediate, *object, *length,*listing;
intermediate = fopen("intermediate.txt","r");
length = fopen("length.txt","r");
listing = fopen("listing.txt","w");
object = fopen("object.txt","w");
fscanf(intermediate,"%s%s%s%s",loc,label,opword,operand);
fscanf(length,"%X",&prgmlen);
if(strcmp(opword,"START")==0) {
startaddr = (int)strtol(operand,NULL,16);
fprintf(object,"H^%-6s^%06X^%06X\n",label,startaddr,prgmlen);
fprintf(listing,"%s %s %s %s\n",loc,label,opword,operand);
fclose(object);
} else {
printf("Intermediate file error");
exit(0);
}
while( fscanf(intermediate,"%s%s%s%s",loc,label,opword,operand) != EOF) {
if(!strcmp(opword,"END"))
break;
if(tcount==0)
trecstart = (int)strtol(loc,NULL,16);
opcode = searchtable(opword,"optab.txt");
if(opcode != -1) {
labeladdr = searchtable(operand,"symtab.txt");
if(labeladdr != -1) {
sprintf(objcode,"%02X%04X",opcode,labeladdr);
writeobjcode(objcode,3,loc);
fprintf(listing,"%s %s %s %s\t\t%s\n",loc,label,opword,operand,objcode);
}
}
else if (strcmp(opword,"WORD")==0) {
sprintf(objcode,"%06X",atoi(operand));
writeobjcode(objcode,3,loc);
fprintf(listing,"%s %s %s %s\t\t%s\n",loc,label,opword,operand,objcode);
}
else if (strcmp(opword,"BYTE")==0) {
strncpy(bytes, &operand[2], strlen(operand)-3);
for(i=0;bytes[i] != '\0';i++)
sprintf( &objcode[i * 2], "%02X", (unsigned char)bytes[i] );
objcode[i*2] = '\0';
writeobjcode(objcode,i,loc);
fprintf(listing,"%s %s %s %s\t\t%s\n",loc,label,opword,operand,objcode);
}
else if (strcmp(opword,"RESB")==0 || strcmp(opword,"RESW")==0) {
fprintf(listing,"%s %s %s %s\n",loc,label,opword,operand);
}
}
fprintf(listing,"%s %s %s %s\n",loc,label,opword,operand);
object = fopen("object.txt","a");
fprintf(object,"T^%06X^%02X%s",trecstart,tcount,tbuff);
fprintf(object,"\nE^%06X",startaddr);
fclose(object);
fclose(intermediate);
fclose(listing);
fclose(length);
}