You are on page 1of 19

Practical No :1

Problem Statement: Design suitable data structure & implement pass-1 & pass-2 of two
pass assembler foe pseudo machines. Implementation should consist of a few instructions
from each category & few assembler directives.

Main file
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char opcode[10], operand[10], label[10], code[10], mnemonic[3];
int locctr, start, length;
FILE *fp1, *fp3, *fp4, *fp2;

fp1=fopen("input.txt", "r");
fp2=fopen("optab.txt", "r");
fp3=fopen("symtab.txt", "w");
fp4=fopen("output.txt", "w");

fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);

if(strcmp(opcode, "START")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand);
fscanf(fp1, "%s\t%s\t%s\n", label, opcode, operand);
}
else
locctr=0;

while(strcmp(opcode,"END")!=0)
{
fprintf(fp4, "%d\t", locctr);
if (strcmp(label, "**")!=0)
fprintf(fp3, "%s\t%d\n", label, locctr);
fscanf(fp2, "%s\t%s", code, mnemonic);

while(strcmp(code, "END")!=0)
{
if(strcmp(opcode, code)==0)
{
locctr+=3;
break;
}
fscanf(fp2, "%s\t%s", code, mnemonic);
}
if(strcmp(opcode, "WORD")==0)
locctr+=3;

else if(strcmp(opcode, "RESW")==0)


locctr+=(3*(atoi(operand)));
else if (strcmp(opcode, "RESB")==0)
locctr+=atoi(operand);
else if (strcmp(opcode, "BYTE")==0)
++locctr;

fprintf(fp4, "%s\t%s\t%s\t\n", label, opcode, operand);


fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
}

// fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);

length=locctr-start;

printf("The length of the code: %d\n", length);

fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}

Input File
** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 2
FIVE WORD 5
CHARZ BYTE C'2'
C1 RESB 1
** END **

OPT File
START *
LDA 03
STA 0F
LDCH 53
STCH 57
END *
Output :
Practical No : 3
Problem Statement : Design sutaible data structures & implement pass-1 of a two pass
macro processor using c. the output of pass-1 (MNT, MDT, ALA, & Intermediate code file
without any macro defiation ) should be input for pass-II

Code :

Main File

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char line[80],t1[10],t2[20],t3[10],FPN[20],APN[20],mname[10];
int count , v1,v2,v3,v4;
FILE *ifp;

int main()
{
int t21,t31,index=1;
ifp= fopen("int.txt","r");
while(!feof(ifp))
{
fgets(line,179,ifp);
count = sscanf(line,"%s%s%s",t1,t2,t3);
if(strcmp("MACRO",t1)==0)
{
strcpy(mname,t2);
printf("\n macro name table");
printf("\n----------------\n");
}
if(strcmp(mname,t2)==0)
{
strcpy(FPN,t3);
printf("\n\n\n**FORMAL PARAMETER NAME TABLE**");
printf("\n-------------------------------:\n");
printf("\nINDEX\t\t:MACRO NAME");
printf("\n%d\t:%s",index,FPN);
}
if(strcmp(mname,t1)==0)
{
strcpy(APN,t2);
printf("\n\n\n**ACTUAL PARAMETER NAME TABLE**");
printf("\n----------------------:\n");
printf("\nINDEX\t\t:MACRO NAME");
printf("\n%d\t:%s",index,APN);
}
}}
INI txt file

MACRO ADDS X
ADD AREG BREG MEND
START 100
MOVER AREG CREG
ADDS X1
SUB AREG CREG
END4

OUTPUT :
Practical No : 4
Problem Statement : Write a program to simulate CPU scheduling algorithms : FCFS,
SJF, priority algorithm & round robin .

Code :

FCFS :

#include<stdio.h>
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
wt[0] = 0;

for (int i = 1; i < n ; i++ )


wt[i] = bt[i-1] + wt[i-1] ;
}

void findTurnAroundTime( int processes[], int n,


int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

void findavgTime( int processes[], int n, int bt[])


{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst time Waiting time Turn around time\n");
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);
return 0;
}

Output :

SJF :

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& execution time:");
//flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

Output :
Practical No : 5
Problem Statement : Write a program to simulate memory replacement strategies – First
fit, Best fir, Worst fir and net fit.

Code :

First fit code:

#include<stdio.h>
#define max 25

void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];int flag,flagn[max],fragi = 0,fragx = 0;

printf("\n\tMemory Management Scheme - First Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of Process:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");

for(i=1;i<=nb;i++) {
printf("Block %d:",i);
scanf("%d",&b[i]);
ff[i] = i;
}
printf("Enter the size of the Processes :-\n");

for(i=1;i<=nf;i++) {
printf("Process %d:",i);
scanf("%d",&f[i]);
}
int x = 1;
printf("\n\nProcess_No\tProcess_Size\tBlock_No\tBlock_Size\tFragment\n");
for(i=1;i<=nf;i++)
{
flag = 1;
for(j=x;j<=nb;j++) /*just used x for starting from index 1 and added
in else x = 1 or else it will be next fit*/
{
if(f[i] <= b[j]){
flagn[j] = 1;
printf("%-15d\t%-15d\t%-15d\t%-15d\t",i, f[i],ff[j],b[j]);
b[j] = b[j] - f[i];
fragi = fragi + b[j];
printf("%-15d\n",b[j]);
break;
}
else
{flagn[j] = 0;
x = 1;
flag++;
}
}
if(flag > nb)
printf("%-15d\t%-15d\t%-15s\t%-15s\t%-15s\n",i,
f[i],"WAIT...","WAIT...","WAIT...");
}
}

Output :

Best Fit code :

#include<stdio.h>
#define max 25

void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max],fragi = 0;
printf("\n\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++) {
printf("Block %d:",i);
scanf("%d",&b[i]);
ff[i] = i;
}
printf("Enter the size of the Processes :-\n");

for(i=1;i<=nf;i++) {
printf("Process %d:",i);
scanf("%d",&f[i]);
}
int y,m,z,temp1,flag;
for(y=1;y<=nb;y++)
{
for(z=y;z<=nb;z++)
{
if(b[y]>b[z])
{
temp=b[y];
b[y]=b[z];
b[z]=temp;
temp1=ff[y];
ff[y]=ff[z];
ff[z]=temp1;
}
}
}
int flagn[max];
int fragx = 0;

printf("\n\nProcess_No\tProcess_Size\tBlock_No\tBlock_Size\tFragment\n");
for(i=1;i<=nf;i++)
{
flag = 1;
for(j=1;j<=nb;j++)
{
if(f[i] <= b[j]){
flagn[j] = 1;
printf("%-15d\t%-15d\t%-15d\t%-15d\t",i, f[i],ff[j],b[j]);
b[j] = b[j] - f[i];
fragi = fragi + b[j];
printf("%-15d\n",b[j]);
break;
}
else
{flagn[j] = 0;
flag++;
}
}
if(flag > nb)
printf("%-15d\t%-15d\t%-15s\t%-15s\t%-15s\n",i, f[i],"WAIT...","WAIT...","WAIT...");
}
}
Output :

Worst fit code:

#include<stdio.h>
#define max 25

void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];int flag,fragi = 0;

printf("\n\tMemory Management Scheme - Worst Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of Process:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++) {
printf("Block %d:",i);
scanf("%d",&b[i]);
ff[i] = i;
}
printf("Enter the size of the Processes :-\n");

for(i=1;i<=nf;i++) {
printf("Process %d:",i);
scanf("%d",&f[i]);
}
int y,z,temp1;
/*sorting for worst and best fit only*/
for(y=1;y<=nb;y++)
{
for(z=y;z<=nb;z++)
{
if(b[y]<b[z]) /*change < to > for best fit*/
{
temp=b[y];
b[y]=b[z];
b[z]=temp;
temp1=ff[y];
ff[y]=ff[z];
ff[z]=temp1;
}
}
}
int flagn[max];
int fragx = 0;
/*Following is the code of next fit*/
printf("\n\nProcess_No\tProcess_Size\tBlock_No\tBlock_Size\tFragment\n");
for(i=1;i<=nf;i++)
{
flag = 1;
for(j=1;j<=nb;j++)
{
if(f[i] <= b[j]){
flagn[j] = 1;
printf("%-15d\t%-15d\t%-15d\t%-15d\t",i, f[i],ff[j],b[j]);
b[j] = b[j] - f[i];
fragi = fragi + b[j];
printf("%-15d\n",b[j]);
break;
}
else
{flagn[j] = 0;
flag++;
}
}
if(flag > nb)
printf("%-15d\t%-15d\t%-15s\t%-15s\t%-15s\n",i,
f[i],"WAIT...","WAIT...","WAIT...");
}
}
Output
Practical No : 6
Problem Statement : Write a program to simulate page replacement algorithms using
FIFO, LRU and Optimal Algorithm.

Code :

FIFO
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
}
for(k=0;k<no;k++)
printf("%d\t",frame[k]);

printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
Output :

LRU

#include<stdio.h>

int findLRU(int time[], int n){


int i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i){


if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2,
i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i)
{
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i)


{
flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j)


{
if(frames[j] == pages[i])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}

if(flag2 == 0)
{
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}

printf("\n");

for(j = 0; j < no_of_frames; ++j)


{
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);

return 0;
}

Output :

You might also like