Space for google add

SMACO Simulator

 Assignment 2 :- Smaco 


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int pc,lc,address,reg[4];

long int mem[1000],content;

int cc[6]={0,0,0,0,0,1};

char fname[20];

FILE *fp;

void load(char *fname)

{

fp=fopen(fname,"r");

if(fp==NULL)

printf("\nFile error.");

else

{

while(!feof(fp))

{

fscanf(fp,"%d %ld\n",&address,&content);

if(address==-1)

{

pc=content; /*printf("\n pc=%ld",content);*/return;

}

else

{

lc=address;

mem[address]=content;

}

}

}

fclose(fp);

}

void trace()

{

int i,j;

printf("\nRegister Values :\n");

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

{

printf("\nRegister %d : %d",i,reg[i]);

}

printf("\nCondition Code Values :\n");

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

{

printf("\nCondition code  %d : %d",j,cc[j]);

}

}

void print()

{

int i;

for(i=pc;i<=lc;i++)

{

printf("\n%d : %ld\n",i,mem[i]);

}

}

void accept(char *fname)

{

FILE *fp;

fp=fopen(fname,"w");

do

{

printf("\nEnter  address and content : \n");

scanf("%d %ld",&address,&content);

fprintf(fp,"%d %ld\n",address,content);

}

while(address!=-1);

fclose(fp);

}

void run()

{

int opcode,op1,op2,i,j;

long int temp;pc=content;

while(pc)

{

opcode=mem[pc]/10000;

op1=(mem[pc]%10000)/1000-1;

op2=mem[pc]%1000;

               /* printf("\n%ld \n%d %d %d",mem[pc],opcode,op1,op2)*/;

switch(opcode)

{

case 0: pc=-1;

return;

break;

case 1:reg[op1]+=mem[op2];

pc++;

break;

case 2:reg[op1]-=mem[op2];

pc++;

break;

case 3:reg[op1]*=mem[op2];

pc++;

break;

case 4:reg[op1]=mem[op2];

pc++;

break;

case 5:mem[op2]=reg[op1];

pc++;

break;

case 6:

if(reg[op1]<mem[op2])

cc[0]=1;

if(reg[op1]<=mem[op2])

cc[1]=1;

if(reg[op1]>mem[op2])

cc[3]=1;

if(reg[op1]>=mem[op2])

cc[4]=1;

if (reg[op1]==mem[op2])

cc[2]=1;

                                pc++;

break;

case 7:

if(cc[op1]==1)

pc=op2;

                                else 

pc++;

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

cc[i]=0;

break;

case 8:reg[op1]/=mem[op2];

pc++;break;

case 9:

printf("\nEnter input :\n");

scanf("%ld",&mem[op2]);pc++;

break;

case 10:printf("\nResult : %ld \n",mem[op2]);pc++;

break;

default : printf("\nInvalid opcode\n");

break;

}

//if(opcode!=7) pc++;

               //scanf("%d",&i);

          /*printf("\nRegister ");

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

{

printf(" %d",reg[i]);

}

printf("\nCondition Code Values :\n");

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

{

printf("%d ",cc[j]);

}*/

}

}

void main(int argc,char *argv[])

{

int ch;

char fname[25];

if(argc==1)

{

printf("\nFile does not exist.\n");

printf("\nEnter file name :");

scanf("%s",fname);

accept(fname);

}


if(argc==2)

{

strcpy(fname,argv[1]);

do

{

printf("\n1.Load\n2.Print\n3.Run\n4.Trace\n5.Quit\n");

printf("\nEnter Choise :\n");

scanf("%d",&ch);

switch(ch)

{

case 1: load(fname);break;

case 2: print();break;

case 3: run();break;

case 4: trace();break;

case 5: exit(0);

}

}while(ch<6);

}

}





Post a Comment

0 Comments