Professional Documents
Culture Documents
AIM:
ALGORITHM:
#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 ur 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 variableD 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 variabe to be searched
A
The location of variable is 1. The value of A is 26
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
5
Variable value
A 26
B 42
C 20
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
6
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 2
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct sourcefile
{
char label[10],instr[10],operand[10];
}s;
struct pseudocd
{
char s[10];
}ps;
struct source_result
{
int address;
char label[10],instr[10],oper[10];
}res;
int find(char str[10])
{
FILE *pse;
pse=fopen("pseudo.txt","r");
while(!feof(pse))
{
fscanf(pse,"%s",ps.s);
if(strcmp(str,ps.s)==0)
{
fclose(pse);
return 1;
}
}
fclose(pse);
return 0;
}
void main()
{
FILE *ss,*ss1;
int addr,detect;
clrscr();
ss=fopen("s.txt","r+");
ss1=fopen("res.txt","w+");
while(!feof(ss))
{
fscanf(ss,"%s%s%s",s.label,s.instr,s.operand);
detect=find(s.instr);
if(detect==1)
{
if(strcmp(s.instr,"START")==0)
addr=atoi(s.operand);
else if(strcmp(s.instr,"WORD")==0)
addr=res.address+3;
else if(strcmp(s.instr,"RESW")==0)
addr=res.address+(3*atoi(s.operand));
else if(strcmp(s.instr,"RESB")==0)
addr=res.address+atoi(s.operand);
else if(strcmp(s.instr,"BYTE")==0)
addr=res.address+strlen(s.operand)-3;
else
addr=res.address+3;
strcpy(res.label,s.label);
strcpy(res.instr,s.instr);
strcpy(res.oper,s.operand);
fprintf(ss1,"%d\t%s\t%s\t%s\n",res.address,res.label,res.instr,res.oper);
res.address=addr;
}
else
{
printf("Error Encounted");
fclose(ss);
fclose(ss1);
getch();
}
}
printf("\nPass 1 Completed");
fclose(ss);
fclose(ss1);
getch();
}
OUTPUT:
Input:
pseudo.txt
START
READ
WORD
BYTE
RESW
RESB
END
s.txt
Output:
Pass 1 Completed
res.txt
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 3
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct opc
{
int len;
char mnemonic[10],code[3];
}opcode;
struct opd
{
int address;
char code[10];
}op;
struct source_result
{
int address;
char label[10],instr[10],operand[10];
}res;
struct res
{
int a;
char c[10];
}s;
void main()
{
FILE *r,*o,*result,*symb;
int i,j,found=0,l;
char s1[10];
clrscr();
r=fopen("re.txt","r");
o=fopen("opcode.txt","r");
result=fopen("output.txt","w");
symb=fopen("symbol.txt","r");
while(!feof(r))
{
fscanf(r,"%d%s%s%s",&res.address,&res.label,&res.instr,&res.operand);
rewind(o);
rewind(symb);
found = 0;
while(!feof(o))
{
fscanf(o,"%d%s%s",&opcode.len,&opcode.mnemonic,&opcode.code);
if(strcmp(res.instr,opcode.mnemonic)==0)
{
op.address=res.address;
strcpy(op.code,opcode.code);
fprintf(result,"%d\t%s",op.address,op.code);
found=1;
break;
}
}
if(found==0)
continue;
while(!feof(symb))
{
fscanf(symb,"%d%s",&s.a,&s.c);
if(strcmp(res.operand,s.c)==0)
{
fprintf(result,"%d\n",s.a);
break;
}
else if(strcmp(res.operand,"NULL")==0)
{
fprintf(result,"0000");
break;
}
else if(res.operand[0]=='#')
{
strcpy(s1,res.operand);
l=strlen(s1)-1;
for(i=4;i>l;i--)
fprintf(result,"0");
for(j=1;j<=l;j++)
fprintf(result,"%c",s1[j]);
fprintf(result,"\n");
break;
}
}
}
printf("Pass 2 completed!");
fcloseall();
getch();
}
OUTPUT:
opcode.txt
1 READ 1F
1 LDA 00
re.txt
symbol.txt
1000 A
1100 LOOP
1107 C
1110 G
1125 CLOOP
Output
Pass 2 completed!
output.txt
1100 1F1000
1125 000005
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 4
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2;
char ch,str[30],str1[10],str2[30],cstr[15];
int i,j,num,q,r;
clrscr();
printf("Enter your assembly instructions\n");
f1=fopen("asin","w");
while(1)
{
ch=getchar();
if(ch=='*')
break;
fputc(ch,f1);
}
fclose(f1);
f1=fopen("asin","r");
f2=fopen("asout","w");
while(1)
{
fgets(str,25,f1);
strncpy(str1,str,3);
str1[3]='\0';
j=0;
for(i=3;i<strlen(str);i++)
{
str2[j]=str[i];
j++;
}
str2[j]='\0';
if((strcmp(str1,"lda"))==0)
{
fputs("3a\t",f2);
fputs(str2,f2);
}
else if((strcmp(str1,"mov"))==0)
{
fputs("47\n",f2);
}
else if((strcmp(str1,"add"))==0)
{
fputs("80\n",f2);
}
else if((strcmp(str1,"sub"))==0)
{
fputs("90\n",f2);
}
else if((strcmp(str1,"hlt"))==0)
{
fputs("76\n",f2);
break;
}
else if((strcmp(str1,"sta"))==0)
{
fputs("32\t",f2);
num=atoi(str2)
q=num/100;
r=num%100;
if(r==0)
fputs("00\t",f2);
else
fputs(itoa(r,cstr,10),f2);
fputs("\t",f2);
fputs(itoa(q,cstr,10),f2);
fputs("\n",f2);
}
else
{
fputs("error\n",f2);
}
}
fclose(f1);
fclose(f2);
f2=fopen("asout","r");
printf("\nTHE OBJECT CODE CONTENTS\n");
ch=fgetc(f2);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(f2);
}
fclose(f2);
getch();
}
OUTPUT:
Input
Enter your assembly instructions
lda 5000
sub z
sta 9988
hlt
*
Output
THE OBJECT CODE CONTENTS
3a 00 50
90
32 88 99
76
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 5
IMPLEMENTATION OF MACROPROCESSOR
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char n1,n,c1,i;
char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];
FILE *fp1,*fp2,*p[5];
clrscr();
n=0;
fp1=fopen("z:\macin.txt","r");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
n++;
}
printf("no.of macros=%d\n",n);
n1=n;
printf("enter the text filename \n");
for(i=0;i<n;i++)
{
scanf("%s",fn[i]);
p[i]=fopen(fn[i],"w");
}
n=0;
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
{
strcpy(m[n],oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"mend")!=0)
{
fprintf(p[n],"%s%s%s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(p[n]);
n++;
}
}
for(i=0;i<n1;i++)
p[i]=fopen(fn[i],"r");
fp2=fopen("z:\outm.txt","w");
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"call")==0)
{
for(i=0;i<n1;i++)
{
if(strcmp(m[i],oper)==0)
{
rewind(p[i]);
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
while(!feof(p[i]))
{
fprintf(fp2,"%s%s%s",ilab,iopd,oper);
c1=1;
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
}
break;
}
}
}
if(c1!=1)
fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);
c1=0;
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);
}
OUTPUT:
Input:
macin.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** call m1
** call m2
** add a,b
Output:
No. of Macros =2
Enter the Text file Names
Mac1.dat
Mac2.dat
outm.txt
** macro m1
** move a,b
** mend---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** move a,b callm1
** lda b callm2
** add a,b
mac1.dat
** move a,b
mac2.dat
** lda b
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 6
AIM:
ALGORITHM:
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *myfile;
int i=0,f=0,f1=0,j,k;
char ch,fn[30],stadd[15],textrec[80];
clrscr();
myfile=fopen("in.txt","r");
if(myfile == NULL)
{
puts("Cannot open the file");
exit(0);
}
while((!feof(myfile)) && f1==0)
{
while(f1==0)
{
ch = fgetc(myfile);
if((ch=='H' || ch=='h' ) && f==0)
f=1;
if(ch=='-')
{
f1=1;
break;
}
if(ch != 'H' && f==1 && f1==0)
{
fn[i]=ch;
i++;
fn[i]='\0';
}
}
if(f1==1)
for(j=0;j<=12;j++)
stadd[j]=(ch=fgetc(myfile));
stadd[j]='\0';
while(!feof(myfile))
{
ch=fgetc(myfile);
if(ch=='T')
for(k=1;k<=80;k++)
textrec[k]=(ch=fgetc(myfile));
textrec[k]='\0';
}
}
printf("Program name is %s \n",fn);
printf("\n Starting Address ");
for(i=0;i<=5;i++)
printf("%c",stadd[i]);
Input
IN.TXT
HCOPY-001000-00107A
T001000-141033-482039-001036-281030-301015-482061-00102D
001000
Output
Program name is COPY
Starting address 1000
Length of the Program 00107A
Text Address
001000
141033
482039
001036
281030
301015
482061
00102D
001000
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 7
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char add[10];
}obcode[300];
void main()
{
char input[100][16],output[100][16],binary[20],address[20],stloc[10];
int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("z:\ssinput.dat","r");
fp2=fopen("z:\ssoutput.dat","w");
printf("Enter the location where the program has to be loaded:\n");
scanf("%s",stloc);
start=atoi(stloc);
location=start;
tloc=start;
fscanf(fp1,"%s",input[i]);
while(strcmp(input[i],"T")!=0)
{
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
itoa(start,output[2],10);
while(strcmp(input[i],"E")!=0)
{
strcpy(output[i],input[i]);
if(strcmp(input[i],"T")==0)
{
for(j=0;j<3;j++)
{
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
bitmask=atoi(output[i]);
itoa(bitmask,binary,2);
strcpy(output[i],"NULL");
textloc=atoi(output[i-2]);
textloc=textloc+start;
itoa(textloc,output[i-2],10);
for(n=0;n<(textloc-(tloc+tlen));n++)
{
strcpy(obcode[inc].add,"xx");
obcode[inc++].locctr=location++;
}
tlen=atoi(output[i-1]);
tloc=textloc;
k=0;
}
else
{
if(binary[k]=='1')
{
num=0;
len=strlen(output[i]);
strcpy(address,NULL);
for(j=2;j<len;j++)
{
address[num]=output[i][j];
output[i][j]='\0';
num++;
}
loc=atoi(address);
loc=loc+start;
itoa(loc,address,10);
strcat(output[i],address);
}
k++;
len=strlen(output[i]);
num=0;
for(n=0;n<len;n++)
{
obcode[inc].add[num++]=output[i][n];
if(num>1)
{
obcode[inc++].locctr=location++;
num=0;
}
}
}
i++;
fscanf(fp1,"%s",input[i]);
}
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
loc=atoi(input[i]);
loc=loc+start;
strcpy(output[i],itoa(loc,address,10));
count=0;
i=0;
n=0;
fprintf(fp2,"%d\t",obcode[n].locctr);
for(n=0;n<inc;n++)
{
fprintf(fp2,"%s",obcode[n].add);
i++;
if(i>3)
{
fprintf(fp2,"\t");
i=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%d\t",obcode[n+1].locctr);
count=0;
}
}
getch();
}
OUTPUT:
Input:
ssinput.dat
Output:
ssoutput.dat
RESULT
Thus the above program has been done and output is verified.
Ex No: 8
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct estab
{
char csect[10];
char sym_name[10];
long int add;
int length;
}
table[MAX];
void main()
{
FILE *fp1,*fp2;
char input[10];
long int i,count=0,start,length,loc;
clrscr();
fp1=fopen("link1in.c","r");
fp2=fopen("file1.c","w");
printf("Enter the location where the program has to be loaded");
scanf("%lx",&start);
fprintf(fp2,"CSECT\t\tSYMNAME\t\tADDRESS\tLENGTH\n");
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",input);
strcpy(table[count].csect,input);
strcpy(table[count].sym_name,"\0");
fscanf(fp1,"%s",input);
table[count].add=atoi(input)+start;
fscanf(fp1,"%s",input);
length=atoi(input);
table[count++].length=atoi(input);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"D")==0)
{
fscanf(fp1,"%s%lx",input,&loc);
while((strcmp(input,"R")!=0))
{
strcpy(table[count].csect,"\0");
strcpy(table[count].sym_name,input);
table[count].add=loc+start;
table[count++].length=0;
fscanf(fp1,"%s%lx",input,&loc);
}
while(strcmp(input,"T")!=0)
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
while(strcmp(input,"E")!=0)
fscanf(fp1,"%s",input);
fscanf(fp1,"%s",input);
start=start+length;
}
for(i=0;i<count;i++)
fprintf(fp2,"%s\t\t%s\t\t%lx\t\t
%d\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length);
getch();
}
OUTPUT:
Input File:
link1in.c
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
T 000054 16 100014 15100006 00002F 100014 FFFFC0
M 000024 05+LISTB
M 000054 06+LISTC
M 000058 06+ENDC
M 000064 06+LISTB
E 000000
RESULT:
Thus the program has been done and the output has been verified.
Ex No: 9
AIM:
ALGORITHM:
Input:
link1.c
Output:
link3.c
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
16 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
32 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
48 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
64 xxxxxxxx xxxxxxxx xxxxxxxx 0xxxxxx
80 0xxxxxx 0xxxxxx xxxx0320 1D771000
96 045150014 xxxxxxxx xx0xxxx 0xxxx0
112 xxxxxxxx xxxxxxxx xxxxxxxx 101415
128 10000600 22F1000 14FFFFC0 03100000
144 77202705 100000xx xxxxxxxx xxxx0xx
160 xxxx0xx xxxx0xx xxxx0xx xxxx1000
176 00051000 00051000 20051000 30100000
192 03100000 77100004 05100000 xxxxxxxx
208 xxxxxxxx xxxxxxxx 10003010 00081000
224 01100000 100000
RESULT
Thus the above program has been done and output is verified.
Ex No: 10
AIM:
ALGORITHM:
1.CREATE
2.VIEW
3.APPEND
4.DELETE
5.EXIT
ENTER YOUR CHOICE:1
SAVE:CTRL &S
ENTER THE FILE NAME:ANAND
JEYRAJ
EXIT…
RESULT
Thus the above program has been done and output is verified.