Tuesday, 8 November 2011

Revolution...

Hey visitors...
Please visit my new Blog...
Actually it is Wordpress...
http://prashantkulkarni17.wordpress.com/
Thank You... :D :)

Wednesday, 25 May 2011

Difference Equation...


/******************************************************************************
 Find Output of a System described by given difference equation & initial conditions for given I/P sqeuence.(Solution of difference equation).Obtain Response for different systems by changing degree of difference equation(N) & coefficients & also for difference input sequence x(n).Observe the Response by considering System as FIR & IIR system.
******************************************************************************/
#include<iostream.h>
#include<conio.h>
/******************************************************************************/
void main()
{
int a[100],b[100],x[100],y[100],i,j,k,l,m,n,M,N,ein,einit,es;
clrscr();
for(i=0;i<=100;i++)
a[i]=0;
     F: ein=einit=es=0;
cout<<"\n How Many Terms of Y (M+1)? ==> ";
cin>>M;
cout<<"\n How Many Terms of X (N+1)? ==> ";
cin>>N;


cout<<"\n Enter Coefficients Terms of X ==>  ";
for(i=0;i<N;i++)
cin>>a[i];
    D: if(ein==1)
{
cout<<"\n How Many Terms of X ? ==> ";
cin>>N;
ein=0;
}
cout<<"\n Enter x(N) ==>  ";
for(i=0;i<N;i++)
cin>>x[i];
if(M>1)
goto B;
cout<<"\n Difference Equation is ==> { ";
for(i=0;i<100;i++)
{
y[i]=0;
for(j=0;j<N;j++)
if(i-j>=0 && i-j<N)
y[i]=y[i]+a[j]*x[i-j];
cout<<" "<<y[i];
}
cout<<" }";
goto A;


    B:  if(ein==1)
goto G;


cout<<"\n Enter Coefficients Terms of Y ==>  ";
for(i=0;i<M;i++)
cin>>b[i];
    E: for(i=(-(M-1));i<0;i++)
{
cout<<"\n Enter Initial Condition for y["<<i<<"] = ";
cin>>y[i]; // past condn forced to -1 ...
}
    G: cout<<"\n Difference Equation is ==> { ";
for(i=0;i<100;i++)
{
y[i]=0;
for(j=0;j<M;j++)
y[i]=y[i]-(b[j]*y[i-j]);
for(j=0;j<N;j++)
if(i-j>=0 && i-j<N)
y[i]=y[i]+(a[j]*x[i-j]);
cout<<" "<<y[i];


}
cout<<" }";


     A: cout<<"\n Do You Want To Change Input Condition ? [1/0]==> ";
cin>>ein;
if(ein==1)
goto D;
cout<<"\n Do You Want To Change Initial Condition ? [1/0]==> ";
cin>>einit;
if(einit==1)
goto E;
cout<<"\n Do You Want To Change Whole System ? [1/0]==> ";
cin>>es;
if(es==1)
goto F;
     getch();
}
/******************** Output *******************/


/* How Many Terms of Y (M+1)? ==> 2
                                                                                
 How Many Terms of X (N+1)? ==> 3                                               
                                                                                
 Enter Coefficients Terms of X ==>  1 0 0                                       
                                                                                
 Enter x(N) ==>  1 2 3                                                          
                                                                                
 Enter Coefficients Terms of Y ==>  1 -1                                        
                                                                                
 Enter Initial Condition for y[-1] = 1                                          
                                                                                
 Difference Equation is ==> {  2 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 }        
 Do You Want To Change Input Condition ? [1/0]==> 0                             
                                                                                
 Do You Want To Change Initial Condition ? [1/0]==> 0                           


 Do You Want To Change Whole System ? [1/0]==> 0 
*/

Linear Convolution Problem...


Prog :- Find Output of a given System for given input sequence using Linear Convolution.
/**************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
/**************************************************************************/
class list
{
    public:
int index,value,length;
list *next;
list()
{
value=0;
}
list *accepthn(list *w);
list *acceptxn(list *w);
list *convol(list *w,list *v);
};
list *p,*q,*yy,*qq,*r,*head,*head1,*head2;
list *f,*h,*x,*head3;
int i,j,k,l,m,e,xe,he;
int y[201]={0};
int xlen,hlen,max,size,xlo,xup,hlo,hup;
/**************************************************************************/
list *list::accepthn(list *w)
{
   i=0;
   head=q=w;
   do
   {
       p=new list;
       cout<<"\n Enter index & Element ==> ";
       cin>>p->index>>p->value;
       p->next=NULL;
       if(head==NULL)
       {
 head=p;
 q=head;
       }
       else
       {
 q->next=p;
 q=p;
       }
       cout<<"\n Continue ? [1/0]==> ";
       cin>>e;
       i++;
       q->length++;
   }while(e==1);
   return(head);
}
/**************************************************************************/
list *list::convol(list *x,list *h)
{
   cout<<"\n \n Impulse Response Of Given System is ==>  \n{ ";
   for(i=(xlo+hlo);i<=(xup+hup);i++)
  y[i]=0;
   for(i=(xlo+hlo);i<=(xup+hup);i++)
   {
y[i]=0;
head1=x;
head2=h;
for(head2=h;head2!=NULL;head2=head2->next)
{
   for(head1=x;head1!=NULL;head1=head1->next)
   {
if(head1->index==i-head2->index)
    y[i]=y[i]+(head1->value*head2->value);
   }
}
cout<<y[i]<<" ";
   }
   cout<<" }";
}
/**************************************************************************/
void main()
{
   clrscr();
B: xlo=hlo=100; //Accepting System Input h(n)
   xup=hup=-100;
   cout<<"\n System h(n) ==>  ";
   h=r->accepthn(NULL);
   cout<<"\n h(n) ==> { ";
   head1=h;
   do
   {
  if(head1->index==0)
    cout<<" @";
cout<<" -> "<<head1->value;
if(head1->index>hup)
  hup=head1->index;
if(head1->index<hlo)
  hlo=head1->index;
head1=head1->next;
   }while(head1!=NULL);
   cout<<" }";
A: xlo=100;        //Accepting Input x(n)
   xup=-100;
   cout<<"\n Input x(n) ==>  ";
   x=r->accepthn(NULL);
   cout<<"\n h(n) ==> { ";
   head1=x;
   do
   {
    if(head1->index==0)
  cout<<" @";
cout<<" -> "<<head1->value;
if(xup<head1->index)
  xup=head1->index;
if(xlo>head1->index)
  xlo=head1->index;
head1=head1->next;
}while(head1!=NULL);
yy=r->convol(x,h);
cout<<"\n Do You Want to change input x(n) [1/0] => ";
cin>>xe;
if(xe==1)
  goto A;
cout<<"\n Do You Want to change System Imput h(n) [1/0] => ";
cin>>he;
if(he==1)
  goto B;
getch();
}
/********************************** Output **********************************/
 System h(n) ==>                                                                
 Enter index & Element ==> 0 1                                                  
                                                                                
 Continue ? [1/0]==> 1                                                          
                                                                                
 Enter index & Element ==> -1 1                                                 
                                                                                
 Continue ? [1/0]==> 1                                                          
                                                                                
 Enter index & Element ==> 1 1                                                  
                                                                                
 Continue ? [1/0]==> 0                                                          
                                                                                
 h(n) ==> {  @ -> 1 -> 1 -> 1 }                                                 
 Input x(n) ==>                                                                 
 Enter index & Element ==> -99 2                                                
                                                                                
 Continue ? [1/0]==> 1                                                          
                                                                                
 Enter index & Element ==> 99 3                                                 
                                                                                
 Continue ? [1/0]==> 0                                                          
                                                                                
 h(n) ==> {  -> 2 -> 3                                                          
                                                                                
 Impulse Response Of Given System is ==>                                        
{ 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3  }          
 Do You Want to change input x(n) [1/0] => 0                                    
                                                                                
 Do You Want to change System Imput h(n) [1/0] => 0                             
*/

Thursday, 19 May 2011

Packages In Java...


This is a general program explaining how to make use of packages in JAVA...
/****************************** pk.java *****************************/
                        // creating package 
package pack;
public class pk
{
public void show()
{
System.out.println("You are in Package now........");
}
}
/*
(1) In Command Prompt Compile pk.java
C:\jdk1.5.0_12\bin>javac pk.java
(2) It makes class file as pk.class in C:\jdk1.5.0_12\bin\ folder..
(3) Now Make a folder named pack in this directory..
(4) Copy & paste this pk.class file in pack folder.
*/
/****************************** pkg.java *****************************/
                   // importing file from package
import pack.pk;
class pkg
{
public static void main(String[] args) 
{
pk p=new pk();
System.out.println("Hi..........");
p.show();
}
}
/***************************** Output **********************************
C:\jdk1.5.0_12\bin>javac pk.java


C:\jdk1.5.0_12\bin>javac pkg.java


C:\jdk1.5.0_12\bin>java pkg
Hi..........
You are in Package now........
*/

Tuesday, 10 May 2011

Dining Philosopher's Algorithm...


/*********************************************************************************
By  => Ajit Medhekar... Xtream Kings...  K.K.Wagh COE,Nashik...
            Ajit's Blog :- http://ammedhekar.blogspot.com
*********************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
//status:1=eating,status:0:thinking
class semaphore
{
   public:
   int s;
   semaphore()
   {
      s=1;
   }
   int wait()
   {
      int s1;
      s1=s;
      s1--;

      if(s1<0)
      {
cout<<"\nblock";
return(1);
      }
      else
      {
s--;
cout<<"\nWait:"<<s;
return(0);
      }
   }
   void signal()
   {  int s1;
      s1=s;
      s1++;

      if(s1<=0)
      {
cout<<"\nblocked philosophers can eat now";
      }
      else
      {
s++;
cout<<"\nsignal:"<<s;
      }
   }
};


void main()
{
   int ch,phil,status[10],i,block1,block2;
   semaphore chopstik[5];
   clrscr();
   for(i=1;i<=5;i++)
   {
      status[i]=0;        //thinking
   }
   do
   {
      cout<<"\n1.eat\n2.think\n3.status\n4.Allowed philosophers for eating\n5.exit";
      cout<<"\nEnter choice:";
      cin>>ch;
      switch(ch)
      {
case 1:
cout<<"\nEnter the philosopher who wants to eat:";
cin>>phil;
if(status[phil]==1)
cout<<"\nAlready eating ;)";
else
{
block1=chopstik[phil].wait();
block2=chopstik[(phil+1)%5].wait();
status[phil]=1;
if(block1==1 || block2==1)
{
  if(block1==0)
  {
     chopstik[phil].signal();
  }
  else if(block2==0)
  {
     chopstik[(phil+1)%5].signal();
  }
  status[phil]=0;

}
}
break;
case 2:
cout<<"\nEnter the philosopher who wants to think:";
cin>>phil;
if(status[phil]==0)
cout<<"\nAlready thinking ;)";
else
{
chopstik[phil].signal();
chopstik[(phil+1)%5].signal();
status[phil]=0;
}
break;
case 3:
for(i=0;i<5;i++)
{
  cout<<"\nPhilosopher "<<i+1<<":";
  if(status[i+1]==1)
  {
    cout<<"eating";
  }
  else
  {
     cout<<"thinking";
  }
}
break;
case 4:
      for(i=1;i<=5;i++)
      {
 if(chopstik[i].s==1 && chopstik[((i+1)%5)].s==1)
 {
    cout<<"\nPhilosopher "<<i<<" is allowd to eat";
 }
      }
      break;
case 5:
exit(0);
      }
   }while(ch<=5);
   getch();
}

Producer-Consumer Algorithm...


/*********************************************************************************
By => Ajinkya Mahajan... Xtream Kings... K.K.Wagh COE,Nashik...
           Ajinkya's Blog :- http://aju-feelitsayit.blogspot.com/
*********************************************************************************/
import java.io.DataInputStream;

class p extends Thread
{
public static int pcnt=0;
public void run()
{
if(PC.n<PC.np)
{
System.out.println("Producer Producing...");
PC.n++;
System.out.println("In Stock : "+PC.n);
pcnt++;
}
else
{
System.out.println("Stock Full!!!");
}
}
}
class c extends Thread
{
public static int ccnt=0;
    public void run()
{

if(PC.n>0)
{
System.out.println("Consumer Consuming...");
PC.n--;
System.out.println("In Stock : "+PC.n);
ccnt++;
}
else
{
System.out.println("Stock Empty!!!");
}
}
}
public class PC
{
public static int n=0,np,i;
public static void main(String[] args)
{
DataInputStream in=new DataInputStream(System.in);

try
{
System.out.println("Enter The Maximum Number Of Production Allowed : ");
np=Integer.parseInt(in.readLine());
System.out.println("NP : "+np);

p p1=new p();
c c1=new c();
p1.start();
c1.start();

for(i=0;;i++)
{
int n=(int) (Math.random()*50);
if(n<25)
{
p1.run();
}
else
{
c1.run();
}
if(p.pcnt==np)
                          break;
}
System.out.println("Total Production : "+p.pcnt);
System.out.println("Total Consumption : "+c.ccnt);
System.out.println("Total Transactions taken place(including stok empty): "+i);
}
catch(Exception e){}
}
}

Reader Writer Algorithm...


/*******************************************************************************
By => Anand Mahuli...  Xtream Kings... K.K.Wagh COE,Nashik...
           Anand's Blog :- http://designyourdreamshere.blogspot.com/
*******************************************************************************/
class ww extends Thread{
    ww()
    {
    start();
    }
public void run()
    {
if(RW1.sem!=3)
        {
        System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Waiting...");  
            try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        }
        else
        {
        RW1.sem=2;
        System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Writing...");
            try{sleep((int)(Math.random()*10000));}catch(Exception e){}
            RW1.sem=3;
            System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Done Writing...");
        }
    }
}

class rr extends Thread{
    rr()
    {
    start();
    }
public void run()
    {
if(RW1.sem==2)
        {
System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Waiting...");            
            try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        }
        else
        {
        RW1.sem=1;
        System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Reading...");
        try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        RW1.sem=3;
        System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Done Reading...");
        }
    }
}
public class RW1 extends Thread
{
    public static int sem=3,i;
   
    public static void main(String args[])
    {
    int j;
        rr threadr[] = new rr[5];
        ww threadw[] = new ww[5];
     
        for(i=0;i<3;i++)
        {
        System.out.println(i);
        try {sleep(1000);} catch(Exception e) {}
        j=(int)(Math.random()*3);
        //this is the key area in this program...i used  a random switch so as two create random no of reader write at a paritcular moment
        switch(j)
        {
        case 0:
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+"(2)) Thread Created : "+threadw[i].getId());
       
        break;
        case 1:
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+"(2)) Thread Created : "+threadr[i].getId());
       
        break;
        case 2:
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        break;
        case 3:
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        break;
  }
        }
        System.out.println("Exit From Main Thread ");
}
}

/*
 0
Writer(i=0) Thread Created : 8
Writer(i=0(2)) Thread Created : 9
1
Writer(i=0) (Thread : 8) Writing...
Writer(i=0) (Thread : 9) Waiting...
Reader(i=1) Thread Created : 10
Reader(i=1(2)) Thread Created : 11
2
Reader(i=0) (Thread : 10) Waiting...
Reader(i=0) (Thread : 11) Waiting...
Writer(i=0) (Thread : 8) Done Writing...
Writer(i=2) Thread Created : 12
Reader(i=2) Thread Created : 13
Exit From Main Thread
Writer(i=0) (Thread : 12) Writing...
Reader(i=0) (Thread : 13) Waiting...
Writer(i=0) (Thread : 12) Done Writing...

 */

Pass-1 Of Two Pass MACRO Processor...

/*********************************************************************************
By  => Ajit Medhekar... Xtream Kings...  K.K.Wagh COE,Nashik...
            Ajit's Blog :- http://ammedhekar.blogspot.com
*********************************************************************************
Design suitable data structure & Implement the pass-I of two pass MACRO processor...                                                                              
*********************************************************************************/
# include <stdio.h>
# include<conio.h>
#include<string.h>

struct table1
{
 char name[10];
 int index,add_mdt;
}MNT[5];
struct table2
{
 char des[30];
 int add;
}MDT[30];
struct table3
{
 char param[10],no[3];

}ALA[20];

/*
struct table1 MNT[5];
struct table2 MDT[30];
struct table3 ALA[20];
*/
FILE *fp1,*fp2;
char ch,word[20],word1[20],*mdtdes;
int newline=0;
void get_word();
char* num[10]={"#1","#2","#3","#4","#5","#6","#7","#8","#9"};

fpos_t pos;
void main()
{
 int i,j,mnt=0,mdt=0,ala=0,k,t=0,com=1,flag,flag1=0;
 char line[30],line1[30],line2[30];
 clrscr();

 fp1=fopen("c:\\a.asm","r");
 fp2=fopen("c:\\b.txt","w");
 i=0;
 while(!feof(fp1))
 {
  fgetpos(fp1,&pos);
  get_word();
  if(strcmp(word,"MACRO")==0)
  {
   fgetpos(fp1,&pos);
   get_word();

   strcpy(MNT[mnt].name,word);
   MNT[mnt].index=mnt;
   MNT[mnt].add_mdt=mdt;
   mnt++;

   fsetpos(fp1,&pos);
   i=0;
   while(!feof(fp1))
   {
    ch=fgetc(fp1);
    if(ch=='\n')
     goto l9;
    line1[i]=ch;
    i++;
   }
       l9:line1[i]=NULL;
   strcpy(MDT[mdt].des,line1);
   MDT[mdt].add=mdt;
   mdt++;

   fsetpos(fp1,&pos); //find actual parameter
   get_word();
   do
   {
    get_word();
    t=0;
    if(strstr(word,"=")!=0)
    {
        while(word[t]!='=')
        {
     word1[t]=word[t];
     t++;
        }
    }
    else
     strcpy(word1,word);

    strcpy(ALA[ala].param,word1);
    strcpy(ALA[ala].no,num[ala]);
    ala++;

   }while(ch!='\n');
   }
   else if(strcmp(word,"MEND")==0)
   {
   fsetpos(fp1,&pos);
   get_word();
   strcpy(MDT[mdt].des,word);
   MDT[mdt].add=mdt;
   mdt++;
   }
   else if(strcmp(word,"START")==0)
   {
   fsetpos(fp1,&pos);
   while(!feof(fp1))
   {
    ch=fgetc(fp1);
    fprintf(fp2,"%c",ch);
   }
   break;
   }
   else
   {
//code in comment to display macro call as INCR &A,&B,&REG2
//otherwise macro call is like INCR #4,#5,#6
//
/*   fsetpos(fp1,&pos);
   get_word();

   for(i=0;i<mnt;i++) //check nested call of macro
   {
    if(strcmp(word,MNT[i].name)==0)
    {
     fsetpos(fp1,&pos);
     fgets(line2,30,fp1);
     flag1=1;
    }
   }

   if(flag1==1)
   {
    flag1=0;
    strcpy(MDT[mdt].des,line2);
    MDT[mdt].add=mdt;
    mdt++;
   }
   else
*/   {
    fsetpos(fp1,&pos);
    get_word();

    strcpy(line2,word);
    com=1;
    do
    {
     if(ch=='\n')
      goto l1;
     get_word();
     strcat(line2," ");
     flag=0;
     for(i=0;i<ala;i++)
     {
      if(strcmp(word,ALA[i].param)==0)
      {
       strcpy(word1,ALA[i].no);
       strcat(line2,word1);
       flag=1;
       break;
      }
     }
     if(flag==0)
      strcat(line2,word);
     if(com==1)
     {
      strcat(line2,",");
      com=0;
     }
    }while(ch!='\n');
        l1:strcpy(MDT[mdt].des,line2);
    MDT[mdt].add=mdt;
    mdt++;
    }
   }
 }
 fclose(fp1);
 fclose(fp2);

 printf("\nMacro Name Table (MNT) :");
 printf("\n\nIndex\tName\tAddress In MDT");
 printf("\n-------------------------------");
 for(j=0;j<mnt;j++)
  printf("\n%d\t%s\t%d",MNT[j].index,MNT[j].name,MNT[j].add_mdt);

 printf("\n\nArgument Array List (ALA) :");
 printf("\n\nNo\tParam");
 printf("\n-----------------");
 for(j=0;j<ala;j++)
  printf("\n%s\t%s",ALA[j].no,ALA[j].param);

 printf("\n\nMacro Definition Table (MDT) :");
 printf("\n\nAdd\tDef");
 printf("\n-----------------");
 for(j=0;j<mdt;j++)
  printf("\n%d\t%s",MDT[j].add,MDT[j].des);

   getch();
}


void get_word()
{
 int i=0;
 do
 {
  ch=getc(fp1);
  if(ch!=' ' && ch!='\n' && ch!=',')
  {
   word[i]=ch;
   i++;
  }
  if(ch==' ' || ch=='\n' || ch==',')
  {
   word[i]=NULL;
   i=0;
   break;
  }
  }while(!feof(fp1));
}

/*********  OUTPUT ***********

Macro Name Table (MNT) :

Index   Name    Address In MDT
-------------------------------
0       INCR    0
1       DECR    5

Argument Array List (ALA) :

No      Param
-----------------
#1      &X
#2      &Y
#3      &REG1
#4      &A
#5      &B
#6      &REG2
                                                                             
Macro Definition Table (MDT) :                                                
                                                                             
Add     Def                                                                  
-----------------                                                            
0       INCR &X,&Y,&REG1=AREG                                                
1       MOVER #3, #1                                                          
2       ADD #3, #2                                                            
3       MOVEM #3, #1                                                          
4       MEND                                                                  
5       DECR &A,&B,&REG2=BREG                                                
6       MOVER #6, #4                                                          
7       SUB #6, #5                                                            
8       MOVEM #6, #4                                                          
9       INCR #4,#5,#6
10      MEND

*/
/*********** a.asm ******************/
MACRO INCR &X,&Y,&REG1=AREG
MOVER &REG1,&X
ADD &REG1,&Y
MOVEM &REG1,&X
MEND
MACRO DECR &A,&B,&REG2=BREG
MOVER &REG2,&A
SUB &REG2,&B
MOVEM &REG2,&A
INCR &A,&B,&REG2
MEND
START 100
READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END
/*************** b.txt **************/
START 100READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END

Monday, 9 May 2011

Pass-1 of Two Pass Assembler... (My Best Ever C++ Program)


/**********************************************/
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
/**********************************************/
int i,j,k,cnt,l,kahihi,add,flag,regular,addr,litno;
FILE *pk;
int flag1,rest,symcnt,litaddress,purvichakahihi,consno;
int poolind[30],pool,poolpresence[30],sadhalen,attempt;
char p,str[200][30],temp[2],a[5],sadha[10],s1[10],s2[3],q;
//char typ[3],val[3],address[5],op[3],clas[3],mnemonic[7];
int endadd,poi,ptr,s1add;
// endadd is var for showing address of END mnemonic...
// litaddress is used for lit address determination...
// purvichakahihi is the variable used for getting last correct position of kahihi...
/*********************************************/
class mn
{
public:
char mnemonic[200][7],clas[200][3],op[200][3],address[200][5];
char typ[200][3],val[200][3];
int count[200];
mn()
{
for(kahihi=0;kahihi<200;kahihi++)
{
strcpy(mnemonic[i],NULL);
strcpy(clas[i],NULL);
strcpy(op[i],NULL);
strcpy(address[i],NULL);
strcpy(typ[i],NULL);
strcpy(val[i],NULL);
count[kahihi]=0;
}
}
};
mn MOT,resultMOT,resultLIT,restpartlit,resultSYM,firstofline;
void declare()
{
// Declareation of all mnemonics.....

strcpy(MOT.mnemonic[0],"STOP");
strcpy(MOT.mnemonic[1],"ADD");
strcpy(MOT.mnemonic[2],"SUB");
strcpy(MOT.mnemonic[3],"MULT");
strcpy(MOT.mnemonic[4],"MOVER");
strcpy(MOT.mnemonic[5],"MOVEM");
strcpy(MOT.mnemonic[6],"COMP");
strcpy(MOT.mnemonic[7],"BC");
strcpy(MOT.mnemonic[8],"DIV");
strcpy(MOT.mnemonic[9],"READ");
strcpy(MOT.mnemonic[10],"PRINT");
strcpy(MOT.mnemonic[11],"START");
strcpy(MOT.mnemonic[12],"END");
strcpy(MOT.mnemonic[13],"ORIGIN");
strcpy(MOT.mnemonic[14],"EQU");
strcpy(MOT.mnemonic[15],"LTORG");
strcpy(MOT.mnemonic[16],"DS");
strcpy(MOT.mnemonic[17],"DC");
strcpy(MOT.mnemonic[18],"AREG");
strcpy(MOT.mnemonic[19],"BREG");
strcpy(MOT.mnemonic[20],"CREG");
strcpy(MOT.mnemonic[21],"EQ");
strcpy(MOT.mnemonic[22],"LT");
strcpy(MOT.mnemonic[23],"GT");
strcpy(MOT.mnemonic[24],"LE");
strcpy(MOT.mnemonic[25],"GE");
strcpy(MOT.mnemonic[26],"NE");
strcpy(MOT.mnemonic[27],"ANY");

// declaration of all classes of mnemonics....

strcpy(MOT.clas[0],"IS");
strcpy(MOT.clas[1],"IS");
strcpy(MOT.clas[2],"IS");
strcpy(MOT.clas[3],"IS");
strcpy(MOT.clas[4],"IS");
strcpy(MOT.clas[5],"IS");
strcpy(MOT.clas[6],"IS");
strcpy(MOT.clas[7],"IS");
strcpy(MOT.clas[8],"IS");
strcpy(MOT.clas[9],"IS");

strcpy(MOT.clas[10],"IS");
strcpy(MOT.clas[11],"AD");
strcpy(MOT.clas[12],"AD");
strcpy(MOT.clas[13],"AD");
strcpy(MOT.clas[14],"AD");
strcpy(MOT.clas[15],"AD");
strcpy(MOT.clas[16],"DL");
strcpy(MOT.clas[17],"DL");
strcpy(MOT.clas[18],"RG");
strcpy(MOT.clas[19],"RG");
strcpy(MOT.clas[20],"RG");
strcpy(MOT.clas[21],"CC");
strcpy(MOT.clas[22],"CC");
strcpy(MOT.clas[23],"CC");
strcpy(MOT.clas[24],"CC");
strcpy(MOT.clas[25],"CC");
strcpy(MOT.clas[26],"CC");
strcpy(MOT.clas[27],"CC");

// declaration of all opcodes of mnemonics....

strcpy(MOT.op[0],"00");
strcpy(MOT.op[1],"01");
strcpy(MOT.op[2],"02");
strcpy(MOT.op[3],"03");
strcpy(MOT.op[4],"04");
strcpy(MOT.op[5],"05");
strcpy(MOT.op[6],"06");
strcpy(MOT.op[7],"07");
strcpy(MOT.op[8],"08");
strcpy(MOT.op[9],"09");
strcpy(MOT.op[10],"10");
strcpy(MOT.op[11],"01");
strcpy(MOT.op[12],"02");
strcpy(MOT.op[13],"03");
strcpy(MOT.op[14],"04");
strcpy(MOT.op[15],"05");
strcpy(MOT.op[16],"01");
strcpy(MOT.op[17],"02");
strcpy(MOT.op[18],"01");
strcpy(MOT.op[19],"02");
strcpy(MOT.op[20],"03");
strcpy(MOT.op[21],"01");
strcpy(MOT.op[22],"02");
strcpy(MOT.op[23],"03");
strcpy(MOT.op[24],"04");
strcpy(MOT.op[25],"05");
strcpy(MOT.op[26],"06");
strcpy(MOT.op[27],"07");
}
//MOT.mnemonic;
/*********************************************/
void findwordtype()
{
for(i=0;i<29;i++)
{
if(strcmp(str[j-1],MOT.mnemonic[i])==0)
{
// function is for getting starting address of prg...
if(flag==1)
{
add=atoi(str[j-2]);
flag=2;
}
if(strcmp(str[j-1],MOT.mnemonic[11])==0 && flag!=1)
flag=1;

// compare both strings & if matches then make reslut MOT
strcpy(resultMOT.mnemonic[k],MOT.mnemonic[i]);
strcpy(resultMOT.clas[k],MOT.clas[i]);
strcpy(resultMOT.op[k],MOT.op[i]);
strcpy(restpartlit.clas[rest],MOT.clas[i]);
strcpy(restpartlit.op[rest],MOT.op[i]);
resultMOT.count[k]=k;
addr=add+cnt;
itoa(addr,MOT.address[i],10);
strcpy(resultMOT.address[k],MOT.address[i]);
k++;
flag1=1;// used for checking lit
goto baher;
}
flag1=0;
}
baher:
addr=add+cnt;
itoa(addr,a,10);
if(q=='\n' && p==' ')
{
strcpy(firstofline.mnemonic[rest],str[j-1]);
firstofline.count[rest]=rest;
strcpy(firstofline.address[rest],a);
}
strcpy(restpartlit.mnemonic[rest],str[j-1]);
restpartlit.count[rest]=rest;
strcpy(restpartlit.address[rest],a);
rest++;
}
/*********************************************/
void getwordsfromfile()
{
pk=fopen("c:\\fil.txt","r");
j=0;
flag1=0;
q='\0';
for(i=0;i<200;i++)
strcpy(str[i],NULL);

while(!feof(pk))
{
regular=j;
p=fgetc(pk);
if(p!=' ' || p!='\n')
{
if(p!=',' && p!=' ' && p!='\n')
{
temp[0]=p;
temp[1]='\0';
strcat(str[j],temp);
}
}
//checking word.....
// increment wor number....
if(p==' ' || p=='\n' || p==',')
j++;

if(regular!=j)
findwordtype();
if(p=='\n')
cnt=cnt+1;
if(p==' ' || p=='\n' || p==',')
q=p;
}
}
/**********************************************/
void resultofMOT()
{
cout<<"\nMOT Table is ==> ";
cout<<"\n\n\tMN\tCLASS\tOPCODE\tADDRESS";
for(i=0;i<k;i++)
cout<<"\n\n\t"<<resultMOT.count[i]<<"\t"<<resultMOT.mnemonic[i]<<".\t"<<resultMOT.clas[i]<<"\t"<<resultMOT.op[i]<<"\t"<<resultMOT.address[i];
getch();
clrscr();
cout<<"\nMOT Table is ==> ";
cout<<"\n\n\tMN\tCLASS\tOPCODE\tADDRESS";
for(i=0;i<rest;i++)
cout<<"\n\t"<<resultMOT.count[i]<<"\t"<<firstofline.mnemonic[i]<<".\t"<<firstofline.address[i];
}
/**********************************************/
void findliteral()
{
for(i=0;i<rest;i++)
{
if(strcmp(restpartlit.mnemonic[i],"LTORG")==0 || strcmp(restpartlit.mnemonic[i],"END")==0)
{
poolind[pool]=litno;
if(strcmp(restpartlit.mnemonic[i],"END")==0)
litaddress=atoi(restpartlit.address[i])+1;
else
litaddress=atoi(restpartlit.address[i]);
for(kahihi=purvichakahihi;kahihi<rest;kahihi++)
{
if(restpartlit.mnemonic[kahihi][0]=='=')
{
strcpy(resultLIT.mnemonic[litno],restpartlit.mnemonic[kahihi]);
itoa(litaddress,restpartlit.address[kahihi],10);
strcpy(restpartlit.clas[kahihi],"L");
itoa(litno,restpartlit.op[kahihi],10);
itoa(litaddress,resultLIT.address[litno],10);
litaddress++;
resultLIT.count[litno]=litno;
litno++;
poolpresence[pool]++;
purvichakahihi=kahihi+1;
}
}
pool++;
}
}
}
/**********************************************/
void literaltable()
{
getch();
clrscr();
findliteral();
cout<<"\n\n\tRemaining => ";
for(i=0;i<rest;i++)
cout<<"\n\t"<<restpartlit.count[i]<<"\t"<<restpartlit.mnemonic[i]<<"\t"<<restpartlit.address[i]<<"\t"<<restpartlit.clas[i]<<"\t"<<restpartlit.op[i];
cout<<"\nLiteral Table is ==> ";
for(i=0;i<litno;i++)
cout<<"\n\n\t"<<resultLIT.count[i]<<"\t"<<resultLIT.mnemonic[i]<<"\t"<<resultLIT.address[i];

}
/**********************************************/
void findafterequ()
{
for(endadd=0;endadd<rest;endadd++)
{
if(strcmp(restpartlit.mnemonic[endadd],"EQU")==0 && strcmp(restpartlit.mnemonic[endadd-1],firstofline.mnemonic[kahihi])==0)
{
strcpy(sadha,restpartlit.mnemonic[endadd+1]);
ptr=0;
while(ptr!=strlen(sadha))
{
if(attempt==1)
{
temp[0]=sadha[ptr];
temp[1]='\0';
strcat(s2,temp);
sadhalen++;
}
if(sadha[ptr]=='+')
{
sadhalen=0;
attempt=1;
}
if(attempt!=1)
{
temp[0]=sadha[ptr];
temp[1]='\0';
strcat(s1,temp);
sadhalen++;
}
ptr++;
}
attempt=0;
int sadhaint=atoi(s2);
for(int ii=0;ii<rest;ii++)
{
if(strcmp(firstofline.mnemonic[ii],s1)==0)
{
s1add=atoi(firstofline.address[ii]);
goto neeche;
}
}
neeche:
s1add=s1add+sadhaint;
itoa(s1add,firstofline.address[kahihi],10);
}
}
}
/**********************************************/
void findsymbol()
{
getch();
clrscr();
for(i=0;i<rest;i++)
{
kahihi=i;
for(poi=0;poi<29;poi++)
{
if(strcmp(firstofline.mnemonic[kahihi],MOT.mnemonic[poi])==0)
goto khali;
}
strcpy(resultSYM.mnemonic[symcnt],firstofline.mnemonic[kahihi]);
findafterequ();
strcpy(resultSYM.address[symcnt],firstofline.address[kahihi]);
strcpy(restpartlit.clas[kahihi],"S");
itoa(symcnt,restpartlit.op[kahihi],10);
resultSYM.count[symcnt]=symcnt;
symcnt++;
khali:
}
for(i=0;i<rest;i++)
{
for(kahihi=0;kahihi<symcnt;kahihi++)
if(strcmp(restpartlit.mnemonic[i],resultSYM.mnemonic[kahihi])==0)
{
strcpy(restpartlit.clas[i],"S");
itoa(symcnt,restpartlit.op[kahihi],10);
}
}
iner:
cout<<"\n\n\tSYMTABLE => ";
for(i=0;i<symcnt;i++)
cout<<"\n\n\t"<<resultSYM.count[i]<<"\t"<<resultSYM.mnemonic[i]<<"\t"<<resultSYM.address[i]<<"\t"<<resultSYM.op[i];
}
/**********************************************/
void pooltable()
{
getch();
clrscr();
cout<<"\n\n\tPool Table => ";
for(i=0;i<pool;i++)
{
if(poolpresence[i]!=0)
cout<<"\n\t"<<i<<"\t"<<poolind[i];
}
}
/**********************************************/
void output()
{
getch();
clrscr();
cout<<"\n\n\tOutput is  => ";
int varcnt;
varcnt=0;
endadd=atoi(restpartlit.address[0]);
for(i=0;i<restpartlit.count[rest-1];i++)
{
if(atoi(restpartlit.address[i])!=endadd)
{
if(strcmp(restpartlit.clas[i],"S")==0)
i++;
if(strcmp(restpartlit.op[i],NULL)!=0 && strcmp(restpartlit.op[i],NULL)!=0)
cout<<"\n\t("<<restpartlit.clas[i]<<","<<restpartlit.op[i]<<")";
else
cout<<"\n\t";
varcnt++;
}
else
{
if(varcnt==1)
{
if(strcmp(restpartlit.clas[i],"AD")==0)
{

}
else
{
if(strcmp(restpartlit.clas[i],"RG")==0)
cout<<"\t("<<restpartlit.clas[i]<<","<<restpartlit.op[i]<<")";
else
cout<<"\t(RG,00)";
}
}
else
cout<<"\t("<<restpartlit.clas[i]<<","<<restpartlit.op[i]<<")";
varcnt=0;

}
endadd=atoi(restpartlit.address[i]);
}
}
/**********************************************/
void hi()
{
getch();
clrscr();
cout<<"\n\n\tRemaining => ";
for(i=0;i<rest;i++)
cout<<"\n\t"<<restpartlit.count[i]<<"\t"<<restpartlit.mnemonic[i]<<"\t"<<restpartlit.address[i]<<"\t"<<restpartlit.clas[i]<<"\t"<<restpartlit.op[i];
}
/**********************************************/
void constants()
{
getch();
clrscr();
for(i=0;i<rest;i++)
{
if(strcmp(restpartlit.clas[i],NULL)==0)
{
for(kahihi=0;kahihi<symcnt;kahihi++)
if(strcmp(restpartlit.mnemonic[i],resultSYM.mnemonic[kahihi])==0){goto AA;}
AA: if(kahihi==symcnt)
{
strcpy(restpartlit.clas[i],"C");
itoa(consno,restpartlit.val[i],10);
consno++;
}
}
}
}
/**********************************************/
void main()
{
clrscr();
cout<<"Input to Assembler is => \n";
pk=fopen("c:\\fil.txt","r");
for(i=0;i<30;i++)
{
poolind[i]=0;
poolpresence[i]=0;
}
strcpy(sadha,NULL); // this string is used for defining string after EQU stmt...
strcpy(s1,NULL); // this string is used for defining string of 1st part after EQU stmt & before '+'...
strcpy(s2,NULL); // this string is used for defining string of 2nd part after EQU stmt & after '+'...
s1add=0;
attempt=0;
// it is the value used to detect attemption of '+' in file...
pool=0;
// it is the value used to measure pool values in file...
consno=0;
// it is the value used to measure cons values in file...
symcnt=0;
// it is the value used to measure rest values in file...
rest=0;
// it is the value used to measure rest values in file...
litno=0;
// it is the value used to measure no. of literals in file...
regular=0;
// it is the value used to measure all the words less than j parrameter in file...
k=0; // k is parameter used for counting no. of MOT records...
cnt=-1;// cnt is parameter used for measuring row number for address purpose...
fclose(pk);
clrscr();
declare();
getwordsfromfile();
findwordtype();
resultofMOT();
literaltable();
pooltable();
findsymbol();
constants();
hi();
output();
getch();
}
/**********************************************/
/******************** C:/fil.txt**********************/
START 1000

READ N
MOVER BREG,='1'
MOVEM BREG,TERM
L MULT BREG,TERM
MOVER CREG,TERM
COMP CREG,N
BC LE,AGAIN
NEXT EQU L+1
MOVEM B,RESULT
LTORG
PRINT RESULT
STOP
N DS 1
RESULT DS 20
TERM DS 1
END
/**********************************************/