SS Lab - PRGRM - 1 PDF
SS Lab - PRGRM - 1 PDF
SOFTWARE
LAB
C
G Giridhar
CS-5-B
Roll No- 21
1
CONTENTS
2 File Organization 6
3 Banker’s Algorithm 14
11 Absolute Loader 36
Experiment 1 :
Simulate the following non-preemptive CPU scheduling algorithms to find turnaround
time and waiting time.
a) FCFS b) SJF c) Round Robin (pre-emptive) d) Priority
Program :
#include<stdio.h>
void main(){
int choice,size,num=1;
int order[size],burst[size],arrival[size],turn=0,wait,priority[size];
int pos,temp,small,quant;
printf("Enter your choice : \n 1.FCFS \n 2.SJF \n 3.RR \n 4.Priority ");
scanf("%d",&choice);
printf("\n");
for(int counter=0;counter<size;counter++)
{
printf("Enter the burst time and arrival time \n");
scanf("%d %d",&burst[counter],&arrival[counter]);
order[counter]=num;
num++;
}
switch(choice){
case 1:{
printf("%d \t %d \t %d \t %d ms \t %d ms
\n",order[j],burst[j],arrival[j],turn,wait);
break;
}
case 2:{
//SORTING
for(int i=0;i<size;i++)
{ for(int j=1;j<size;j++)
{
if(burst[j]<burst[j-1])
{ //SWAPPING BURSTS
temp=burst[j];
burst[j]=burst[j-1];
burst[j-1]=temp;
//SWAPPING ORDERS
temp=order[j];
order[j]=order[j-1];
order[j-1]=temp;
}
}
}
break;
4
}
case 3:{
printf("Enter the time slice :");
scanf("%d",&quant);
int turn[size],wait[size],stop,counter=0,t=0,total=0;
int rem_bt[size];
for(int i=0;i<size;i++)
{
total+=burst[i];
wait[i]=0;
rem_bt[i]=burst[i];
}
while (1)
{
int flag = 1;
if (rem_bt[i] > 0)
{
flag = 0;
rem_bt[i] -= quant;
}
else
{
t = t + rem_bt[i];
wait[i] = t - burst[i];
rem_bt[i] = 0;
}
}
}
if(flag == 1)
break;
}
for(int i=0;i<size;i++)
turn[i]=wait[i]+burst[i];
printf("SI \t Bst \t Arr \t Wait \t Turn \n");
5
for(int j=0;j<size;j++)
{
printf("%d \t %d \t %d \t %d ms \t %d ms
\n",order[j],burst[j],arrival[j],wait[j],turn[j]);
}
break;
}
case 4:{
break;
}
default :{printf("NO Valid Option");break;}
}
}
Experiment 2:
Simulate the following file organization techniques
a) Single level directory b) Two level directory c) Hierarchical
Program :
a) Single level directory :
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
struct{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main(){
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
7
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}
#include<ctype.h>
struct{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
9
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
10
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}
c) Hierarchical :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
struct node
{
char name[128];
bool isDir;
struct node *p;
struct node *c[100];
int i;
} * head, *curr;
void ls()
{
if (curr->i == 0)
{
printf("Empty directory\n");
return;
}
int i=0;
for ( i = 0; i < curr->i; i++)
{
if (curr->c[i]->isDir)
printf("*%s* ", curr->c[i]->name);
else
11
void add(bool d)
{
printf("Enter the Name:\n");
char fname[128];
scanf("%s", fname);
struct node *temp = (struct node *)malloc(sizeof(struct node));
strcpy(temp->name, fname);
temp->isDir = d;
temp->p = curr;
temp->i=0;
curr->c[curr->i] = temp;
curr->i = (curr->i) + 1;
}
void cd()
{
printf("Enter directory name:\n");
char dname[128];
scanf("%s", dname);
int i=0;
for (i = 0; i < curr->i; i++)
{
if (!strcmp(curr->c[i]->name, dname) && curr->c[i]->isDir)
{
curr = curr->c[i];
return;
}
}
printf("Directory not present.\n");
}
void cdparent()
{
if (curr->p == NULL)
{
printf("You are at the root directory\n");
return;
}
12
curr = curr->p;
}
void del(bool d)
{
printf("Enter name of file or directory to delete:\n");
char name[128];
scanf("%s", name);
int i =0;
for ( i = 0; i < curr->i; i++)
{
if (!strcmp(curr->c[i]->name, name) && ((d && curr->c[i]->isDir == true) || (!d &&
curr->c[i]->isDir == false)))
{
int t = i;
while (t < (curr->i) - 1)
{
curr->c[t] = curr->c[t + 1];
t++;
}
curr->i = (curr->i) - 1;
printf("Successfully deleted.\n");
return;
}
}
printf("Not found\n");
}
void main()
{
int in;
head = (struct node *)malloc(sizeof(struct node));
strcpy(head->name, "root");
head->isDir = true;
head->p = NULL;
head->i = 0;
curr = head;
while (true)
{
printf("\n\nYou are in %s directory.\n1. show everything in this directory\n2. change
directory\n3. go to parent directory\n4. add new file\n5. delete file\n6. create new
directory\n7. delete directory\n8. exit\n", curr->name);
scanf("%d", &in);
13
switch (in)
{
case 1:
ls();
break;
case 2:
cd();
break;
case 3:
cdparent();
break;
case 4:
add(false);
break;
case 5:
del(false);
break;
case 6:
add(true);
break;
case 7:
del(true);
break;
default:
exit(0);
}
}
}
14
Experiment 3 :
Implement the banker’s algorithm for deadlock avoidance.
Program :
#include<stdio.h>
#include<stdlib.h>
void main(){
int p=0,r=0,i,j,count=0,flag;
////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
scanf("%d",&alloc[i][j]);
}
}
/*
//Output for alloc
for(int i=0;i<p;i++){
for (int j = 0; j < r; j++)
{
printf("%d ",alloc[i][j]);
}
printf("\n");
}
*/
//////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//Counting each resource allocated
for ( i = 0; i < r; i++)
{ available[i]=num[i];
work[i]=num[i];
for ( j = 0; j < p; j++)
{
available[i]-=alloc[j][i];
work[i]-=alloc[j][i];
}
}
/*
for (int i = 0; i < r; i++)
16
{
printf("%d ",available[i]);
}
*/
///////////////////////////////////////////////////////////
//Maximum Matrix
printf("Enter the Maximum resource table : \n");
printf("---------------------------------------\n");
for(i=0;i<p;i++){
for(j=0;j<r;j++){
scanf("%d",&max[i][j]);
}
}
///////////////////////////////////////////////////////////
//Need Matrix
for ( i = 0; i < p; i++)
{
for ( j = 0; j < r; j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
//Output for Need
for(int i=0;i<p;i++){
for (int j = 0; j < r; j++)
{
printf("%d ",need[i][j]);
}
printf("\n");
}
///////////////////////////////////////////////////////////
//SAFTEY ALGORITHM
while (count<p)
{
flag=0;
for ( i = 0; i < p; i++)
17
{
if (finish[i] == 0)
{
for (j = 0; j < r; j++)
if (need[i][j]>work[j])
break;
if (j == r)
{
for ( int k = 0; k < r; k++)
work[k] += alloc[i][k];
order[count]=i;
count++;
finish[i]=1;
flag=1;
}
}
}
if (flag == 0)
{
printf("System is not in safe state");
exit(0);
}
}
printf("System is in safe state\n Order is : \t");
for(i=0;i<p;i++)
printf(" P%d -",order[i]+1);
printf("\n");
}
18
Experiment 4:
Implement the producer-consumer problem using semaphores.
Program :
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int empty=0,full=0,n=0,mutex=1;
int pro_cnt=0,con_cnt=0;
int wait(int);
int signal(int);
void main(){
int choice;
void producer();
void consumer();
printf("Enter the buffer size : ");
scanf("%d",&n);
empty=n;
printf("Enter your choice :-\n");
printf("1.Produce\t2.Consume\t3.Exit\n");
while(1){
printf("Your choice : ");
scanf("%d",&choice);
printf("\n");
19
switch(choice){
case 1:{ if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!!!\n");
break;
}
case 2:{ if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!!!!\n");
break;
}
default:exit(0);break;
}
printf("Number of produced items : %d \nNumber of consumed items : %d
\n",pro_cnt,con_cnt);
}
}
//Producer Function
void producer(){
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
pro_cnt++;
printf("Produced Item.. \n");
mutex=signal(mutex);
}
//Consumer Function
void consumer(){
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
con_cnt++;
printf("Consumed Item.. \n");
mutex=signal(mutex);
}
//Wait
int wait(int semaphore){
return(--semaphore);
}
//Signal
int signal(int semaphore){
20
return(++semaphore);
}
Experiment 5 :
Write a program to simulate the working of the dining philosopher’s problem.
Program :
#include<stdio.h>
#include<stdlib.h>
void pickup(int);
void putdown(int);
void test(int);
void initialize();
enum{THINKING,HUNGRY,EATING} state[5];
int flag[5];
void main(){
int phil=0,choice=0;
initialize();
while(1){
printf("Enter the philosopher number(1-5) : ");
scanf("%d",&phil);
printf("Enter the operation needed \n 1.Start Eating\t2.Stop Eating\t 3.Exit\n");
scanf("%d",&choice);
switch(choice){
21
case 1:{
pickup(phil);
break; }
case 2:{
putdown(phil);
break; }
default:exit(0);break;
} } }
void initialize(){
int i=0;
for(i=0;i<5;i++){
state[i]=THINKING;
flag[i]=0; } }
void pickup(int id){
state[id]=HUNGRY;
test(id);
if(state[id]!=EATING)
flag[id]=0;
if(flag[id]==1)
printf("Philosopher %d is eating!\n",id);
else
printf("Philosopher %d cannot eat\n",id);
}
void putdown(int id){
state[id]=THINKING;
test((id+4)%5);
test((id+1)%5);
printf("Philosopher %d is thinking!\n",id); }
void test(int i){
if((state[(i+4)%5]!=EATING) && (state[i]==HUNGRY) && (state[(i+1)%5]!=EATING)){
state[i]=EATING;
flag[i]=1; }
}
22
Experiment 6 :
Simulate the following disk scheduling algorithms.
a) FCFS b)SCAN c) C-SCAN
Program :
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main(){
int choice,n,start,seek_time,range,temp,index=0,count=0;
printf("DISK SCHEDULING \n");
printf("--------------------\n");
printf("Enter the range : ");
scanf("%d",&range);
printf("Enter the number of disk requests : ",range);
scanf("%d",&n);
printf("Enter the head position (< %d): ",range);
scanf("%d",&start);
if(start>range)
exit(0);
23
printf("%d\t%d\n",count,a[i]);
seek_time+=abs(a[i+1]-a[i]);
count++;
}
printf("Seek Time : %d ms \n",seek_time);
break;
}
default:exit(0);break;
}
}
}
Experiment 7 :
Implement pass one of a two pass assembler.
Program :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2,*f3,*f4,*f5;
int lc,sa,l,op1,o,len;
char m1[20],la[20],op[20],otp[20];
f1=fopen("input.txt","r");
f3=fopen("symtab.txt","w");
f4=fopen("length.txt","w");
f5=fopen("start.txt","w");
fscanf(f1,"%s %s %d",la,m1,&op1);
26
if(strcmp(m1,"START")==0)
{
sa=op1;
fprintf(f5,"%d",sa);
lc=sa;
printf("\t%s\t%s\t%d\n",la,m1,op1);
}
else
lc=0;
fscanf(f1,"%s %s",la,m1);
while(!feof(f1))
{
fscanf(f1,"%s",op);
printf("\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
if(strcmp(la,"-")!=0)
{
fprintf(f3,"\n%d\t%s\n",lc,la);
}
f2=fopen("optab.txt","r");
fscanf(f2,"%s %d",otp,&o);
while(!feof(f2))
{
if(strcmp(m1,otp)==0)
{
lc=lc+3;
break;
}
fscanf(f2,"%s %d",otp,&o);
}
fclose(f2);
if(strcmp(m1,"WORD")==0)
{
lc=lc+3;
}
else if(strcmp(m1,"RESW")==0)
{
op1=atoi(op);
lc=lc+(3*op1);
}
else if(strcmp(m1,"BYTE")==0)
{
if(op[0]=='X')
lc=lc+1;
27
else
{
len=strlen(op)-2;
lc=lc+len;
}
}
else if(strcmp(m1,"RESB")==0)
{
op1=atoi(op);
lc=lc+op1;
}
fscanf(f1,"%s%s",la,m1);
}
if(strcmp(m1,"END")==0)
{
printf("Program length = %d \n",lc-sa);
fprintf(f4,"%d",lc-sa);
}
fclose(f1);
fclose(f3);
fclose(f4);
}
Experiment 8:
Implement pass two of a two pass assembler.
Program :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
FILE *fint,*ftab,*flen,*fsym;
int op1[10],txtlen,txtlen1,i,j=0,len;
char
add[5],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],symtab[10],op
mne[10];
28
fint=fopen("input2.txt","r");
flen=fopen("length.txt","r");
ftab=fopen("optab.txt","r");
fsym=fopen("symtab.txt","r");
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
if(strcmp(mne,"START")==0)
{
strcpy(start,operand);
fscanf(flen,"%d",&len);
}
printf("H^%s^%s^%d\nT^00%s^",label,start,len,start);
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
while(strcmp(mne,"END")!=0)
{
fscanf(ftab,"%s%s",opmne,op);
while(!feof(ftab))
{
if(strcmp(mne,opmne)==0)
{
fclose(ftab);
fscanf(fsym,"%s%s",symadd,symtab);
while(!feof(fsym))
{
if(strcmp(operand,symtab)==0)
{
printf("%s%s^",op,symadd);
break;
}
else
fscanf(fsym,"%s%s",symadd,symtab);
}
break;
}
else
fscanf(ftab,"%s%s",opmne,op);
}
if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
{
if(strcmp(mne,"WORD")==0)
printf("0000%s^",operand);
29
else
{
len=strlen(operand);
for(i=2;i<len;i++)
{
printf("%d",operand[i]);
}
printf("^");
}
}
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
ftab=fopen("optab.txt","r");
fseek(ftab,SEEK_SET,0);
}
printf("\nE^00%s",start);
fclose(fint);
fclose(ftab);
fclose(fsym);
fclose(flen);
}
Experiment 9:
Implement a single pass assembler.
Program :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4,*f5;
int lc,sa,i=0,j=0,m[10],pgmlen,len,k,len1,l=0;
char name[10],opnd[10],la[10],mne[10],s1[10],mne1[10],opnd1[10];
char lcs[10],ms[10];
30
char sym[10],symaddr[10],obj1[10],obj2[10],s2[10],q[10],s3[10];
f1=fopen("input3.txt","r");
f2=fopen("optab3.txt","r");
f3=fopen("symtab3.txt","w+");
f4=fopen("symtab13.txt","w+");
f5=fopen("output3.txt","w+");
fscanf(f1,"%s%s%s",la,mne,opnd);
if(strcmp(mne,"START")==0)
{
sa=atoi(opnd);
strcpy(name,la);
lc=sa;
}
strcpy(s1,"*");
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"END")!=0)
{
if(strcmp(la,"-")==0)
{
fscanf(f2,"%s%s",mne1,opnd1);
while(!feof(f2))
{
if(strcmp(mne1,mne)==0)
{
m[i]=lc+1;
fprintf(f3,"%s\t%s\n",opnd,s1);
fprintf(f5,"%s\t0000\n",opnd1);
lc=lc+3;
i=i+1;
break;
}
else
fscanf(f2,"%s%s",mne1,opnd1);
}
}
else
{
fseek(f3,SEEK_SET,0);
fscanf(f3,"%s%s",sym,symaddr);
while(!feof(f3))
{
if(strcmp(sym,la)==0)
{
31
//itoa(lc,lcs,10);
snprintf(lcs,10,"%d",lc);
fprintf(f4,"%s\t%s\n",la,lcs);
//itoa(m[j],ms,10);
snprintf(ms,10,"%d",m[j]);
j=j+1;
fprintf(f5,"%s\t%s\n",ms,lcs);
i=i+1;
break;
}
else
fscanf(f3,"%s%s",sym,symaddr);
} //f3
if(strcmp(mne,"RESW")==0)
lc=lc+3*atoi(opnd);
else if(strcmp(mne,"BYTE")==0)
{
strcpy(s2,"-");
len=strlen(opnd);
lc=lc+len-2;
for(k=2;k<len;k++)
{
q[l]=opnd[k];
l=l+1;
}
fprintf(f5,"%s\t%s\n",q,s2);
break;
}
else if(strcmp(mne,"RESB")==0)
lc=lc+atoi(opnd);
else if(strcmp(mne,"WORD")==0)
{
strcpy(s3,"#");
lc=lc+3;
fprintf(f5,"%s\t%s\n",opnd,s3);
break;
}
} // else la=-
fseek(f2,SEEK_SET,0);
fscanf(f1,"%s%s%s",la,mne,opnd);
}
fseek(f5,SEEK_SET,0);
pgmlen=lc-sa;
32
printf("H^%s^%d^0%x\n",name,sa,pgmlen);
printf("T^");
printf("00%d^0%x",sa,pgmlen);
fscanf(f5,"%s%s",obj1,obj2);
while(!feof(f5))
{
if(strcmp(obj2,"0000")==0)
printf("^%s%s",obj1,obj2);
else if(strcmp(obj2,"-")==0)
{
printf("^");
len1=strlen(obj1);
for(k=0;k<len1;k++)
printf("%d",obj1[k]);
}
else if(strcmp(obj2,"#")==0)
{
printf("^");
printf("%s",obj1);
}
fscanf(f5,"%s%s",obj1,obj2);
}
fseek(f5,SEEK_SET,0);
fscanf(f5,"%s%s",obj1,obj2);
while(!feof(f5))
{
if(strcmp(obj2,"0000")!=0)
{
if(strcmp(obj2,"-")!=0)
{
if(strcmp(obj2,"#")!=0)
{
printf("\n");
printf("T^%s^02^%s",obj1,obj2);
}
}
}
fscanf(f5,"%s%s",obj1,obj2);
}
printf("\nE^00%d",sa);
}
33
Experiment 10 :
Implement a two pass macro processor.
Program :
Pass 1 :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
34
void main()
{
FILE *f1,*f2,*f3;
char mne[20],opnd[20],la[20];
f1=fopen("input.txt","r");
f2=fopen("namtab.txt","w+");
f3=fopen("deftab.txt","w+");
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"MEND")!=0)
{
if(strcmp(mne,"MACRO")==0)
{
fprintf(f2,"%s\n",la);
fprintf(f3,"%s\t%s\n",la,opnd);
}
else
fprintf(f3,"%s\t%s\n",mne,opnd);
fscanf(f1,"%s%s%s",la,mne,opnd);
}
fprintf(f3,"%s",mne);
fclose(f1);
fclose(f2);
fclose(f3);
printf("\nPass 1 of 2 pass macroprocessor is successful.");
}
Pass 2 :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2,*f3,*f4,*f5;
int i,len;
char mne[20],opnd[20],la[20],name[20],mne1[20],opnd1[20],arg[20];
f1=fopen("input.txt","r");
f2=fopen("namtab.txt","r");
f3=fopen("deftab.txt","r");
f4=fopen("argtab.txt","w+");
f5=fopen("output.txt","w");
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"END")!=0)
35
{
if(strcmp(mne,"MACRO")==0)
{
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"MEND")!=0)
fscanf(f1,"%s%s%s",la,mne,opnd);
}
else
{
fscanf(f2,"%s",name);
if(strcmp(mne,name)==0)
{
len=strlen(opnd);
for(i=0;i<len;i++)
{
if(opnd[i]!=',')
fprintf(f4,"%c",opnd[i]);
else
fprintf(f4,"\n");
}
fseek(f2,SEEK_SET,0);
fseek(f4,SEEK_SET,0);
fscanf(f3,"%s%s",mne1,opnd1);
fprintf(f5,".\t%s\t%s\n",mne1,opnd);
fscanf(f3,"%s%s",mne1,opnd1);
while(strcmp(mne1,"MEND")!=0)
{
if((opnd1[0]=='&'))
{
fscanf(f4,"%s",arg);
fprintf(f5,"-\t%s\t%s\n",mne1,arg);
}
else
fprintf(f5,"-\t%s\t%s\n",mne1,opnd1);
fscanf(f3,"%s%s",mne1,opnd1);
}
}
else
fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
}
fscanf(f1,"%s%s%s",la,mne,opnd);
}
fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
36
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
printf("\nPass 2 of 2 Pass Macroprocessor is Successful.");
}
Experiment 11:
Implement an absolute loader.
Program :
#include<stdio.h>
#include<string.h>
37
char input[10],label[10],ch1,ch2;
int addr, w=0, start, ptaddr, l, length=0, end, count=0, k, taddr, address, i=0;
FILE *fp1,*fp2;
void check();
void main() {
fp1=fopen("INPUT.dat","r");
fp2=fopen("OUTPUT.dat","w");
fscanf(fp1,"%s",input);
printf("\n\n\t\t\t\tABSOLUTE LOADER\n");
fprintf(fp2,"\n-------------------------------------------------------\n");
fprintf(fp2,"MEMORY ADDRESS\t\t\tCONTENTS");
fprintf(fp2,"\n-------------------------------------------------------\n");
while(strcmp(input,"E")!=0) {
if(strcmp(input,"H")==0) {
fscanf(fp1,"%s %x %x %s",label,&start,&end,input);
address=start;
}
else if(strcmp(input,"T")==0) {
l=length;
ptaddr=addr;
fscanf(fp1,"%x %x %s",&taddr,&length,input);
addr=taddr;
if(w==0) {
ptaddr=address;
w=1;
}
for(k=0;k<(taddr-(ptaddr+l));k++) {
address=address+1;
fprintf(fp2,"xx");
count++;
if(count==4) {
fprintf(fp2," ");
i++;
if(i==4) {
fprintf(fp2,"\n\n%x\t\t",address);
i=0;
}
count=0; } }
if(taddr==start)
fprintf(fp2,"\n\n%x\t\t",taddr);
fprintf(fp2,"%c%c",input[0],input[1]);
check();
fprintf(fp2,"%c%c",input[2],input[3]);
38
check();
fprintf(fp2,"%c%c",input[4],input[5]);
check();
fscanf(fp1,"%s",input);
}
else {
fprintf(fp2,"%c%c",input[0],input[1]);
check();
fprintf(fp2,"%c%c",input[2],input[3]);
check();
fprintf(fp2,"%c%c",input[4],input[5]);
check();
fscanf(fp1,"%s",input);
} }
fprintf(fp2,"\n-------------------------------------------------------\n");
fclose(fp1);
fclose(fp2);
printf("\n\n The contents of output file:\n\n");
fp2=fopen("OUTPUT.dat","r");
ch2=fgetc(fp2);
while(ch2!=EOF) {
printf("%c",ch2);
ch2=fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
}
void check() {
count++;
address++;
taddr=taddr+1;
if(count==4) {
fprintf(fp2," ");
i++;
if(i==4) {
fprintf(fp2,"\n\n%x\t\t",taddr);
i=0;
}
count=0; } }
Experiment 12 :
Implement a symbol table with suitable hashing.
Program :
39
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX 11
char l[10];
struct symb {
int add;
char label[10];
}sy[11];
void search() {
FILE *fp1;
char la[10];
int set=0,s;
int j,i;
printf("\nEnter the Label:");
scanf("%s",la);
fp1=fopen("symbol.txt","r");
for(i=0;i<MAX;i++) {
fscanf(fp1,"%d%d",&j,&sy[i].add);
if(sy[i].add!=0)
fscanf(fp1,"%s",sy[i].label);
}
for(i=0;i<MAX;i++) {
if(sy[i].add!=0) {
if(strcmp(sy[i].label,la)==0) {
set=1;
s=sy[i].add;
}
}
}
if(set==1)
printf("\nThe Label --%s-- is present in the Symbol Table at Address:%d",la,s);
else
printf("\nThe Label is Not Present in the Symbol Table!!");
}
void display(int a[MAX]) {
FILE *fp;
int i;
fp=fopen("symbol.txt","w");
printf("\nThe Symbol Table");
printf("\n*****************");
printf("\nHash Values\tAddress\tLabel");
40
for(i=0;i<MAX;i++) {
printf("\n%d\t %d\t %s",i,sy[i].add,sy[i].label);
fprintf(fp,"\n%d %d %s",i,sy[i].add,sy[i].label);
}
fclose(fp);
}
int create(int num) {
int key;
key=num%11;
return key;
}
void lprob(int a[MAX],int key,int num) {
int flag,i,count=0;
flag=0;
if(a[key]==0) {
a[key]=num;
sy[key].add=num;
strcpy(sy[key].label,l);
}
else {
i=0;
while(i<MAX) {
if(a[i]!=0)
count++;
i++;
}
if(count==MAX) {
printf("\nHash table is Full!!");
display(a);
exit(1);
}
for(i=key+1;i<MAX;i++)
if(a[i]==0) {
a[i]=num;
flag=1;
sy[key].add=num;
strcpy(sy[key].label,l);
break;
}
for(i=0;i<key && flag==0;i++)
if(a[i]==0) {
a[i]=num;
flag=1;
41
sy[key].add=num;
strcpy(sy[key].label,l);
break;
}
}
}
void main() {
int a[MAX],num,key,i,ch;
char ans='y';
for(i=0;i<MAX;i++)
a[i]=0;
do {
printf("\nSymbol Table Menu\n1.Create a Symbol Table\n2.Search in the Symbol
Table\n3.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch) {
case 1: while(ans=='y') {
printf("\nEnter the Address:");
scanf("%d",&num);
key=create(num);
printf("\nEnter The Label:");
scanf("%s",l);
lprob(a,key,num);
printf("\nDo you want to Continue(y/n)?");
scanf(" %c",&ans);
}
display(a);
break;
case 2: search();
break;
case 3: exit(0); }
}while(ch<=3);
}