TE COMPUTER
SPOS Lab Program
Scheduling Algorithm
#include<stdio.h>
void FIFO();
void SJF();
void RR();
int main()
{
int ch;
do
{
printf("\n\n MENU\n 1.FIFO\n2.SJF\n3.RR\n4.exit\n\t\n");
printf("\n\n Enter your choice");
printf("\n\n");
scanf("\n\n%d",&ch);
switch(ch)
{
case 1:FIFO();
break;
case 2:SJF();
break;
case 3:RR();
break;
case 4:
exit(0);
default:
printf("\n\tINVALID CHOICE ENTERED.\n");
}
}while(ch!=4);
return(0);
}
void FIFO()
{
int p[10],wt=0,bt=0,b[10],n,i,avf,tr[10];
float sum=0,sum1=0,avwt,avbt;
printf("\n Enter how many processes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter process & burst time:");
scanf("%d%d",&p[i],&b[i]);
}
printf("\n Process\t\tBurst time");
for(i=1;i<=n;i++)
{
printf("\n%d\t\t%d",p[i],b[i]);
}
printf("\n Waiting time of P0:%d",wt);
for(i=1;i<n;i++)
{
wt=wt+b[i];
printf("\n Waiting time of processed %d :%d",i,wt);
sum=sum+wt;
}
avwt=sum/n;
printf("\n Avg waiting time=%0.2f",avwt);
wt=0;
f#include<stdio.h>
void FIFO();
void SJF();
void RR();
int main()
{
int ch;
do
{
printf("\n\n MENU\n 1.FIFO\n2.SJF\n3.RR\n4.exit\n\t\n");
printf("\n\n Enter your choice");
printf("\n\n");
scanf("\n\n%d",&ch);
switch(ch)
{
case 1:FIFO();
break;
case 2:SJF();
break;
case 3:RR();
break;
case 4:
exit(0);
default:
printf("\n\tINVALID CHOICE ENTERED.\n");
}
}while(ch!=4);
return(0);
}
void FIFO()
{
int p[10],wt=0,bt=0,b[10],n,i,avf,tr[10];
float sum=0,sum1=0,avwt,avbt;
printf("\n Enter how many processes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter process & burst time:");
scanf("%d%d",&p[i],&b[i]);
}
printf("\n Process\t\tBurst time");
for(i=1;i<=n;i++)
{
printf("\n%d\t\t%d",p[i],b[i]);
}
printf("\n Waiting time of P0:%d",wt);
for(i=1;i<n;i++)
{
wt=wt+b[i];
or(i=1;i<=n;i++)
{
bt=bt+wt+b[i];
Reader-Writer Problem with Multithreading
printf("\n Waiting time of process %d:%d",i,bt);sum1=sum1+bt;
}
avbt=sum1/n;
printf("\n Average turn around time=%0.2f",avbt);
}
void SJF()
{
int p[10],wt,bt,b[10],n,i,j,avf,temp,temp1,tr[10];
float sum=0,sum1=0,avwt=0,avbt=0;
printf("\nEnter the no. of processes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter process & burst time:");
scanf("%d%d",&p[i],&b[i]
Reader-Writer Problem with Multithreading
);}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp1=p[i];
p[i]=p[j];
p[j]=temp1;
}
}
}
printf("\n Process\t\tBurst time");
for(i=1;i<=n;i++)
{
printf("\n%d\t\t%d",p[i],b[i]);
}
wt=0;
printf("\n Waiting time of P0:%d",wt);
for(i=1;i<n;i++)
{
wt=wt+b[i];
printf("\n Waiting time of processed %d :%d",i,wt);
sum=sum+wt;
}
avwt=sum/n;
printf("\n Avg waiting time=%0.2f",avwt);
wt=0;
bt=0;
for(i=1;i<=n;i++)
{
bt=bt+wt+b[i];
printf("\n Turn around time of process%d: %d",i,bt);
sum1=sum1+bt;
}
avbt=sum1/n;
printf("\n Average turn around time=%0.2f",avbt);
}
void RR()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
printf("\n Enter no of process:");
scanf("%d",&n);
printf("\n Enter burst time for sequence:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("\n Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("\n Process no. Burst time Wait time Turn around time");
for(i=0;i<n;i++)
printf("\n%d %d %d %d",i+1,bt[i],wt[i],tat[i]);
printf("\nAvg wait time is %f \navg turn around is %f",awt,atat);
}
//Experiment No.1 :Pass-I of an Assembler :
....................................................O/P................................................
[root@localhost ~]# cc sc.c
sc.c: In function ‘main’:
sc.c:27: warning: incompatible implicit declaration of built-in function ‘exit’
[root@localhost ~]# ./a.out
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
1
Enter how many processes:5
Enter process & burst time:19
2
Enter process & burst time:2 2
5
Enter process & burst time:3 5
Enter process & burst time:4 3
Enter process & burst time:5 2
Process Burst time
19 2
2 2
3 5
4 3
5 2
Waiting time of P0:0
Waiting time of processed 1 :2
Waiting time of processed 2 :4
Waiting time of processed 3 :9
Waiting time of processed 4 :12
Avg waiting time=5.40
Waiting time of process 1:2
Waiting time of process 2:4
Waiting time of process 3:9
Waiting time of process 4:12
Waiting time of process 5:14
Average turn around time=8.20
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
2
Enter the no. of processes:1 9
Enter process & burst time:2 2
Process Burst time
9 2
Waiting time of P0:0
Avg waiting time=0.00
Turn around time of process1: 2
Average turn around time=2.00
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
Enter the no. of processes:2
Enter process & burst time:5 6
Enter process & burst time:7 8
Process Burst time
5 6
7 8
Waiting time of P0:0
Waiting time of processed 1 :6
Avg waiting time=3.00
Turn around time of process1: 6
Turn around time of process2: 14
Average turn around time=10.00
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
3
Enter no of process:3
Enter burst time for sequence:6 7
9
Enter time quantum:10
Process no. Burst time Wait time Turn around time
1 6 0 6
2 7 6 13
3 9 13 22
Avg wait time is 6.333333
avg turn around is 13.666667
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
#include<stdio.h>
#include<string.h>
struct MOTtable
{
char Mnemonic[6];
int Class;
char Opcode[3];
};
struct symboltable
{
char Symbol[8];
int Address;
int Size;
}ST[20];
struct literaltable
{
int Literal;
int Address;
}LT[10];
int PT[20];
struct intermediatecode
{
int LC;
int Code1,Type1;
int Code2,Type2;
int Code3,Type3;
}IC[30];
static struct MOTtable MOT[28]={{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},
{"MULT",1,"03"},{"MOVER",1,"04"},{"MOVEM",1,"05"},
{"COMP",1,"06"},{"BC",1,"07"},{"DIV",1,"08"},
{"READ",1,"09"},{"PRINT",1,"10"},
{"START",3,"01"},{"END",3,"02"},{"ORIGIN",3,"03"},
{"EQU",3,"04"},{"LTORG",3,"05"},
{"DS",2,"01"},{"DC",2,"02"},
{"AREG",4,"01"},{"BREG",4,"02"},{"CREG",4,"03"},
{"EQ",5,"01"},{"LT",5,"02"},{"GT",5,"03"},{"LE",5,"04"},
{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}};
int nMOT=28; //Number of entries in MOT
int LC=0; //Location counter
int iPT; //Index of next entry in Pool Table
int iLT=0; //Index of next entry in Literal Table
int iST=0; //Index of next entry in Symbol Table
int iIC=0; //Index of next entry in intermediate code table
int searchST(char symbol[])
{
int i;
for(i=0;i<iST;i++)
if(strcmp(ST[i].Symbol,symbol)==0)
return(i);
return(-1);
}
int searchMOT(char symbol[])
{
int i;
for(i=0;i<nMOT;i++)
if(strcmp(MOT[i].Mnemonic,symbol)==0)
return(i);
return(-1);
}
int insertST( char symbol[],int address,int size)
{
strcpy(ST[iST].Symbol,symbol);
ST[iST].Address=address;
ST[iST].Size=size;
iST++;
return(iST-1);
}
void imperative(); //Handle an executable statement
void declaration(); //Handle a declaration statement
void directive(); //Handle an assembler directive
void print_symbol(); //Display symbol table
void print_pool(); //Display pool table
void print_literal(); //Display literal table
void print_opcode(); //Display opcode table
void intermediate(); //Display intermediate code
char s1[8],s2[8],s3[8],label[8];
void LTORG(); //Handle LTORG directive
void DC(); //Handle declaration statement DC
void DS(); //Handle declaration statement DS
void START(); //Handle START directive
int tokencount; //total number of words in a statement
void main()
{
char file1[40],nextline[80];
int len,i,j,temp,errortype;
FILE *ptr1;
clrscr();
printf("\nenter Source file name:");
gets(file1);
ptr1=fopen(file1,"r");
while(!feof(ptr1))
{
//Read a line of assembly program and remove special characters
i=0;
nextline[i]=fgetc(ptr1);
while(nextline[i]!='\n'&& nextline[i]!=EOF )
{
if(!isalnum(nextline[i]))
nextline[i]=' ';
else
nextline[i]=toupper(nextline[i]);
i++;
nextline[i]=fgetc(ptr1);
}
nextline[i]='\0';
//if the nextline is an END statement
sscanf(nextline,"%s",s1); //read from the nextline in s1
if(strcmp(s1,"END")==0)
break;
//if the nextline contains a label
if(searchMOT(s1)==-1)
{
if(searchST(s1)==-1)
insertST(s1,LC,0);
//separate opcode and operands
tokencount=sscanf(nextline,"%s%s%s%s",label,s1,s2,s3);
tokencount--;
}
else
//separate opcode and operands
tokencount=sscanf(nextline,"%s%s%s",s1,s2,s3);
if(tokencount==0)//blank line
continue;//goto the beginning of the loop
i=searchMOT(s1);
if(i==-1)
{
printf("\nWrong Opcode .... %s",s1);
continue;
}
switch (MOT[i].Class)
{
case 1: imperative();break;
case 2: declaration();break;
case 3: directive();break;
default: printf("\Wrong opcode ...%s",s1);
break;
}
}
LTORG();
print_symbol();
getch();
print_pool();
getch();
print_literal();
getch();
print_opcode();
getch();
intermediate();
getch();
}
void imperative()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
LC=LC+1;
if(tokencount>1)
{
index=searchMOT(s2);
if(index != -1)
{
IC[iIC].Code2=index;
IC[iIC].Type2=MOT[index].Class;
}
else
{ //It is a variable
index=searchST(s2);
if(index==-1)
index=insertST(s2,0,0);
IC[iIC].Code2=index;
IC[iIC].Type2=7; //VALUE 7 IS FOR VARIABLES
}
}
if(tokencount>2)
{
if(isdigit(*s3))
{
LT[iLT].Literal=atoi(s3);
IC[iIC].Code3=iLT;
IC[iIC].Type3=8; //VALUE 8 IS FOR LITERAL TYPE
iLT++;
}
else
{
index=searchST(s3);
if(index==-1)
index=insertST(s3,0,0);
IC[iIC].Code3=index;
IC[iIC].Type3=7; //VALUE 7 IS FOR VARIABLES
}
}
iIC++;
}
void declaration()
{
if(strcmp(s1,"DC")==0)
{
DC();
return;
}
if(strcmp(s1,"DS")==0)
DS();
}
void directive()
{
if(strcmp(s1,"START")==0)
{
START();
return;
}
if(strcmp(s1,"LTORG")==0)
LTORG();
}
void LTORG()
{
int i,index;
for(i=PT[iPT];i<iLT;i++)
{
LT[i].Address=LC;
index=searchMOT("DC");
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=LT[i].Literal;
LC=LC+1;
iIC++;
}
iPT++;
PT[iPT]=iLT;
}
void DC()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=atoi(s2);
index=searchST(label);
if(index==-1)
index=insertST(label,0,0);
ST[index].Address=LC;
ST[index].Size=1;
LC=LC+1;
iIC++;
}
void DS()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=atoi(s2);
index=searchST(label);
if(index==-1)
index=insertST(label,0,0);
ST[index].Address=LC;
ST[index].Size=atoi(s2);
LC=LC+atoi(s2);
iIC++;
}
void START()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=atoi(s2);
LC=atoi(s2);
iIC++;
}
void intermediate()
{ int i;
char decode[9][3]={" ","IS","DL","AD","RG","CC","C","S","L"};
printf("\n\nIntermediate Code :");
for(i=0;i<iIC;i++)
{
printf("\n%3d) (%s,%2s)",IC[i].LC,decode[IC[i].Type1]
,MOT[IC[i].Code1].Opcode);
if(IC[i].Type2!=0)
{
if(IC[i].Type2<6)
printf(" (%s,%2s)",decode[IC[i].Type2]
,MOT[IC[i].Code2].Opcode);
else
printf(" (%s,%2d)",decode[IC[i].Type2]
,IC[i].Code2);
}
if(IC[i].Type3!=0)
printf(" (%s,%2d)",decode[IC[i].Type3]
,IC[i].Code3);
}
}
void print_symbol()
{
int i;
printf("\*******symbol table ***********\n");
for(i=0;i<iST;i++)
printf("\n%10s %3d %3d",ST[i].Symbol,ST[i].Address
,ST[i].Size);
}
void print_pool()
{
int i;
printf("\npool table *********\n");
for(i=0;i<iPT;i++)
printf("\n%d",PT[i]);
}
void print_literal()
{
int i;
printf("\nliteral table*******\n");
for(i=0;i<iLT;i++)
printf("\n%5d\t%5d",LT[i].Literal,LT[i].Address);
}
void print_opcode()
{
int i;
printf("\nopcode table *************");
for(i=0;i<nMOT;i++)
if(MOT[i].Class==1)
printf("\n%6s %2s",MOT[i].Mnemonic,MOT[i].Opcode);
}
********* Input / Output ******************
enter Source file name:xx.asm
*******symbol table ***********
L1 100 0
X 108 1
Y 109 2
pool table *********
0
2
literal table*******
5 103
2 104
4 111
opcode table *************
STOP 00
ADD 01
SUB 02
MULT 03
MOVER 04
MOVEM 05
COMP 06
BC 07
DIV 08
READ 09
PRINT 10
Intermediate Code :
0) (AD,01) (C,100)
100) (IS,04) (RG,01) (L, 0)
101) (IS,05) (RG,02) (S, 1)
102) (IS,02) (RG,01) (L, 1)
103) (DL,02) (C, 5)
104) (DL,02) (C, 2)
105) (IS,04) (RG,01) (S, 2)
106) (IS,07) (CC,07) (S, 0)
107) (IS,01) (RG,03) (L, 2)
108) (DL,02) (C, 5)
109) (DL,01) (C, 2)
111) (DL,02) (C, 4)
Contents of the file xx.asm
----------------------------
START 100
L1 MOVER AREG,=5
MOVEM BREG X
SUB AREG,=2
LTORG
MOVER AREG Y
BC any,L1
ADD CREG,4
X DC 5
Y DS 2
END
sc.c: In function ‘main’:
sc.c:27: warning: incompatible implicit declaration of built-in function ‘exit’
[root@localhost ~]# ./a.out
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
1
Enter how many processes:5
Enter process & burst time:19
2
Enter process & burst time:2 2
5
Enter process & burst time:3 5
Enter process & burst time:4 3
Enter process & burst time:5 2
Process Burst time
19 2
2 2
3 5
4 3
5 2
Waiting time of P0:0
Waiting time of processed 1 :2
Waiting time of processed 2 :4
Waiting time of processed 3 :9
Waiting time of processed 4 :12
Avg waiting time=5.40
Waiting time of process 1:2
Waiting time of process 2:4
Waiting time of process 3:9
Waiting time of process 4:12
Waiting time of process 5:14
Average turn around time=8.20
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
2
Enter the no. of processes:1 9
Enter process & burst time:2 2
Process Burst time
9 2
Waiting time of P0:0
Avg waiting time=0.00
Turn around time of process1: 2
Average turn around time=2.00
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
Enter the no. of processes:2
Enter process & burst time:5 6
Enter process & burst time:7 8
Process Burst time
5 6
7 8
Waiting time of P0:0
Waiting time of processed 1 :6
Avg waiting time=3.00
Turn around time of process1: 6
Turn around time of process2: 14
Average turn around time=10.00
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
3
Enter no of process:3
Enter burst time for sequence:6 7
9
Enter time quantum:10
Process no. Burst time Wait time Turn around time
1 6 0 6
2 7 6 13
3 9 13 22
Avg wait time is 6.333333
avg turn around is 13.666667
MENU
1.FIFO
2.SJF
3.RR
4.exit
Enter your choice
//Experiment No.1 :Pass-I of an Assembler :
#include<stdio.h>
#include<string.h>
struct MOTtable
{
char Mnemonic[6];
int Class;
char Opcode[3];
};
struct symboltable
{
char Symbol[8];
int Address;
int Size;
}ST[20];
struct literaltable
{
int Literal;
int Address;
}LT[10];
int PT[20];
struct intermediatecode
{
int LC;
int Code1,Type1;
int Code2,Type2;
int Code3,Type3;
}IC[30];
static struct MOTtable MOT[28]={{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},
{"MULT",1,"03"},{"MOVER",1,"04"},{"MOVEM",1,"05"},
{"COMP",1,"06"},{"BC",1,"07"},{"DIV",1,"08"},
{"READ",1,"09"},{"PRINT",1,"10"},
{"START",3,"01"},{"END",3,"02"},{"ORIGIN",3,"03"},
{"EQU",3,"04"},{"LTORG",3,"05"},
{"DS",2,"01"},{"DC",2,"02"},
{"AREG",4,"01"},{"BREG",4,"02"},{"CREG",4,"03"},
{"EQ",5,"01"},{"LT",5,"02"},{"GT",5,"03"},{"LE",5,"04"},
{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}};
int nMOT=28; //Number of entries in MOT
int LC=0; //Location counter
int iPT; //Index of next entry in Pool Table
int iLT=0; //Index of next entry in Literal Table
int iST=0; //Index of next entry in Symbol Table
int iIC=0; //Index of next entry in intermediate code table
int searchST(char symbol[])
{
int i;
for(i=0;i<iST;i++)
if(strcmp(ST[i].Symbol,symbol)==0)
return(i);
return(-1);
}
int searchMOT(char symbol[])
{
int i;
for(i=0;i<nMOT;i++)
if(strcmp(MOT[i].Mnemonic,symbol)==0)
return(i);
return(-1);
}
int insertST( char symbol[],int address,int size)
{
strcpy(ST[iST].Symbol,symbol);
ST[iST].Address=address;
ST[iST].Size=size;
iST++;
return(iST-1);
}
void imperative(); //Handle an executable statement
void declaration(); //Handle a declaration statement
void directive(); //Handle an assembler directive
void print_symbol(); //Display symbol table
void print_pool(); //Display pool table
void print_literal(); //Display literal table
void print_opcode(); //Display opcode table
void intermediate(); //Display intermediate code
char s1[8],s2[8],s3[8],label[8];
void LTORG(); //Handle LTORG directive
void DC(); //Handle declaration statement DC
void DS(); //Handle declaration statement DS
void START(); //Handle START directive
int tokencount; //total number of words in a statement
void main()
{
char file1[40],nextline[80];
int len,i,j,temp,errortype;
FILE *ptr1;
clrscr();
printf("\nenter Source file name:");
gets(file1);
ptr1=fopen(file1,"r");
while(!feof(ptr1))
{
//Read a line of assembly program and remove special characters
i=0;
nextline[i]=fgetc(ptr1);
while(nextline[i]!='\n'&& nextline[i]!=EOF )
{
if(!isalnum(nextline[i]))
nextline[i]=' ';
else
nextline[i]=toupper(nextline[i]);
i++;
nextline[i]=fgetc(ptr1);
}
nextline[i]='\0';
//if the nextline is an END statement
sscanf(nextline,"%s",s1); //read from the nextline in s1
if(strcmp(s1,"END")==0)
break;
//if the nextline contains a label
if(searchMOT(s1)==-1)
{
if(searchST(s1)==-1)
insertST(s1,LC,0);
//separate opcode and operands
tokencount=sscanf(nextline,"%s%s%s%s",label,s1,s2,s3);
tokencount--;
}
else
//separate opcode and operands
tokencount=sscanf(nextline,"%s%s%s",s1,s2,s3);
if(tokencount==0)//blank line
continue;//goto the beginning of the loop
i=searchMOT(s1);
if(i==-1)
{
printf("\nWrong Opcode .... %s",s1);
continue;
}
switch (MOT[i].Class)
{
case 1: imperative();break;
case 2: declaration();break;
case 3: directive();break;
default: printf("\Wrong opcode ...%s",s1);
break;
}
}
LTORG();
print_symbol();
getch();
print_pool();
getch();
print_literal();
getch();
print_opcode();
getch();
intermediate();
getch();
}
void imperative()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
LC=LC+1;
if(tokencount>1)
{
index=searchMOT(s2);
if(index != -1)
{
IC[iIC].Code2=index;
IC[iIC].Type2=MOT[index].Class;
}
else
{ //It is a variable
index=searchST(s2);
if(index==-1)
index=insertST(s2,0,0);
IC[iIC].Code2=index;
IC[iIC].Type2=7; //VALUE 7 IS FOR VARIABLES
}
}
if(tokencount>2)
{
if(isdigit(*s3))
{
LT[iLT].Literal=atoi(s3);
IC[iIC].Code3=iLT;
IC[iIC].Type3=8; //VALUE 8 IS FOR LITERAL TYPE
iLT++;
}
else
{
index=searchST(s3);
if(index==-1)
index=insertST(s3,0,0);
IC[iIC].Code3=index;
IC[iIC].Type3=7; //VALUE 7 IS FOR VARIABLES
}
}
iIC++;
}
void declaration()
{
if(strcmp(s1,"DC")==0)
{
DC();
return;
}
if(strcmp(s1,"DS")==0)
DS();
}
void directive()
{
if(strcmp(s1,"START")==0)
{
START();
return;
}
if(strcmp(s1,"LTORG")==0)
LTORG();
}
void LTORG()
{
int i,index;
for(i=PT[iPT];i<iLT;i++)
{
LT[i].Address=LC;
index=searchMOT("DC");
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=LT[i].Literal;
LC=LC+1;
iIC++;
}
iPT++;
PT[iPT]=iLT;
}
void DC()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=atoi(s2);
index=searchST(label);
if(index==-1)
index=insertST(label,0,0);
ST[index].Address=LC;
ST[index].Size=1;
LC=LC+1;
iIC++;
}
void DS()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=atoi(s2);
index=searchST(label);
if(index==-1)
index=insertST(label,0,0);
ST[index].Address=LC;
ST[index].Size=atoi(s2);
LC=LC+atoi(s2);
iIC++;
}
void START()
{
int index;
index=searchMOT(s1);
IC[iIC].Type1=IC[iIC].Type2=IC[iIC].Type3=0; //intialize
IC[iIC].LC=LC;
IC[iIC].Code1=index;
IC[iIC].Type1=MOT[index].Class;
IC[iIC].Type2=6; //6 IS TYPE FOR CONSTANTS
IC[iIC].Code2=atoi(s2);
LC=atoi(s2);
iIC++;
}
void intermediate()
{ int i;
char decode[9][3]={" ","IS","DL","AD","RG","CC","C","S","L"};
printf("\n\nIntermediate Code :");
for(i=0;i<iIC;i++)
{
printf("\n%3d) (%s,%2s)",IC[i].LC,decode[IC[i].Type1]
,MOT[IC[i].Code1].Opcode);
if(IC[i].Type2!=0)
{
if(IC[i].Type2<6)
printf(" (%s,%2s)",decode[IC[i].Type2]
,MOT[IC[i].Code2].Opcode);
else
printf(" (%s,%2d)",decode[IC[i].Type2]
,IC[i].Code2);
}
if(IC[i].Type3!=0)
printf(" (%s,%2d)",decode[IC[i].Type3]
,IC[i].Code3);
}
}
void print_symbol()
{
int i;
printf("\*******symbol table ***********\n");
for(i=0;i<iST;i++)
printf("\n%10s %3d %3d",ST[i].Symbol,ST[i].Address
,ST[i].Size);
}
void print_pool()
{
int i;
printf("\npool table *********\n");
for(i=0;i<iPT;i++)
printf("\n%d",PT[i]);
}
void print_literal()
{
int i;
printf("\nliteral table*******\n");
for(i=0;i<iLT;i++)
printf("\n%5d\t%5d",LT[i].Literal,LT[i].Address);
}
void print_opcode()
{
int i;
printf("\nopcode table *************");
for(i=0;i<nMOT;i++)
if(MOT[i].Class==1)
printf("\n%6s %2s",MOT[i].Mnemonic,MOT[i].Opcode);
}
********* Input / Output ******************
enter Source file name:xx.asm
*******symbol table ***********
L1 100 0
X 108 1
Y 109 2
pool table *********
0
2
literal table*******
5 103
2 104
4 111
opcode table *************
STOP 00
ADD 01
SUB 02
MULT 03
MOVER 04
MOVEM 05
COMP 06
BC 07
DIV 08
READ 09
PRINT 10
Intermediate Code :
0) (AD,01) (C,100)
100) (IS,04) (RG,01) (L, 0)
101) (IS,05) (RG,02) (S, 1)
102) (IS,02) (RG,01) (L, 1)
103) (DL,02) (C, 5)
104) (DL,02) (C, 2)
105) (IS,04) (RG,01) (S, 2)
106) (IS,07) (CC,07) (S, 0)
107) (IS,01) (RG,03) (L, 2)
108) (DL,02) (C, 5)
109) (DL,01) (C, 2)
111) (DL,02) (C, 4)
Contents of the file xx.asm
----------------------------
START 100
L1 MOVER AREG,=5
MOVEM BREG X
SUB AREG,=2
LTORG
MOVER AREG Y
BC any,L1
ADD CREG,4
X DC 5
Y DS 2
END