System Programming
System Programming
Algorithm
STEP 1: Start the program execution.
STEP 2: Create a structure for opcode table and assign the values.
STEP 3: Create a structure for symbol table and assign the values.
STEP 4: Create a structure for intermediate code table and assign the values.
STEP 5: Write the opcode in separate file and machine code in another separate file.
STEP 6: Open the opcode file and compare it with the given machine code and then
generate opcode for corresponding source code.
STEP 7: Check the forward reference in intermediate code and print the corresponding
jump statement address.
STEP 8: Compare machine code with the opcode.If any jump statement with backward
reference is present, then print backward reference address.
STEP 9: For symbol table, print the symbol and address of the symbol.
STEP 10: Stop the program execution.
Source
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char var[10];
int value;
};
struct table tbl[20];
int i,j,n;
void create();
void modify();
int search(char variable[],int n);
void insert();
void display();
void main()
{
int ch,result=0;
char v[10];
clrscr();
do
{
printf("Enter ur choice:\n1.Create\n2.Insert\n3.Modify\n4.Search\n5.Display\n6.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
modify();
break;
case 4:
printf("Enter the variabe to be searched\n");
scanf("%s",&v);
result=search(v,n);
if(result==0)
printf("The variable does not belong to the table\n");
else
printf("The location of variable is %d. The value of %s is %d",
result,tbl[result].var,tbl[result].value);
break;
case 5:
display();
break;
case 6:
exit(1);
}
}
while(ch!=6);
getch();
}
void create()
{
printf("Enter the number of entries\n");
scanf("%d",&n);
printf("Enter the variable and the value:\n");
for(i=1;i<=n;i++)
{
scanf("%s%d",tbl[i].var,&tbl[i].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9')
{
printf("The variable should start with an alphabet\nEnter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<1;j++)
{
if(strcmp(tbl[i].var,tbl[j].var)==0)
{
printf("The variable already exists.\nEnter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
}
printf("The table after creation is\n");
display();
}
void insert()
{
if(i>=20)
printf("Cannotinsert. Table is full");
else
{
n++;
printf("Enter the variable and value\n");
scanf("%s%d",tbl[n].var,&tbl[n].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9')
{
printf("The variable should start with alphabet\nEnter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<n;j++)
{
if(strcmp(tbl[j].var,tbl[i].var)==0)
{
printf("The variable already exist\nEnter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
printf("The table after insertion is\n");
display();
}
}
void modify()
{
char variable[10];
int result=0;
printf("Enter the variable to be modified\n");
scanf("%s",&variable);
result=search(variable,n);
if(result==0)
printf("%sdoes not belong to the table",variable);
else
{
printf("The current value of the variable%s is %d, Enter the new variable and its
value",tbl[result].var,tbl[result].value);
scanf("%s%d",tbl[result].var,&tbl[result].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0] <= '9')
{
printf("The variable should start with alphabet\n Enter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
}
printf("The table after modification is\n");
display();
} int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
{
if(strcmp(tbl[i].var,variable)==0)
{
flag=1;
break;
}
}
if(flag==1)
return i;
else
return 0;
}
void display()
{
printf("Variable\t value\n");
for(i=1;i<=n;i++)
printf("%s\t\t%d\n",tbl[i].var,tbl[i].value);
}
OUTPUT
Enter your choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
1
Enter the number of entries
2
Enter the variable and the value:
A 26
B 42
The table after creation is
Variable value
A 26
B 42
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
2
Enter the variable and value
D 10
The table after insertion is
Variable value
A 26
B 42
D 10
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
3
Enter the variable to be modified
D
The current value of the variable D is 10, Enter the new variable and its value
C
20
The table after modification is
Variable value
A 26
B 42
C 20
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
4
Enter the variable to be searched
A The location of variable is 1. The value of A
is 26
Enter your choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
5
Variable value
A 26
B 42
C 20
Enter your choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
Source
1.
2.
3.
#include<stdio.h>
4.
#include<conio.h>
5.
#include<string.h>
6.
void main()
7.
8.
FILE *f1,*f2,*f3,*f4;
9.
int lc,sa,l,op1,o,len;
10.
char m1[20],la[20],op[20],otp[20];
11.
clrscr();
12.
f1=fopen("input.txt","r");
13.
f3=fopen("symtab.txt","w");
14.
fscanf(f1,"%s %s %d",la,m1,&op1);
15.
if(strcmp(m1,"START")==0)
16.
17.
sa=op1;
18.
lc=sa;
19.
printf("\t%s\t%s\t%d\n",la,m1,op1);
20.
21.
else
22.
lc=0;
23.
fscanf(f1,"%s %s",la,m1);
24.
while(!feof(f1))
25.
26.
fscanf(f1,"%s",op);
27.
printf("\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
28.
if(strcmp(la,"-")!=0)
29.
30.
fprintf(f3,"\n%d\t%s\n",lc,la);
31.
32.
f2=fopen("optab.txt","r");
33.
fscanf(f2,"%s %d",otp,&o);
34.
while(!feof(f2))
35.
36.
if(strcmp(m1,otp)==0)
37.
38.
lc=lc+3;
39.
break;
40.
41.
fscanf(f2,"%s %d",otp,&o);
42.
43.
fclose(f2);
44.
if(strcmp(m1,"WORD")==0)
45.
46.
47.
lc=lc+3;
48.
49.
else if(strcmp(m1,"RESW")==0)
50.
51.
op1=atoi(op);
52.
lc=lc+(3*op1);
53.
54.
else if(strcmp(m1,"BYTE")==0)
55.
56.
if(op[0]=='X')
57.
lc=lc+1;
58.
else
59.
60.
len=strlen(op)-2;
61.
lc=lc+len;}
62.
63.
else if(strcmp(m1,"RESB")==0)
64.
65.
op1=atoi(op);
66.
lc=lc+op1;
67.
68.
fscanf(f1,"%s%s",la,m1);
69.
70.
if(strcmp(m1,"END")==0)
71.
72.
73.
74.
fclose(f1);
75.
fclose(f3);
76.
getch();
77.
Input:
Input.txt
COPY
START
1000
LDA
ALPHA
ADD
ONE
Input.txt
SUB
TWO
STA
BETA
ALPHA
BYTE
C'KLNCE
ONE
RESB
TWO
WORD
BETA
RESW
END
Optab.txt
LDA
00
STA
23
ADD
01
SUB
05
Output:
Symtab.txt
1012
ALPHA
Symtab.txt
1017
ONE
1019
TWO
1022
BETA
COPY
START
1000
1000
LDA
ALPHA
1003
ADD
ONE
1006
SUB
TWO
1009
STA
BETA
1012
ALPHA
BYTE
C'KLNCE
1017
ONE
RESB
1019
TWO
WORD
1022
BETA
RESW
1025
END
Program length = 25
COPY
START
1000
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<ctype.h>
5.
main()
6.
7.
FILE *fint,*ftab,*flen,*fsym;
8.
int op1[10],txtlen,txtlen1,i,j=0,len;
9.
char
add[5],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],sy
mtab[10],opmne[10];
10.
clrscr();
11.
fint=fopen("input.txt","r");
12.
flen=fopen("length.txt","r");
13.
ftab=fopen("optab.txt","r");
14.
fsym=fopen("symbol.txt","r");
15.
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
16.
if(strcmp(mne,"START")==0)
17.
18.
strcpy(start,operand);
19.
fscanf(flen,"%d",&len);
20.
21.
printf("H^%s^%s^%d\nT^00%s^",label,start,len,start);
22.
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
23.
while(strcmp(mne,"END")!=0)
24.
25.
fscanf(ftab,"%s%s",opmne,op);
26.
while(!feof(ftab))
27.
28.
if(strcmp(mne,opmne)==0)
29.
30.
fclose(ftab);
31.
fscanf(fsym,"%s%s",symadd,symtab);
32.
while(!feof(fsym))
33.
34.
if(strcmp(operand,symtab)==0)
35.
36.
printf("%s%s^",op,symadd);
37.
break;
38.
39.
else
40.
fscanf(fsym,"%s%s",symadd,symtab);
41.
42.
break;
43.
44.
else
45.
fscanf(ftab,"%s%s",opmne,op);
46.
47.
if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
48.
49.
if(strcmp(mne,"WORD")==0)
50.
printf("0000%s^",operand);
51.
else
52.
53.
len=strlen(operand);
54.
for(i=2;i<len;i++)
55.
56.
printf("%d",operand[i]);
57.
58.
printf("^");
59.
60.
61.
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
62.
ftab=fopen("optab.txt","r");
63.
fseek(ftab,SEEK_SET,0);
64.
65.
printf("\nE^00%s",start);
66.
fclose(fint);
67.
fclose(ftab);
68.
fclose(fsym);
69.
fclose(flen);
70.
fclose(fout);
71.
getch();
72.
73.
input.txt:
COPY
START
1000
1000
LDA
ALPHA
1003
ADD
ONE
1006
SUB
TWO
1009
STA
BETA
1012
ALPHA
BYTE
C'KLNCE
1017
ONE
RESB
1019
TWO
WORD
input.txt:
1022
BETA
RESW
1025
END
optab.txt:
LDA
00
STA
23
ADD
01
SUB
05
length.txt:25
symbol.txt:
1012
ALPHA
1017
ONE
1019
TWO
1022
BETA
symbol.txt:
Source
1.
#include<stdio.h>
2.
#include<stdlib.h>
3.
#include<conio.h>
4.
#include<string.h>
5.
void main()
6.
7.
FILE *f1,*f2,*f3,*f4,*f5;
8.
int lc,sa,i=0,j=0,m[10],pgmlen,len,k,len1,l=0;
9.
char name[10],opnd[10],la[10],mne[10],s1[10],mne1[10],opnd1[10];
10.
char lcs[10],ms[10];
11.
char sym[10],symaddr[10],obj1[10],obj2[10],s2[10],q[10],s3[10];
12.
clrscr();
13.
f1=fopen("input.txt","r");
14.
f2=fopen("optab.txt","r");
15.
f3=fopen("symtab.txt","w+");
16.
f4=fopen("symtab1.txt","w+");
17.
f5=fopen("output.txt","w+");
18.
fscanf(f1,"%s%s%s",la,mne,opnd);
19.
20.
if(strcmp(mne,"START")==0)
{
21.
sa=atoi(opnd);
22.
strcpy(name,la);
23.
lc=sa;
24.
25.
strcpy(s1,"*");
26.
fscanf(f1,"%s%s%s",la,mne,opnd);
27.
while(strcmp(mne,"END")!=0)
28.
29.
if(strcmp(la,"-")==0)
30.
31.
fscanf(f2,"%s%s",mne1,opnd1);
32.
while(!feof(f2))
33.
34.
if(strcmp(mne1,mne)==0)
35.
36.
m[i]=lc+1;
37.
fprintf(f3,"%s\t%s\n",opnd,s1);
38.
fprintf(f5,"%s\t0000\n",opnd1);
39.
lc=lc+3;
40.
i=i+1;
41.
break;
42.
43.
else
44.
fscanf(f2,"%s%s",mne1,opnd1);
45.
46.
47.
48.
49.
else
50.
51.
fseek(f3,SEEK_SET,0);
52.
fscanf(f3,"%s%s",sym,symaddr);
53.
while(!feof(f3))
54.
55.
if(strcmp(sym,la)==0)
56.
57.
itoa(lc,lcs,10);
58.
fprintf(f4,"%s\t%s\n",la,lcs);
59.
itoa(m[j],ms,10);
60.
j=j+1;
61.
fprintf(f5,"%s\t%s\n",ms,lcs);
62.
i=i+1;
63.
break;
64.
65.
else
66.
fscanf(f3,"%s%s",sym,symaddr);
67.
} //f3
68.
if(strcmp(mne,"RESW")==0)
69.
lc=lc+3*atoi(opnd);
70.
else if(strcmp(mne,"BYTE")==0)
71.
72.
strcpy(s2,"-");
73.
len=strlen(opnd);
74.
lc=lc+len-2;
75.
for(k=2;k<len;k++)
76.
77.
q[l]=opnd[k];
78.
l=l+1;
79.
80.
fprintf(f5,"%s\t%s\n",q,s2);
81.
break;
82.
83.
else if(strcmp(mne,"RESB")==0)
84.
lc=lc+atoi(opnd);
85.
else if(strcmp(mne,"WORD")==0)
86.
87.
strcpy(s3,"#");
88.
lc=lc+3;
89.
fprintf(f5,"%s\t%s\n",opnd,s3);
90.
break;
91.
92.
}
} // else la=-
93.
94.
95.
fseek(f2,SEEK_SET,0);
96.
fscanf(f1,"%s%s%s",la,mne,opnd);
97.
98.
fseek(f5,SEEK_SET,0);
99.
pgmlen=lc-sa;
100.
printf("H^%s^%d^0%x\n",name,sa,pgmlen);
101.
printf("T^");
102.
printf("00%d^0%x",sa,pgmlen);
103.
fscanf(f5,"%s%s",obj1,obj2);
104.
while(!feof(f5))
105.
106.
if(strcmp(obj2,"0000")==0)
107.
printf("^%s%s",obj1,obj2);
108.
else if(strcmp(obj2,"-")==0)
109.
110.
printf("^");
111.
len1=strlen(obj1);
112.
for(k=0;k<len1;k++)
113.
printf("%d",obj1[k]);
114.
115.
else if(strcmp(obj2,"#")==0)
116.
117.
printf("^");
118.
printf("%s",obj1);
119.
120.
fscanf(f5,"%s%s",obj1,obj2);
121.
122.
fseek(f5,SEEK_SET,0);
123.
fscanf(f5,"%s%s",obj1,obj2);
124.
while(!feof(f5))
125.
126.
if(strcmp(obj2,"0000")!=0)
127.
128.
if(strcmp(obj2,"-")!=0)
129.
130.
if(strcmp(obj2,"#")!=0)
131.
132.
printf("\n");
133.
printf("T^%s^02^%s",obj1,obj2);
134.
135.
136.
137.
fscanf(f5,"%s%s",obj1,obj2);
138.
139.
printf("\nE^00%d",sa);
140.
141.
142.
getch();
143.
Input:
input.txt
COPY
START
1000
LDA
ALPHA
STA
BETA
ALPHA
RESW
BETA
RESW
END
optab.txt
optab.txt
LDA
00
STA
23
LDCH
15
STCH
18
Output:
symtab.txt
ALPHA
BETA
ALPHA
1006
BETA
1009
symtab1.txt
Output.txt
00
0000
23
0000
1001
1006
Output.txt
1004
result.txt:
H^COPY^1000^0c
T^001000^0c^000000^230000
T^1001^02^1006
T^1004^02^1009
E^001000
1009
Source
Pass one:
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<stdlib.h>
5.
void main()
6.
7.
FILE *f1,*f2,*f3;
8.
char mne[20],opnd[20],la[20];
9.
clrscr();
10.
f1=fopen("minp2.txt","r");
11.
f2=fopen("ntab2.txt","w+");
12.
f3=fopen("dtab2.txt","w+");
13.
fscanf(f1,"%s%s%s",la,mne,opnd);
14.
while(strcmp(mne,"MEND")!=0)
15.
16.
if(strcmp(mne,"MACRO")==0)
17.
18.
fprintf(f2,"%s\n",la);
19.
fprintf(f3,"%s\t%s\n",la,opnd);
20.
21.
else
22.
fprintf(f3,"%s\t%s\n",mne,opnd);
23.
fscanf(f1,"%s%s%s",la,mne,opnd);
24.
25.
fprintf(f3,"%s",mne);
26.
fclose(f1);
27.
fclose(f2);
28.
fclose(f3);
29.
printf("PASS 1 is successful");
30.
getch();
31.
32.
Input file:
minp2.txt
EX1
MACRO
&A,&B
LDA
&A
STA
&B
minp2.txt
MEND
SAMPLE
START
1000
EX1
N1,N2
N1
RESW
N2
RESW
END
Output files:
dtab2.txt
EX1
&A,&B
LDA
&A
STA
&B
MEND
ntab2.txt
EX1
Pass two:
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<stdlib.h>
5.
void main()
6.
7.
FILE *f1,*f2,*f3,*f4,*f5;
8.
int i,len;
9.
char mne[20],opnd[20],la[20],name[20],mne1[20],opnd1[20],arg[20];
10.
clrscr();
11.
f1=fopen("minp2.txt","r");
12.
f2=fopen("ntab2.txt","r");
13.
f3=fopen("dtab2.txt","r");
14.
f4=fopen("atab2.txt","w+");
15.
f5=fopen("op2.txt","w");
16.
fscanf(f1,"%s%s%s",la,mne,opnd);
17.
while(strcmp(mne,"END")!=0)
18.
19.
if(strcmp(mne,"MACRO")==0)
20.
21.
fscanf(f1,"%s%s%s",la,mne,opnd);
22.
while(strcmp(mne,"MEND")!=0)
23.
fscanf(f1,"%s%s%s",la,mne,opnd);
24.
25.
else
26.
27.
fscanf(f2,"%s",name);
28.
if(strcmp(mne,name)==0)
29.
30.
len=strlen(opnd);
31.
for(i=0;i<len;i++)
32.
33.
if(opnd[i]!=',')
34.
fprintf(f4,"%c",opnd[i]);
35.
else
36.
fprintf(f4,"\n");
37.
38.
fseek(f2,SEEK_SET,0);
39.
fseek(f4,SEEK_SET,0);
40.
fscanf(f3,"%s%s",mne1,opnd1);
41.
fprintf(f5,".\t%s\t%s\n",mne1,opnd);
42.
fscanf(f3,"%s%s",mne1,opnd1);
43.
while(strcmp(mne1,"MEND")!=0)
44.
45.
if((opnd1[0]=='&'))
46.
47.
fscanf(f4,"%s",arg);
48.
fprintf(f5,"-\t%s\t%s\n",mne1,arg);
49.
50.
else
51.
fprintf(f5,"-\t%s\t%s\n",mne1,opnd1);
52.
fscanf(f3,"%s%s",mne1,opnd1);
53.
54.
55.
else
56.
fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
57.
58.
fscanf(f1,"%s%s%s",la,mne,opnd);
59.
60.
fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
61.
fclose(f1);
62.
fclose(f2);
63.
fclose(f3);
64.
fclose(f4);
65.
fclose(f5);
66.
printf("pass2");
67.
getch();
68.
69.
Input files:
minp2.txt
EX1
MACRO
&A,&B
LDA
&A
STA
&B
MEND
SAMPLE
START
1000
EX1
N1,N2
N1
RESW
N2
RESW
END
dtab2.txt
EX1
&A,&B
dtab2.txt
LDA
&A
STA
&B
MEND
ntab2.txt
EX1
Output files:
atab2.txt
N1
N2
op2.txt
SAMPLE
START
1000
EX1
N1,N2
LDA
N1
op2.txt
STA
N2
N1
RESW
N2
RESW
END
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<stdlib.h>
5.
void main()
6.
7.
FILE *f1,*f2,*f3,*f4,*f5;
8.
int len,i,pos=1;
9.
char
arg[20],mne[20],opnd[20],la[20],name[20],mne1[20],opnd1[20],pos1[10],pos2[10];
10.
clrscr();
11.
f1=fopen("input.txt","r");
12.
f2=fopen("namtab.txt","w+");
13.
f3=fopen("deftab.txt","w+");
14.
f4=fopen("argtab.txt","w+");
15.
f5=fopen("op.txt","w+");
16.
fscanf(f1,"%s%s%s",la,mne,opnd);
17.
while(strcmp(mne,"END")!=0)
18.
19.
if(strcmp(mne,"MACRO")==0)
20.
21.
fprintf(f2,"%s\n",la);
22.
fseek(f2,SEEK_SET,0);
23.
fprintf(f3,"%s\t%s\n",la,opnd);
24.
fscanf(f1,"%s%s%s",la,mne,opnd);
25.
while(strcmp(mne,"MEND")!=0)
26.
27.
if(opnd[0]=='&')
28.
29.
itoa(pos,pos1,5);
30.
strcpy(pos2,"?");
31.
strcpy(opnd,strcat(pos2,pos1));
32.
pos=pos+1;
33.
34.
fprintf(f3,"%s\t%s\n",mne,opnd);
35.
fscanf(f1,"%s%s%s",la,mne,opnd);
36.
37.
fprintf(f3,"%s",mne);
38.
39.
else
40.
41.
fscanf(f2,"%s",name);
42.
if(strcmp(mne,name)==0)
43.
44.
len=strlen(opnd);
45.
for(i=0;i<len;i++)
46.
47.
if(opnd[i]!=',')
48.
fprintf(f4,"%c",opnd[i]);
49.
else
50.
fprintf(f4,"\n");
51.
52.
fseek(f3,SEEK_SET,0);
53.
fseek(f4,SEEK_SET,0);
54.
fscanf(f3,"%s%s",mne1,opnd1);
55.
fprintf(f5,".\t%s\t%s\n",mne1,opnd);
56.
fscanf(f3,"%s%s",mne1,opnd1);
57.
while(strcmp(mne1,"MEND")!=0)
58.
59.
if((opnd[0]=='?'))
60.
61.
fscanf(f4,"%s",arg);
62.
fprintf(f5,"-\t%s\t%s\n",mne1,arg);
63.
64.
else
65.
fprintf(f5,"-\t%s\t%s\n",mne1,opnd1);
66.
fscanf(f3,"%s%s",mne1,opnd1);
67.
68.
69.
else
70.
fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
71.
72.
fscanf(f1,"%s%s%s",la,mne,opnd);
73.
74.
fprintf(f5,"%s\t%s\t%s",la,mne,opnd);
75.
fclose(f1);
76.
fclose(f2);
77.
fclose(f3);
78.
fclose(f4);
79.
fclose(f5);
80.
81.
printf("1. argtab.txt\n");
82.
printf("2. namtab.txt\n");
83.
printf("3. deftab.txt\n");
84.
printf("4. op.txt\n");
85.
getch();
86.
87.
88.
Input.txt
EX1 MACRO &A,&B
- LDA &A
- STA &B
- MEND SAMPLE START 1000
- EX1 N1,N2
N1 RESW 1
N2 RESW 1
- END Argtab.txt
N1
N2
Op.txt
SAMPLE START 1000
. EX1 N1,N2
- LDA ?1
- STA ?2
N1 RESW 1
N2 RESW 1
- END Deftab.txt
EX1 &A,&B
LDA ?1
STA ?2
MEND
Namtab.txt
EX1
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<stdlib.h>
5.
void main()
6.
7.
FILE *fp;
8.
int i,addr1,l,j,staddr1;
9.
char name[10],line[50],name1[10],addr[10],rec[10],ch,staddr[10];
10.
clrscr();
11.
12.
scanf("%s",name);
13.
fp=fopen("abssrc.txt","r");
14.
fscanf(fp,"%s",line);
15.
for(i=2,j=0;i<8,j<6;i++,j++)
16.
name1[j]=line[i];
17.
name1[j]='\0';
18.
19.
if(strcmp(name,name1)==0)
20.
21.
do
22.
23.
fscanf(fp,"%s",line);
24.
if(line[0]=='T')
25.
26.
for(i=2,j=0;i<8,j<6;i++,j++)
27.
staddr[j]=line[i];
28.
staddr[j]='\0';
29.
staddr1=atoi(staddr);
30.
i=12;
31.
while(line[i]!='$')
32.
33.
if(line[i]!='^')
34.
35.
36.
staddr1++;
37.
i=i+2;
38.
39.
else i++;
40.
41.
42.
else if(line[0]='E')
43.
fclose(fp);
44.
}while(!feof(fp));
45.
46.
47.
getch();
48.
INPUT (ABSSRC.TXT)
H^SAMPLE^001000^0035
T^001000^0C^001003^071009$
T^002000^03^111111$
E^001000
OUTPUT
001000 00
001001 10
OUTPUT
001002 03
001003 07
001004 10
001005 09
002000 11
002001 11
002002 11
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<stdlib.h>
5.
6.
char bitmask[12];
7.
char bit[12]={0};
8.
void main()
9.
{char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5];
10.
int start,inp,len,i,address,opcode,addr,actualadd,tlen;
11.
FILE *fp1,*fp2;
12.
clrscr();
13.
14.
scanf("%x",&start);
15.
fp1=fopen("RLIN.txt","r");
16.
fp2=fopen("RLOUT.txt","w");
17.
fscanf(fp1,"%s",input);
18.
fprintf(fp2," ----------------------------\n");
19.
fprintf(fp2," ADDRESS\tCONTENT\n");
20.
fprintf(fp2," ----------------------------\n");
21.
while(strcmp(input,"E")!=0)
22.
23.
if(strcmp(input,"H")==0)
24.
25.
fscanf(fp1,"%s",pn);
26.
fscanf(fp1,"%x",add);
27.
fscanf(fp1,"%x",length);
28.
fscanf(fp1,"%s",input);
29.
30.
if(strcmp(input,"T")==0)
31.
32.
fscanf(fp1,"%x",&address);
33.
fscanf(fp1,"%x",&tlen);
34.
fscanf(fp1,"%s",bitmask);
35.
address+=start;
36.
convert(bitmask);
37.
len=strlen(bit);
38.
if(len>=11)
39.
len=10;
40.
for(i=0;i<len;i++)
41.
42.
fscanf(fp1,"%x",&opcode);
43.
fscanf(fp1,"%x",&addr);
44.
relocbit=bit[i];
45.
if(relocbit=='0')
46.
actualadd=addr;
47.
else
48.
actualadd=addr+start;
49.
fprintf(fp2,"\n %x\t\t%x%x\n",address,opcode,actualadd);
50.
address+=3;
51.
52.
fscanf(fp1,"%s",input);
53.
54.
55.
fprintf(fp2," ----------------------------\n");
56.
fcloseall();
57.
58.
fp2=fopen("RLOUT.txt","r");
59.
ch=fgetc(fp2);
60.
while(ch!=EOF)
61.
62.
printf("%c",ch);
63.
ch=fgetc(fp2);
64.
65.
fclose(fp2);
66.
getch();
67.
68.
69.
70.
int i,l;
71.
strcpy(bit,"");
72.
l=strlen(h);
73.
for(i=0;i<l;i++)
74.
75.
switch(h[i])
76.
77.
case '0':
78.
strcat(bit,"0");
79.
break;
80.
case '1':
81.
strcat(bit,"1");
82.
break;
83.
case '2':
84.
strcat(bit,"10");
85.
break;
86.
case '3':
87.
88.
strcat(bit,"11");
break;
89.
90.
case '4':
strcat(bit,"100");
91.
break;
92.
case '5':
93.
strcat(bit,"101");
94.
break;
95.
case '6':
96.
strcat(bit,"110");
97.
break;
98.
case '7':
99.
strcat(bit,"111");
100.
break;
101.
case '8':
102.
strcat(bit,"1000");
103.
break;
104.
case '9':
105.
strcat(bit,"1001");
106.
break;
107.
case 'A':
108.
strcat(bit,"1010");
109.
break;
110.
case 'B':
111.
strcat(bit,"1011");
112.
break;
113.
case 'C':
114.
strcat(bit,"1100");
115.
break;
116.
case 'D':
117.
strcat(bit,"1101");
118.
break;
119.
case 'E':
120.
strcat(bit,"1110");
121.
break;
122.
case 'F':
123.
strcat(bit,"1111");
124.
break;
125.
126.
127.
128.
INPUT:
RLIN.TXT
H COPY 000000 00107A
T 000000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20
002A 1C 0039 30 002D
T 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003
E 000000
OUTPUT:
Enter the actual starting address : 4000
The contents of output file(RLOUT.TXT):
----------------------------------------------------ADDRESS CONTENT
----------------------------------------------------4000 144033
4003 485039
4006 104036
4009 284030
400c 304015
400f 485061
4012 3c4003
4015 20402a
4018 1c4039
401b 30402d
6503 1d4036
6506 184033
6509 4c1000
650c 801000
650f 601003
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
5.
void main()
6.
7.
int i,j,k,pgmaddr,csaddr,cslth=0,z,addr1,l1;
8.
char line[50],name[10],len[10],la[10],addr[10];
9.
FILE *fp1;
10.
clrscr();
11.
12.
scanf("%d",&pgmaddr);
13.
csaddr=pgmaddr;
14.
for(k=0;k<2;k++)
15.
16.
17.
18.
19.
if(k==0)
fp1=fopen("linkin.txt","r");
if(k==1)
fp1=fopen("linkin1.txt","r");
20.
do
21.
22.
fscanf(fp1,"%s",line);
23.
if(line[0]=='H')
24.
25.
for(i=2,j=0;i<8,j<6;i++,j++)
26.
name[j]=line[i];
27.
name[j]='\0';
28.
for(i=16,j=0;i<20,j<5;i++,j++)
29.
len[j]=line[i];
30.
len[j]='\0';
31.
cslth=atoi(len);
32.
printf("%s\t\t%d\t%s\n",name,csaddr,len);
33.
34.
else if(line[0]=='D')
35.
36.
i=2;
37.
j=0;
38.
do
39.
40.
do
41.
42.
la[j++]=line[i++];
43.
}while(line[i]!='^');
44.
la[j]='\0';
45.
j=0;i++;
46.
do
47.
{addr[j++]=line[i++];
48.
} while(line[i]!='^');
49.
i++;
50.
addr[j]='\0';
51.
addr1=atoi(addr)+csaddr;
52.
j=0;
53.
printf("%s\t\t%d\n",la,addr1);
54.
} while(line[i]!='\0');
55.
56.
else if(line[0]=='R'||'T')
57.
z=0;
58.
else if(line[0]=='E')
59.
fclose(fp1);
60.
}while(!feof(fp1));
61.
csaddr=csaddr+cslth;
62.
}getch();
63.
64.
65.
66.
Input files:
linkin.txt
H^PROGA1^000000^0073
D^LISTA^000024^ENDA^000027^
R^LISTB
T^000000^06^000024^010027
E^000000
linkin1.txt
H^PROGA2^000000^0089
D^LISTB^000047^
Output:
Output:
2000
PROGA1
2000
LISTA
2024
ENDA
2027
PROGA2
2073
LISTB
2120
0073
0089
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<string.h>
4.
#include<stdlib.h>
5.
void main()
6.
7.
FILE *f1,*f2,*f3;
8.
int csaddr,progaddr,execaddr,cslen,i,j,k=0,staddr1,staddr2,addr2;
9.
int modadr,val1,adr2,outadr1,esadr;
10.
11.
char
outadr[10],adr1[10],name[20],val[10],pname[10],symname[10],adr[10];
char l[10],line[80],len[10],staddr[10],addr[10],addr1[10];
12.
f3=fopen("estab.txt","r");
13.
f2=fopen("dupout.txt","w");
14.
clrscr();
15.
16.
scanf("%d",&progaddr);
17.
csaddr=progaddr;
18.
execaddr=progaddr;
19.
do
20.
21.
if(k==0)
22.
f1=fopen("link2in.txt","r");
23.
if(k==1)
24.
f1=fopen("linking2.txt","r");
25.
do
26.
27.
fscanf(f1,"%s",line);
28.
if(line[0]=='H')
29.
30.
for(i=9,j=0;i<15,j<6;i++,j++)
31.
addr[j]=line[i];
32.
addr[j]='\0';
33.
for(i=16,j=0;i<20,j<5;i++,j++)
34.
len[j]=line[i];
35.
len[j]='\0';
36.
37.
cslen=atoi(len);
}
38.
else if(line[0]!='E')
39.
40.
do
41.
42.
fscanf(f1,"%s",line);
43.
if(line[0]=='T')
44.
45.
for(i=2,j=0;i<8,j<6;i++,j++)
46.
staddr[j]=line[i];
47.
staddr[j]='\0';
48.
staddr1=atoi(staddr);
49.
staddr2=staddr1+progaddr;
50.
i=12;
51.
while(line[i]!='$')
52.
53.
if(line[i]!='^')
54.
55.
printf("00%d\t%c%c\n",staddr2,line[i],line[i+1]);
56.
fprintf(f2,"00%d\t%c%c\n",staddr2,line[i],line[i+1]);
57.
staddr2++;
58.
i=i+2;
59.
60.
else
61.
i++;
62.
63.
fclose(f2);
64.
65.
else if(line[0]=='M')
66.
67.
for(i=13,j=0;line[i]!='$',j<5;i++,j++)
68.
name[j]=line[i];
69.
name[j]='\0';
70.
do
71.
72.
fscanf(f3,"%s%s%s%s",pname,symname,adr,l);
73.
if(strcmp(name,symname)==0)
74.
75.
for(i=2,j=0;i<8,j<6;i++,j++)
76.
adr1[j]=line[i];
77.
adr1[j]='\0';
78.
adr2=atoi(adr1);
79.
adr2=adr2+progaddr;
80.
f2=fopen("dupout.txt","r");
81.
fscanf(f2,"%s%s",outadr,val);
82.
83.
do
84.
85.
outadr1=atoi(outadr);
86.
if(adr2==outadr1)
87.
88.
val1=atoi(val);
89.
esadr=atoi(adr);
90.
modadr=val1+esadr;
91.
printf("%s\t\t%d\n",outadr,modadr);
92.
93.
fscanf(f2,"%s%s",outadr,val);
94.
95.
96.
while(!feof(f2));
}
97.
98.
}while(!feof(f3));
99.
100.
}while(line[0]!='E');
101.
102.
else
103.
104.
for(i=2,j=0;i<8,j<6;i++,j++)
105.
addr1[j]=line[i];
106.
addr1[j]='\0';
107.
108.
109.
110.
if(strcmp(addr,addr1)==0)
{
addr2=atoi(addr1);
execaddr=csaddr+cslen;
111.
112.
else
113.
csaddr=csaddr+cslen;
114.
115.
fscanf(f1,"%s",line);
116.
}while(!feof(f1));
117.
k++;
118.
}while(k<=2);
119.
fclose(f1);
120.
fclose(f2);
121.
fclose(f3);
122.
123.
getch();
124.
Input files:
estab.txt
PROGA
0000
0063
LISTA
0023
ENDA
0027
estab.txt
PROGB
0000
0089
LISTB
0047
link2in.txt
H^PROGA^000000^0073
D^LISTA^000023^ENDA^000027^
R^LISTB
T^000000^06^000023^000027$
M^000001^01^+LISTB$
E^000000
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<process.h>
4.
int i,j,ch;
5.
char fn[20],e,c;
6.
FILE *fp1,*fp2,*fp;
7.
void Create();
8.
void Append();
9.
void Copy();
10.
void Delete();
11.
void Display();
12.
void main()
13.
14.
do {
15.
clrscr();
16.
17.
printf("\n\n\tMENU:\n\t\n");
18.
printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.COPY\n\t5.DELETE\n\t6.EX
IT\n");
19.
20.
scanf("%d",&ch);
21.
switch(ch)
22.
23.
case 1:
24.
Create();
25.
break;
26.
case 2:
27.
Display();
28.
break;
29.
case 3:
30.
Append();
31.
break;
32.
case 4:
33.
Copy();
34.
break;
35.
case 5:
36.
Delete();
37.
break;
38.
case 6:
39.
exit(0);
40.
41.
}while(1);
42.
43.
void Create()
44.
45.
fp1=fopen("temp.txt","w");
46.
47.
while(1)
48.
49.
c=getchar();
50.
fputc(c,fp1);
51.
if(c == '.')
52.
53.
fclose(fp1);
54.
break;
55.
56.
}}
57.
58.
void Display()
59.
60.
61.
scanf("%s",fn);
62.
fp1=fopen(fn,"r");
63.
if(fp1==NULL)
64.
65.
66.
goto end1;
67.
68.
while(!feof(fp1))
69.
70.
c=getc(fp1);
71.
printf("%c",c);
72.
73.
end1:
74.
fclose(fp1);
75.
76.
getch();
77.
78.
void Copy()
79.
80.
81.
scanf("%s",fn);
82.
fp1=fopen("temp.txt","r");
83.
fp2=fopen(fn,"w");
84.
while(!feof(fp1))
85.
86.
c=getc(fp1);
87.
putc(c,fp2);
88.
89.
fclose(fp2);
90.
91.
92.
void Delete()
93.
94.
95.
scanf("%s",fn);
96.
fp1=fopen(fn,"r");
97.
if(fp1==NULL)
98.
99.
100.
goto end2;
101.
102.
fclose(fp1);
103.
if(remove(fn)==0)
104.
105.
106.
goto end2;
107.
108.
else
109.
printf("\n\tError!\n");
110.
111.
getch();
112.
113.
114.
void Append()
115.
116.
117.
scanf("%s",fn);
118.
fp1=fopen(fn,"r");
119.
if(fp1==NULL)
120.
121.
122.
fclose(fp1);
123.
goto end3;
124.
125.
while(!feof(fp1))
126.
127.
c=getc(fp1);
128.
printf("%c",c);
129.
130.
fclose(fp1);
131.
132.
fp1=fopen(fn,"a");
133.
while(1)
134.
135.
c=getch();
136.
if(c==19)
137.
goto end3;
138.
if(c==13)
139.
140.
c='\n';
141.
printf("\n\t");
142.
fputc(c,fp1);
143.
144.
else
145.
146.
printf("%c",c);
147.
fputc(c,fp1);
148.
149.
150.
end3: fclose(fp1);
151.
152.
getch();
153.
154.
Source
1.
#include<stdio.h>
2.
#include<conio.h>
3.
#include<stdlib.h>
4.
#include<string.h>
5.
#define MAX 11
6.
char l[10];
7.
struct symb
8.
9.
int add;
10.
char label[10];
11.
}sy[11];
12.
void search();
13.
void main()
14.
15.
int a[MAX],num,key,i,ch;
16.
char ans;
17.
int create(int);
18.
19.
20.
clrscr();
21.
for(i=0;i<MAX;i++)
22.
a[i]=0;
23.
do
24.
25.
26.
scanf("%d",&ch);
27.
switch(ch)
28.
29.
case 1:
30.
do
31.
32.
33.
scanf("%d",&num);
34.
key=create(num);
35.
36.
scanf("%s",l);
37.
lprob(a,key,num);
38.
printf("\nContinue(y/n)?");
39.
ans=getche();
40.
41.
while(ans=='y');
42.
display(a);
43.
break;
44.
case 2:
45.
search();
46.
break;
47.
48.
}while(ch<=2);
49.
getch();
50.
51.
52.
53.
54.
int key;
55.
key=num%11;
56.
return key;
57.
58.
59.
60.
61.
int flag,i,count=0;
62.
63.
flag=0;
64.
if(a[key]==0)
65.
66.
a[key]=num;
67.
sy[key].add=num;
68.
strcpy(sy[key].label,l);
69.
70.
else
71.
72.
i=0;
73.
while(i<MAX)
74.
75.
if(a[i]!=0)
76.
count++;
77.
i++;
78.
79.
if(count==MAX)
80.
81.
82.
display(a);
83.
getch();
84.
exit(1);
85.
86.
for(i=key+1;i<MAX;i++)
87.
if(a[i]==0)
88.
89.
a[i]=num;
90.
flag=1;
91.
sy[key].add=num;
92.
strcpy(sy[key].label,l);
93.
break;
94.
95.
96.
if(a[i]==0)
97.
98.
a[i]=num;
99.
flag=1;
100.
sy[key].add=num;
101.
strcpy(sy[key].label,l);
102.
break;
103.
104.
105.
106.
107.
108.
FILE *fp;
109.
int i;
110.
fp=fopen("symbol.txt","w");
111.
112.
113.
for(i=0;i<MAX;i++)
114.
115.
116.
fprintf(fp,"\n%d %d %s",i,sy[i].add,sy[i].label);
117.
118.
fclose(fp);
119.
120.
121.
void search()
122.
123.
FILE *fp1;
124.
char la[10];
125.
int set=0,s;
126.
int j,i;
127.
128.
scanf("%s",la);
129.
fp1=fopen("symbol.txt","r");
130.
for(i=0;i<MAX;i++)
131.
132.
fscanf(fp1,"%d%d",&j,&sy[i].add);
133.
if(sy[i].add!=0)
134.
fscanf(fp1,"%s",sy[i].label);
135.
136.
for(i=0;i<MAX;i++)
137.
138.
if(sy[i].add!=0)
139.
140.
if(strcmp(sy[i].label,la)==0)
141.
142.
set=1;
143.
s=sy[i].add;
144.
145.
146.
147.
if(set==1)
148.
149.
else
150.
151.
152.
enter your choice: 1.create a symbol table 2.search in the symbol table
1
Enter the address:1000
enter The label:data
Continue(y/n)?y
Enter the address:1003
enter The label:data1
Continue(y/n)?y
Enter the address:1007
enter The label:data2
Continue(y/n)? n
address
1003
1007
label
data1
data2
hashvalues
address
label
10
1000
data
enter your choice: 1.create a symbol table 2.search in the symbol table
2
enter the label: data
The label --data-- is present in the symbol table at address:1000
enter your choice: 1.create a symbol table 2.search in the symbol table
2
enter the label: data4
The label is not present in the symbol table
enter your choice: 1.create a symbol table 2.search in the symbol table