Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
60 views

Mos CPP

This C++ program implements a simple MOS (machine operating system) that reads instructions and data from a file, stores them in memory, and executes the instructions. It initializes memory and variables, loads instructions and data into memory by reading from an input file, executes the program by looping through instructions and performing the specified operations, and writes output to a file. The main functions are to initialize memory, load data from the file into memory, execute the program by processing instructions, and terminate by writing output and reloading data.

Uploaded by

himanshu dhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Mos CPP

This C++ program implements a simple MOS (machine operating system) that reads instructions and data from a file, stores them in memory, and executes the instructions. It initializes memory and variables, loads instructions and data into memory by reading from an input file, executes the program by looping through instructions and performing the specified operations, and writes output to a file. The main functions are to initialize memory, load data from the file into memory, execute the program by processing instructions, and terminate by writing output and reloading data.

Uploaded by

himanshu dhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

#include<bits/stdc++.

h>

using namespace std;

char M[100][4],IR[4],R[4],C;

FILE *fp1, *fp2;

int IC;

int B,E,SI,READf;

void TERMINATE();

void INIT()

int i,j;

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

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

M[i][j] = '\0';// making all the elements of 2d array as nulle

B = 0;

E = B + 10;

READf = 0;

void READ(int loc)


{

int i,j,k;

char ch,arr1[20];

if (READf == 0)

k = loc-1;

E = loc+10;

ch = fgetc(fp1);

while (1)

i = 0;

while (i < 4 && ch != '\n')

arr1[i] = ch;

i++;

ch = fgetc(fp1);

arr1[i] = '\0';

if (strcmp(arr1,"$END")==0)

while (ch != '\0' && ch != EOF)

ch = fgetc(fp1);

break;

}
else

if (ch == '\n')

k++;

i=0;

while (i < 4)

M[k][i] = arr1[i];

i++;

k = E-1;

E = E+10;

else

k++;

i=0;

while (i < 4)

M[k][i] = arr1[i];

i++;

}
if (ch == '\n')

ch = fgetc(fp1);

READf = 1;

void WRITE()

int i,j,k,loc,x;

char ch2;

IR[3] = 0;

x = atoi(&IR[2]);

loc = x * 10;

i = loc;

while (i <= loc + 9)

for ( k = 0; k < 4; k++)

if (M[i][k] != '\0')

fputc(M[i][k], fp2);
ch2 = M[i][k];

else

break;

i++;

if (ch2 != '\n')

fputc('\n',fp2);

void MOS(int loc)

switch (SI)

case 1:

READ(loc);

SI=0;

break;

case 2:

WRITE();

SI = 0;

break;

case 3:

TERMINATE();
SI = 0;

break;

default:

break;

void EXECUTEPROGRAM()

int k,i,j,loc,x,y;

while (IC<B)

for ( k = 0; k < 4; k++)

IR[k] = M[IC][k];

x = atoi(&IR[2]);

loc = x;

if (IR[0] == 'L' && IR[1] == 'R')

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

R[i] = '\0';

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


{

if (M[loc][j] != '\0' && M[loc][j] != '\n')

R[j] = M[loc][j];

else

break;

else if (IR[0] == 'S' && IR[1] == 'R')

for ( j = 0; j < 4 && R[j] != '\0'; j++)

M[loc][j] = R[j];

else if (IR[0] == 'C' && IR[1] == 'R')

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

if (R[i] == M[loc][i])

C = 't';
}

else

C = 'f';

break;

else if (IR[0] == 'B' && IR[1] == 'T')

if (E == 't')

IC = loc;

IC--;

else if (IR[0] == 'G' && IR[1] == 'D')

SI = 1;

MOS(loc);

else if (IR[0] == 'P' && IR[1] == 'D')

SI = 2;

MOS(loc);
}

else if (IR[0] == 'H')

SI = 3;

MOS(loc);

IC++;

void STARTEXECUTION()

IC = 0;

EXECUTEPROGRAM();

void LOAD()

int i,j,k;

char ch, temp[4];

// ch = fgetc(fp1);

// int ch = fgetc(fp1);
INIT();//INIT function is called that is made ABOVE

do

i = 0;

while (i<4 && ch != '\n')

temp[i] = ch;

ch = fgetc(fp1);

ch = ch;

i++;

temp[i] = '\0';

//cout<<temp;

if((strcmp(temp,"$AMJ"))==0)

while (ch != '\n')

ch = getc(fp1);

else if ((strcmp(temp,"$DTA"))==0)

{
STARTEXECUTION();

else if ((strcmp(temp,"$DTA"))!=0)

i=0;

if (B == E)

{E = E+10;}

for ( k = 0; k < 4; k++)

M[B][k] = temp[i];

i++;

B++;

if ((char)ch == '\n')

ch = fgetc(fp1);

} while (ch != EOF);

void TERMINATE()
{

fputc('\n', fp2);

fputc('\n', fp2);

LOAD();

int main()

int i,j;

fp1 = fopen("inputMOS.txt","r");

fp2 = fopen("OUTPUTMOS.txt","w");

if (fp1 == NULL)

cout<<"File not found";

else

LOAD();

cout<<" press any button to continue"<<endl;

return 0 ;

}#include<bits/stdc++.h>

using namespace std;


char M[100][4],IR[4],R[4],C;

FILE *fp1, *fp2;

int IC;

int B,E,SI,READf;

void TERMINATE();

void INIT()

int i,j;

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

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

M[i][j] = '\0';// making all the elements of 2d array as nulle

B = 0;

E = B + 10;

READf = 0;

void READ(int loc)

{
int i,j,k;

char ch,arr1[20];

if (READf == 0)

k = loc-1;

E = loc+10;

ch = fgetc(fp1);

while (1)

i = 0;

while (i < 4 && ch != '\n')

arr1[i] = ch;

i++;

ch = fgetc(fp1);

arr1[i] = '\0';

if (strcmp(arr1,"$END")==0)

while (ch != '\0' && ch != EOF)

ch = fgetc(fp1);

break;

else

{
if (ch == '\n')

k++;

i=0;

while (i < 4)

M[k][i] = arr1[i];

i++;

k = E-1;

E = E+10;

else

k++;

i=0;

while (i < 4)

M[k][i] = arr1[i];

i++;

if (ch == '\n')

ch = fgetc(fp1);
}

READf = 1;

void WRITE()

int i,j,k,loc,x;

char ch2;

IR[3] = 0;

x = atoi(&IR[2]);

loc = x * 10;

i = loc;

while (i <= loc + 9)

for ( k = 0; k < 4; k++)

if (M[i][k] != '\0')

fputc(M[i][k], fp2);

ch2 = M[i][k];

}
else

break;

i++;

if (ch2 != '\n')

fputc('\n',fp2);

void MOS(int loc)

switch (SI)

case 1:

READ(loc);

SI=0;

break;

case 2:

WRITE();

SI = 0;

break;

case 3:

TERMINATE();

SI = 0;

break;
default:

break;

void EXECUTEPROGRAM()

int k,i,j,loc,x,y;

while (IC<B)

for ( k = 0; k < 4; k++)

IR[k] = M[IC][k];

x = atoi(&IR[2]);

loc = x;

if (IR[0] == 'L' && IR[1] == 'R')

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

R[i] = '\0';

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

if (M[loc][j] != '\0' && M[loc][j] != '\n')


{

R[j] = M[loc][j];

else

break;

else if (IR[0] == 'S' && IR[1] == 'R')

for ( j = 0; j < 4 && R[j] != '\0'; j++)

M[loc][j] = R[j];

else if (IR[0] == 'C' && IR[1] == 'R')

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

if (R[i] == M[loc][i])

C = 't';

else
{

C = 'f';

break;

else if (IR[0] == 'B' && IR[1] == 'T')

if (E == 't')

IC = loc;

IC--;

else if (IR[0] == 'G' && IR[1] == 'D')

SI = 1;

MOS(loc);

else if (IR[0] == 'P' && IR[1] == 'D')

SI = 2;

MOS(loc);

else if (IR[0] == 'H')


{

SI = 3;

MOS(loc);

IC++;

void STARTEXECUTION()

IC = 0;

EXECUTEPROGRAM();

void LOAD()

int i,j,k;

char ch, temp[4];

// ch = fgetc(fp1);

// int ch = fgetc(fp1);

INIT();//INIT function is called that is made ABOVE


do

i = 0;

while (i<4 && ch != '\n')

temp[i] = ch;

ch = fgetc(fp1);

ch = ch;

i++;

temp[i] = '\0';

//cout<<temp;

if((strcmp(temp,"$AMJ"))==0)

while (ch != '\n')

ch = getc(fp1);

else if ((strcmp(temp,"$DTA"))==0)

STARTEXECUTION();

}
else if ((strcmp(temp,"$DTA"))!=0)

i=0;

if (B == E)

{E = E+10;}

for ( k = 0; k < 4; k++)

M[B][k] = temp[i];

i++;

B++;

if ((char)ch == '\n')

ch = fgetc(fp1);

} while (ch != EOF);

void TERMINATE()

{
fputc('\n', fp2);

fputc('\n', fp2);

LOAD();

int main()

int i,j;

fp1 = fopen("inputMOS.txt","r");

fp2 = fopen("OUTPUTMOS.txt","w");

if (fp1 == NULL)

cout<<"File not found";

else

LOAD();

cout<<" press any button to continue"<<endl;

return 0 ;

You might also like