Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.38 KB | None | 0 0
  1. /*
  2.  
  3. INPUT FILE kk.txt
  4.  
  5.  
  6. macro ddd &a,&b,&reg=AREG
  7. lcl &m
  8. &m set 0
  9. mover &reg,&a
  10. mend
  11. start
  12. MOVER AREG,='2'
  13. ddd 2,4
  14. end
  15. */
  16.  
  17.  
  18.  
  19.  
  20. #include<stdio.h>
  21. #include<conio.h>
  22.  
  23. struct pntab{
  24. char var[15];
  25. }p1[20];
  26.  
  27. struct evntab{
  28. char var[15];
  29. }e1[20];
  30.  
  31. struct ssntab{
  32. char lb[15];
  33. }s1[20];
  34.  
  35. struct mnt{
  36. char mname[20];
  37. int pp,kp,ev,mdtp,kpdtp,sstp;
  38. }m1[10];
  39.  
  40. struct kptab{
  41. char var[15],val[15];
  42. }k1[20];
  43.  
  44. struct sstab{
  45. int lbp;
  46. }ss1[20];
  47.  
  48. struct mdt{
  49. char stmt[50];
  50. }mm1[50];
  51.  
  52.  
  53. struct aptab{
  54. char val[20];
  55. }a1[20];
  56.  
  57. void main()
  58. {
  59.  int kp=1,sp=1,mp=1,snp=1,pnp=1,evp=1,l=1,ap=1,i,j,k,nl,kl,me,cn,opt,e=0,en,mac,a,z,x,pr;
  60.  char fn[15],str[30],str1[30],str2[50];
  61.  
  62.  FILE *f1,*f2;
  63.  
  64.  
  65.     f1=NULL;
  66.     printf("\n\nenter the source file name\n\n");
  67.     scanf("%s",fn);
  68.     f1=fopen(fn,"rb");
  69.  
  70.     while(f1==NULL)
  71.     {
  72.         printf("\n\nSuch file not exists\n\n");
  73.         printf("\n\nEnter the valid file name\n\n");
  74.         scanf("%s",fn);
  75.         f1=fopen(fn,"rb");
  76.     }
  77.  
  78.  
  79. while(e==0)                             //while eof
  80.  {
  81.    i=0;
  82.    do
  83.     {
  84.       str[i]=fgetc(f1);
  85.       if(str[i]==EOF)
  86.        {
  87.     e=1;
  88.     break;
  89.        }
  90.       if(str[i]=='\n'||str[i]=='\r')
  91.        {
  92.     i++;
  93.     break;
  94.        }
  95.     }while(str[i++]!=' ');
  96.  
  97.     i--;
  98.     str[i]='\0';
  99.  
  100.     if(strcmp(str,"START")==NULL)                   //if start kw appears
  101.      {e=1;
  102.       break;}
  103.  
  104.  
  105.     if(!strcmp(str,"macro")||!strcmp(str,"MACRO"))          //if a macro kw
  106.      {
  107.  
  108.        i=0;
  109.        do
  110.     {
  111.     str[i]=fgetc(f1);
  112.  
  113.     if(str[i]=='\n'||str[i]=='\r')
  114.       {
  115.       i++;
  116.       break;
  117.       }
  118.     if(str[i]==EOF)
  119.       {
  120.       i++;
  121.       e=1;
  122.       break;
  123.       }                             //name of macro
  124.     }while(str[i++]!=' ');
  125.     i--;
  126.     str[i]='\0';
  127.  
  128.  
  129.  
  130.     strcpy(m1[l].mname,str);
  131.     nl=0;
  132.     while(nl==0)
  133.     {
  134.       i=0;
  135.       //nl=0;
  136.       kl=0;
  137.       do
  138.         {
  139.        //  if(kl==0)
  140.            str[i]=fgetc(f1);
  141.        //  else
  142.          //  str1[i]=fgetc(f1);
  143.          if(str[i]=='\n'||str[i]=='\r')                     //parameters of macro
  144.            {
  145.         nl=1;
  146.         i++;
  147.         break;
  148.            }
  149.          if(str[i]==EOF)                        //updating mnt
  150.            {
  151.         i++;
  152.         e=1;
  153.         break;
  154.            }
  155.          if(str[i]=='=')
  156.            {
  157.            //   str[i-1]='\0';
  158.            i++;
  159.         kl=1;
  160.          break;
  161.            }
  162.          if(str[i]=='&')
  163.          { i--;
  164.            continue;
  165.          }
  166.        }while(str[i++]!=',');
  167.       i--;
  168.       str[i]='\0';
  169.  
  170.  
  171.     if(kl==1)
  172.      {
  173.       i=0;
  174.       do
  175.         {
  176.       //   if(kl==0)
  177.         //   str[i]=fgetc(f1);
  178.         // else
  179.            str1[i]=fgetc(f1);
  180.          if(str1[i]=='\n'||str1[i]=='\r')                       //parameters of macro
  181.            {
  182.         nl=1;
  183.         i++;
  184.         break;
  185.            }
  186.          if(str[i]==EOF)                        //updating mnt
  187.            {
  188.         i++;
  189.         e=1;
  190.         break;
  191.            }
  192.        }while(str[i++]!=',');
  193.       i--;
  194.       str1[i]='\0';
  195.      }
  196.  
  197.  
  198.  
  199.       if(kl==0)
  200.        {
  201.         strcpy(p1[pnp++].var,str);
  202.         m1[l].pp++;
  203.        }
  204.       m1[l].kpdtp=kp;
  205.       if(kl==1)
  206.        {
  207.         strcpy(k1[kp].var,str);
  208.         strcpy(k1[kp++].val,str1);
  209.         strcpy(p1[pnp++].var,str);
  210.         m1[l].kp++;
  211.        }
  212.       m1[l].mdtp=mp;
  213.       m1[l].ev=0;
  214.       m1[l].sstp=sp;
  215.     }                               //while(nl==0)
  216.  
  217.            str[i]=fgetc(f1);                        //processing macro stmt
  218.  
  219.     me=1;
  220.        i=0;
  221.        do
  222.          {
  223.            str[i]=fgetc(f1);                        //processing macro stmt
  224.            if(str[i]==EOF)
  225.         {
  226.          e=1;
  227.          break;
  228.         }
  229.           if(str[i]=='\n'||str[i]=='\r')
  230.         {
  231.             i++;
  232.             break;
  233.         }
  234.  
  235.          }while(str[i++]!=' ');
  236.  
  237.        i--;
  238.        str[i]='\0';
  239.        if(strcmp(str,"mend")==0||strcmp(str,"MEND")==0)
  240.           me=0;
  241.     while(me==1)
  242.      {
  243.        i=0;
  244.  
  245.        do
  246.          {
  247.            str1[i]=fgetc(f1);
  248.            if(str1[i]==EOF)
  249.         {
  250.          e=1;
  251.          break;
  252.         }
  253.            if(str1[i]=='\n'||str1[i]=='\r')
  254.            {
  255.         i++;
  256.         break;
  257.            }
  258.  
  259.          }while(str1[i++]!=' ');
  260.  
  261.        i--;
  262.        str1[i]='\0';
  263.  
  264.        if(strcmp(str,"LCL")==0||strcmp(str,"lcl")==0)       //if lcl stmt
  265.         {
  266.           for (i=0;i<strlen(str);i++)
  267.         str1[i]=str1[i+1];
  268.           strcpy(e1[evp++].var,str1);
  269.           m1[l].ev++;
  270.           sprintf(str2,"LCL (E,%d)",evp);
  271.           strcpy();
  272.         }
  273.  
  274.        else if(strcmp(str1,"set")==0||strcmp(str1,"SET")==0)        //if set stmt
  275.         {
  276.           for (i=0;i<strlen(str);i++)
  277.         str[i]=str[i+1];
  278.           for (i=1;i<evp;i++)
  279.            {
  280.          if(strcmp(e1[i].var,str)==0)
  281.          break;
  282.            }
  283.           j=i;
  284.            //    str1[i]=fgetc(f1);
  285.           i=0;
  286.           do
  287.            {
  288.          str1[i]=fgetc(f1);
  289.          if(str1[i]==EOF)
  290.           {
  291.             e=1;
  292.             break;
  293.           }
  294.          if(str1[i]=='\n'||str1[i]=='\r')
  295.           {
  296.             i++;
  297.             break;
  298.           }
  299.  
  300.            }while(str1[i++]!=' ');
  301.  
  302.           i--;
  303.           str1[i]='\0';
  304.           sprintf(str2,"(E,%d) SET ",j);
  305.           cn=1;
  306.           opt=1;
  307.           if(str1[0]=='&')
  308.           while(opt==1)
  309.            {
  310.          k=0;
  311.          for(;str1[cn]!='+'&&str1[cn]!='\n';cn++)
  312.           {
  313.             str[k]=str1[cn];
  314.             k++;
  315.           }
  316.          if(str1[cn]=='+')
  317.             opt=1;
  318.          else
  319.             opt=2;
  320.          if(str[0]=='&')
  321.           {
  322.             for (i=0;i<strlen(str);i++)
  323.                str[i]=str[i+1];
  324.             for (i=1;i<evp;i++)
  325.              {
  326.                if(strcmp(e1[i].var,str)==0)
  327.              break;
  328.              }
  329.             if(opt==1)
  330.                sprintf(str,"(E,%d)+",i);
  331.             if(opt==2)
  332.                sprintf(str,"(E,%d)",i);
  333.             strcat(str2,str);
  334.           }
  335.          else
  336.           {
  337.             i=atoi(str);
  338.             if(opt==1)
  339.               sprintf(str,"%d+",i);
  340.             if(opt==2)
  341.               sprintf(str,"%d",i);
  342.             strcat(str2,str);
  343.           }
  344.         }
  345.            else
  346.         {
  347.           strcat(str2,str1);
  348.         }
  349.         }                                   //end of set processing
  350.  
  351.  
  352.        else if(strcmp(str,"AIF")==0||strcmp(str,"aif")==0||strcmp(str,"AGO")==0||strcmp(str,"ago")==0)
  353.         {
  354.  
  355.         }                                   //aif or ago stmt
  356.  
  357.        else
  358.         {
  359.           sprintf(str2,"%s ",strupr(str));
  360.           cn=1;
  361.           while(opt==1)
  362.            {
  363.          for(i=0;str1[cn]!=','&&str1[cn]!='\0';i++,cn++)
  364.            str[i]=str1[cn];
  365.          if(str1[cn++]==',')
  366.            opt=1;
  367.          else
  368.            opt=2;
  369.          cn++;
  370.  
  371.          str[i]='\0';
  372.          for(i=1;i<pnp;i++)
  373.            if(strcmp(str,p1[i].var)==0)
  374.              break;
  375.          if(opt==1)
  376.          sprintf(str,"(P,%d),",i);
  377.          if(opt==2)
  378.          sprintf(str,"(P,%d)",i);
  379.          strcat(str2,str);
  380.            }
  381.         }
  382.  
  383.        strcpy(mm1[mp++].stmt,str2);
  384.            str[i]=fgetc(f1);                        //processing macro stmt
  385.  
  386.        i=0;
  387.        do
  388.          {
  389.            str[i]=fgetc(f1);                        //processing macro stmt
  390.            if(str[i]==EOF)
  391.         {
  392.          e=1;
  393.          break;
  394.         }
  395.            if(str[i]=='\n'||str[i]=='\r')
  396.         {
  397.          i++;
  398.          break;
  399.         }
  400.  
  401.          }while(str[i++]!=' ');
  402.  
  403.        i--;
  404.        str[i]='\0';
  405.        if(strcmp(str,"mend")==0||strcmp(str,"MEND")==0)
  406.         {
  407.           l++;
  408.           me=0;
  409.         }
  410.  
  411.  
  412.  
  413.      }                                  //while(me==1)
  414.  
  415.      }                                      //if macro stmt
  416.  }                                      //while(e=0)
  417.  
  418.  
  419.  fclose(f1);
  420.  f1=fopen(fn,"rb");
  421.  
  422.  while(strcmp(strupr(str),"START")!=0)
  423.   {
  424.     while(str[i]!='\n')
  425.        str[i]=fgetc(f1);
  426.     i=0;
  427.     do
  428.      {
  429.         str[i]=fgetc(f1);
  430.         if(str[i]==EOF)
  431.          {
  432.             e=1;
  433.             break;
  434.           }
  435.         if(str[i]=='\n'||str[i]=='\r')
  436.           {
  437.             i++;
  438.             break;
  439.           }
  440.      }while(str[i++]!=' ');
  441.     i--;
  442.     str[i]='\0';
  443.   }
  444.  
  445.  f2=fopen("kkm.txt","w");
  446.  
  447.  i=0;
  448.  str1[i]=fgetc(f1);
  449.  if(str1[i++]==' ')
  450.  {
  451.  do
  452.  {
  453.     str1[i]=fgetc(f1);
  454.     if(str[i]==EOF)
  455.      {
  456.         e=1;
  457.         break;
  458.       }
  459.     if(str1[i]=='\n'||str1[i]=='\r')
  460.       {
  461.         i++;
  462.         break;
  463.       }
  464.  }while(str1[i++]!=' ');
  465.  
  466.  i--;
  467.  str1[i]='\0';
  468.  
  469.   fprintf(f2,"%s %s\n",str,str1);
  470.  }
  471.  else
  472.   fprintf(f2,"%s\n",str);
  473.  
  474.   e=0;
  475.   en=0;
  476.  
  477.  
  478.  while(e==0&&en==0)
  479.   {
  480.     mac=0;
  481.     i=0;
  482.     while(mac!=1&&e!=1)
  483.       {
  484. /*      while(str[i]!='\n'&&str1[0]!='\n')
  485.          {
  486.            str[i]=fgetc(f1);
  487.            if(str[i]!='\r')
  488.              fprintf(f2,"%c",str[i]);
  489.          } */
  490.             str1[0]='t';
  491.         i=0;
  492.         pr=0;
  493.         do
  494.         {
  495.             str[i]=fgetc(f1);
  496.             //if(str[i]!='\r')
  497.               // fprintf(f2,"%c",str[i]);
  498.             if(str[i]==EOF)
  499.              {       i++;
  500.                 e=1;
  501.                 break;
  502.              }
  503.             if(str[i]=='\n'||str[i]=='\r')
  504.              {
  505.                 pr=1;
  506.                 i++;
  507.                 break;
  508.              }
  509.         }while(str[i++]!=' ');
  510.         i--;
  511.         str[i]='\0';
  512.  
  513.         for(j=1;j<l;j++)
  514.           if(strcmp(m1[j].mname,str)==0)
  515.           {
  516.            mac=1;
  517.            goto zzz;
  518.           }
  519.  
  520.         if(strcmp(strupr(str),"END")==0)
  521.          {
  522.             for(i=1;i<evp;i++)
  523.             fprintf(f2,"%s DC 0\n",e1[i].var);
  524.             fprintf(f2,"%s\n",str);
  525.             if(e==1)
  526.             {
  527.                 fclose(f2);
  528.                 fclose(f1);
  529.                 exit(0);
  530.             }
  531.          }
  532.  
  533.  
  534.         if(pr==0)
  535.           fprintf(f2,"%s ",str);
  536.         else
  537.         {
  538.           fprintf(f2,"%s\n",str);
  539.             str[i]=fgetc(f1);}
  540.       }
  541.  
  542. zzz:    if(e==1)
  543.       exit(0);
  544.  // j--;
  545.     i=0;
  546.     do
  547.     {
  548.         str1[i]=fgetc(f1);
  549.         if(str1[i]==EOF)
  550.          {
  551.             e=1;
  552.             break;
  553.          }
  554.         if(str1[i]==',')
  555.          {
  556.             str1[i]='\0';
  557.             if(str1[0]>='0'&&str1[0]<='9')
  558.              {
  559.                 str1[i+3]='\0';
  560.                 str1[i+2]=39;
  561.                 for(x=i+1;x>1;x--)
  562.                   str1[x]=str1[x-2];
  563.                 str1[0]='=';
  564.                 str1[1]=39;
  565.              }
  566.             strcpy(a1[ap++].val,str1);
  567.             i=0;
  568.             continue;
  569.          }
  570.         else if(str1[i]=='\r')
  571.          {
  572.             str1[i]='\0';
  573.             if(str1[0]>='0'&&str1[0]<='9')
  574.              {
  575.                 str1[i+3]='\0';
  576.                 str1[i+2]=39;
  577.                 for(x=i+1;x>1;x--)
  578.                   str1[x]=str1[x-2];
  579.                 str1[0]='=';
  580.                 str1[1]=39;
  581.              }
  582.             strcpy(a1[ap++].val,str1);
  583.                     str1[i]=fgetc(f1);
  584.  
  585.             break;
  586.          }
  587.         i++;
  588.     }while(str1[i]!='\n'&&str1[i]!='\r');
  589.  
  590.     if(ap<pnp)
  591.      {
  592.        for(i=1;i<kp;i++)
  593.          strcpy(a1[ap++].val,k1[i].val);
  594.      }
  595.  
  596.     if(j<l-1)
  597.       k=m1[j+1].mdtp;
  598.     else
  599.       k=mp;
  600.  
  601.     for(a=m1[j].mdtp;a<k;a++)
  602.      {
  603.         i=0;
  604.         z=0;
  605.         while(mm1[a].stmt[z]!=' ')
  606.         str[i++]=mm1[a].stmt[z++];
  607.         str[i]='\0';
  608.         z++;
  609.  
  610.         i=0;
  611.         while(mm1[a].stmt[z]!='\0'&&mm1[a].stmt[z]!=' ')
  612.         str1[i++]=mm1[a].stmt[z++];
  613.         str1[i]='\0';
  614.  
  615.         if(mm1[a].stmt[z]=='\0')
  616.           nl=0;
  617.         else
  618.           nl=1;
  619.  
  620.         if(strcmp(str,"LCL")==0)        //if lcl stmt
  621.         {
  622.             continue;
  623.         /*  for(i=3;str[i]!=')';i++)
  624.               str[i-3]=str[i];
  625.             str[i-3]='\0';
  626.             z=atoi(str);
  627.             strcpy(str,e1[z].var);
  628.  
  629.             for (i=0;i<strlen(str);i++)
  630.             str1[i]=str1[i+1];
  631.             strcpy(e1[evp++].var,str1);
  632.             m1[l].ev++;
  633.             sprintf(str2,"LCL (E,%d)",evp);
  634.             strcpy();*/
  635.         }
  636.  
  637.         else if(strcmp(str1,"SET")==0)      //if set stmt
  638.         {
  639.             for(i=3;str[i]!=')';i++)
  640.               str[i-3]=str[i];
  641.             str[i-3]='\0';
  642.             i=atoi(str);
  643.             strcpy(str,e1[i].var);
  644.  
  645.             i=0;
  646.             z++;
  647.             while(mm1[a].stmt[z]!='\0')
  648.             str1[i++]=mm1[a].stmt[z++];
  649.             str1[i]='\0';
  650.             z=atoi(str1);
  651.  
  652.             fprintf(f2,"MOVER AREG,='%d'\nMOVEM AREG,%s\n",z,str);
  653.         }                                   //end of set processing
  654.  
  655.  
  656.         //  else if(strcmp(str,"AIF")==0||strcmp(str,"aif")==0||strcmp(str,"AGO")==0||strcmp(str,"ago")==0)
  657.         //  {
  658.  
  659.         //  }                                   //aif or ago stmt
  660.  
  661.         else
  662.          {
  663.  
  664.             fprintf(f2,"%s ",strupr(str));
  665.             i=0;
  666.             while(str1[i]!='\0')
  667.              {
  668.                 if(str1[i]=='('&&str1[i+1]=='E')
  669.                   {
  670.                     i+=3;
  671.                     k=0;
  672.                     while(str1[i]!=')')
  673.                         str[k++]=str1[i++];
  674.                     str[k]='\0';
  675.                     k=atoi(str);
  676.                     fprintf(f2,"%s",e1[k].var);
  677.                     i++;
  678.                     if(str1[i]==',')
  679.                      {
  680.                         fprintf(f2,",");
  681.                         i++;
  682.                      }
  683.                     if(str1[i]=='\0')
  684.                         fprintf(f2,"\n");
  685.                   }
  686.  
  687.                 else if(str1[i]=='('&&str1[i+1]=='P')
  688.                   {
  689.                     i+=3;
  690.                     k=0;
  691.                     while(str1[i]!=')')
  692.                         str[k++]=str1[i++];
  693.                     str[k]='\0';
  694.                     k=atoi(str);
  695.                     fprintf(f2,"%s",a1[k].val);
  696.                     i++;
  697.                     if(str1[i]==',')
  698.                      {
  699.                         fprintf(f2,",");
  700.                         i++;
  701.                      }
  702.                     if(str1[i]=='\0')
  703.                         fprintf(f2,"\n");
  704.                   }
  705.  
  706.                 else
  707.                   {
  708.                     k=0;
  709.                     while(str1[i]!='\0'||str1[i]!=',')
  710.                         str[k++]=str1[i++];
  711.                     str[k]='\0';
  712.                     fprintf(f2,"%s",strupr(str));
  713.                     if(str1[i]==',')
  714.                      {
  715.                         fprintf(f2,",");
  716.                         i++;
  717.                      }
  718.                     if(str1[i]=='\0')
  719.                         fprintf(f2,"\n");
  720.                   }
  721.              }
  722.          }
  723.      }                                                                             //macro expansion     for(a=m1[j].mdtp;a<k;a++)
  724.   }                                 //while(e==0||en==0)
  725.  
  726. }
  727.                                         //main
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755. //@h@    CODE OF ASSEMBLER PASS 1
  756.  
  757.  
  758. #include<stdio.h>
  759. #include<stdlib.h>
  760. #include<conio.h>
  761. #include<string.h>
  762. #include<fstream.h>
  763.  
  764. struct code
  765. {
  766.   char label[10],opcode[10],operand1[10],operand2[10];
  767. }code1[30];
  768.  
  769. struct iccode
  770. {
  771.   int add,opcodevalue,operand1code,operand2value;
  772.   char opcodeclass[3],operand2class;
  773. }iccode1[30];
  774.  
  775. struct symtab
  776. {
  777.   int sno,addr,len;
  778.   char sym[10];
  779. }symtab1[30];
  780.  
  781. struct littab
  782. {
  783.   int littab_ptr,lit_add;
  784.   char lit[10];
  785. }littab1[30];
  786.  
  787.  
  788. int opcodeimpck(char a[])
  789. {
  790.   int i;
  791.   if(strcmp(a,"STOP")==0)
  792.     return(0);
  793.   if(strcmp(a,"ADD")==0)
  794.     return(1);
  795.   if(strcmp(a,"SUB")==0)
  796.     return(2);
  797.   if(strcmp(a,"MUL")==0)
  798.     return(3);
  799.   if(strcmp(a,"MOVER")==0)
  800.     return(4);
  801.   if(strcmp(a,"MOVEM")==0)
  802.     return(5);
  803.   if(strcmp(a,"COMP")==0)
  804.     return(6);
  805.   if(strcmp(a,"BC")==0)
  806.     return(7);
  807.   if(strcmp(a,"DIV")==0)
  808.     return(8);
  809.   if(strcmp(a,"READ")==0)
  810.     return(9);
  811.   if(strcmp(a,"PRINT")==0)
  812.     return(10);
  813.   return(-1);
  814. }
  815.  
  816.  
  817.  
  818. int opcodeassck(char a[])
  819. {
  820.   int i;
  821.   if(strcmp(a,"START")==0)
  822.     return(1);
  823.   if(strcmp(a,"END")==0)
  824.     return(2);
  825.   if(strcmp(a,"ORIGIN")==0)
  826.     return(3);
  827.   if(strcmp(a,"EQU")==0)
  828.     return(4);
  829.   if(strcmp(a,"LTORG")==0)
  830.     return(5);
  831.   return(-1);
  832. }
  833.  
  834.  
  835.  
  836. int opcodedecck(char a[])
  837. {
  838.   if(strcmp(a,"DC")==0)
  839.     return(1);
  840.   if(strcmp(a,"DS")==0)
  841.     return(2);
  842.   return(-1);
  843. }
  844.  
  845.  
  846.  
  847. int oprnd1(char a[])
  848. {
  849.   if(strcmp(a,"AREG")==0)
  850.     return(1);
  851.   if(strcmp(a,"BREG")==0)
  852.     return(2);
  853.   if(strcmp(a,"CREG")==0)
  854.     return(3);
  855.   if(strcmp(a,"DREG")==0)
  856.     return(4);
  857.   return(-1);
  858. }
  859.  
  860.  
  861.  
  862. void main()
  863. {
  864.   int loc_cntr=0,pooltab_ptr=1,pooltab[10],littab_ptr=0,symtab_ptr=0;
  865.  
  866.   int i,j,k,rt,ln,op,opr,e,err;
  867.   char str[15];
  868.   FILE  *fp1,*fp2,*fp3;
  869.   clrscr();
  870.   fp1=fopen("KKM.txt","r");
  871.  
  872.   if(fp1==NULL)
  873.   {
  874.     printf("\ncann't open file\n");
  875.     exit(0);
  876.   }
  877.   ln=0;
  878.   op=0;
  879.   e=0;
  880.  
  881.   pooltab[0]=1;
  882.   i=0;
  883.   rt=1;
  884.  
  885.                   //check for "start" keyword
  886.  
  887.   do
  888.     {
  889.       str[i]=fgetc(fp1);
  890.       if(str[i]==EOF)
  891.        {
  892.     e=1;
  893.     break;
  894.        }
  895.       if(str[i]=='\n')
  896.        {
  897.     rt=0;
  898.     i++;
  899.     break;
  900.        }
  901.     }while(str[i++]!=' ');
  902.     i--;
  903.     str[i]='\0';
  904.  
  905.     if(strcmp(str,"START")==0)
  906.      {
  907.      if(rt==1)
  908.      {  i=0;
  909.      do
  910.       {
  911.     str[i]=fgetc(fp1);
  912.     if(str[i]==EOF)
  913.      {
  914.       e=1;
  915.       break;
  916.      }
  917.       }while(str[i++]!='\n');
  918.       i--;
  919.       str[i]='\0';
  920.       loc_cntr=atoi(str);
  921.      }
  922.      }
  923.  
  924.      else
  925.       {
  926.        printf("\nError:prog should begin with START\n");
  927.        exit(0);
  928.       }
  929.  
  930.      if(rt==0)
  931.      loc_cntr=0;
  932.  
  933.  
  934.  
  935.                         //pass1
  936.  
  937.  
  938.   do{
  939.      iccode1[ln].add=loc_cntr;
  940.      err=0;
  941.  
  942.       do{
  943.       i=0;
  944.  
  945.       do
  946.       {
  947.         str[i]=fgetc(fp1);
  948.         if(str[i]=='\n')
  949.          {
  950.           i++;
  951.           break;
  952.          }
  953.         if(str[i]==EOF)
  954.          {
  955.           i++;
  956.           e=1;
  957.           break;
  958.          }
  959.       }while(str[i++]!=' ');
  960.  
  961.       i--;
  962.       str[i]='\0';
  963.       rt=opcodeimpck(str);              //check for imperative stmt
  964.       if(rt==-1)
  965.        {
  966.          rt=opcodeassck(str);           //check for assembler directive
  967.          if(rt==-1)
  968.           {
  969.            rt=opcodedecck(str);         //check for declarative stmt
  970.            if(rt==-1)
  971.         {
  972.          if(err==1)                 //if second word is not any keyword
  973.          {
  974.          printf("\nError at line %d\n",ln+2);
  975.          printf("%s ",code1[ln].label);
  976.         do
  977.          {
  978.           str[i]=fgetc(fp1);
  979.           printf("%c",str[i]);
  980.             if(str[i]==EOF)
  981.              {
  982.               e=1;
  983.               break;
  984.              }
  985.          }while(str[i++]!='\n');
  986.  
  987.          getch();
  988.          exit(0);
  989.          }
  990.  
  991.          strcpy(code1[ln].label,str);
  992.          j=0;
  993.          err=1;                              //set label has been read
  994.          for(i=0;i<symtab_ptr;i++)
  995.            if(strcmp(symtab1[i].sym,str)==0) //check if label is present in table
  996.              j=1;
  997.  
  998.          if(j==0)                            //if not copy in table
  999.           {
  1000.            symtab1[symtab_ptr].addr=loc_cntr;
  1001.            symtab1[symtab_ptr].len=1;
  1002.            strcpy(symtab1[symtab_ptr].sym,str);
  1003.            symtab_ptr++;
  1004.           }
  1005.          op=0;
  1006.         }
  1007.           else
  1008.            {
  1009.         op=3;
  1010.         strcpy(code1[ln].opcode,str);
  1011.         strcpy(iccode1[ln].opcodeclass,"DL");
  1012.         iccode1[ln].opcodevalue=rt;
  1013.            }
  1014.           }
  1015.          else
  1016.           {
  1017.         op=2;
  1018.         strcpy(code1[ln].opcode,str);
  1019.         strcpy(iccode1[ln].opcodeclass,"AD");
  1020.         iccode1[ln].opcodevalue=rt;
  1021.           }
  1022.        }
  1023.  
  1024.       else
  1025.        {
  1026.          strcpy(code1[ln].opcode,str);
  1027.          strcpy(iccode1[ln].opcodeclass,"IS");
  1028.          iccode1[ln].opcodevalue=rt;
  1029.          op=1;
  1030.        }
  1031.       }while(op==0&&e==0);
  1032.  
  1033.       if(op==1&&e==0)               //imperative stmt
  1034.        { i=0;
  1035.     do                          //Read register
  1036.      {
  1037.       str[i]=fgetc(fp1);
  1038.         if(str[i]==EOF)
  1039.          {
  1040.           e=1;
  1041.           break;
  1042.          }
  1043.      }while(str[i++]!=',');
  1044.     i--;
  1045.     str[i]='\0';
  1046.  
  1047.     opr=oprnd1(str);
  1048.     if(opr==-1)                         //error if not a register
  1049.     {
  1050.       printf("\nError at line %d\n",ln+2);
  1051.       printf("%s %s ",code1[ln].label,code1[ln].opcode);
  1052.       do
  1053.        {
  1054.         str[i]=fgetc(fp1);
  1055.         printf("%c",str[i]);
  1056.        }while(str[i]==EOF||str[i++]!='\n');
  1057.       getch();
  1058.       exit(0);
  1059.     }
  1060.     strcpy(code1[ln].operand1,str);
  1061.     iccode1[ln].operand1code=opr;
  1062.  
  1063.     i=0;                        //read operand 2
  1064.     do
  1065.      {
  1066.     str[i]=fgetc(fp1);
  1067.         if(str[i]==EOF)
  1068.          {
  1069.           e=1;
  1070.           i++;
  1071.           break;
  1072.          }
  1073.        }while(str[i++]!='\n'&&e==0);
  1074.       i--;
  1075.       str[i]='\0';
  1076.       strcpy(code1[ln].operand2,str);
  1077.       if(str[0]=='=')                    //operand 2 is literal
  1078.        {
  1079.     iccode1[ln].operand2class='L';
  1080.     j=0;
  1081.     for(i=pooltab[pooltab_ptr-1];i<littab_ptr;i++)
  1082.       if(strcmp(littab1[i].lit,str)==0)
  1083.        {
  1084.         iccode1[ln].operand2value=i+1;
  1085.         j=1;
  1086.        }
  1087.     if(j==0)
  1088.      {
  1089.       iccode1[ln].operand2value=littab_ptr+1;
  1090.       strcpy(littab1[littab_ptr].lit,str);
  1091.       littab_ptr++;
  1092.      }
  1093.        }
  1094.  
  1095.       else                                  //operand 2 is symbol
  1096.        {
  1097.     iccode1[ln].operand2class='S';
  1098.     j=0;
  1099.     for(i=0;i<symtab_ptr;i++)
  1100.       if(strcmp(symtab1[i].sym,str)==0)
  1101.        {
  1102.         iccode1[ln].operand2value=i+1;
  1103.         j=1;
  1104.        }
  1105.     if(j==0)
  1106.      {
  1107.       iccode1[ln].operand2value=symtab_ptr+1;
  1108.       strcpy(symtab1[symtab_ptr].sym,str);
  1109.       symtab_ptr++;
  1110.      }
  1111.        }
  1112.  
  1113.  
  1114.        }
  1115.  
  1116.       if((op==2&&e==0)||(op==2&&rt==2))           //assembler directive
  1117.        {
  1118.     i=0;
  1119.  
  1120.     if(rt==3)                                   //origin stmt
  1121.      {
  1122.       do
  1123.        {
  1124.         str[i]=fgetc(fp1);
  1125.         if(str[i]==EOF)
  1126.          {
  1127.           e=1;
  1128.           break;
  1129.          }
  1130.        }while(str[i++]!='\n'&&e==0);
  1131.       if(i!=0)
  1132.        loc_cntr=atoi(str);
  1133.      }
  1134.  
  1135.  
  1136.     if(rt==5||rt==2)                       //ltorg or end stmt
  1137.      {
  1138.        for(i=pooltab[pooltab_ptr-1]-1;i<littab_ptr;i++)
  1139.         {
  1140.           littab1[i].lit_add=loc_cntr;
  1141.           iccode1[ln].add=loc_cntr;
  1142.           strcpy(iccode1[ln].opcodeclass,"AD");
  1143.           iccode1[ln].opcodevalue=rt;
  1144.           iccode1[ln].operand2class='-';
  1145.           str[0]=littab1[i].lit[2];
  1146.           str[1]='\0';
  1147.           iccode1[ln].operand2value=atoi(str);
  1148.           ln++;
  1149.           loc_cntr++;
  1150.         }
  1151.         if(rt==5)
  1152.         {pooltab[pooltab_ptr++]=littab_ptr+1;
  1153.          goto ltorg;}
  1154.         else
  1155.          goto end;
  1156.      }
  1157.  
  1158.        }
  1159.  
  1160.       if(op==3&&e==0)                     //declarative stmt
  1161.        {
  1162.     if(rt==1)                         //DC
  1163.      {
  1164.       j=0;
  1165.       for(i=0;i<symtab_ptr;i++)
  1166.        if(strcmp(symtab1[i].sym,code1[ln].label)==0)
  1167.         {
  1168.          j=1;
  1169.          break;
  1170.         }
  1171.        if(j==0)
  1172.          i=symtab_ptr;
  1173.  
  1174.       symtab1[i].addr=loc_cntr;
  1175.       symtab1[i].len=1;
  1176.       strcpy(symtab1[i].sym,code1[ln].label);
  1177.       i=0;
  1178.       do
  1179.        {
  1180.         str[i]=fgetc(fp1);
  1181.         if(str[i]==EOF)
  1182.          {
  1183.            e=1;
  1184.            i++;
  1185.            break;
  1186.          }
  1187.         }while(str[i++]!='\n'&&e==0);
  1188.       i--;
  1189.       str[i]='\0';
  1190.       iccode1[ln].operand2class='C';
  1191.       iccode1[ln].operand2value=atoi(str);
  1192.       loc_cntr++;
  1193.       ln++;
  1194.       goto ltorg;
  1195.      }
  1196.     if(rt==2)                            //DS
  1197.      {
  1198.       j=0;
  1199.       for(i=0;i<symtab_ptr;i++)
  1200.        if(strcmp(symtab1[i].sym,code1[ln].label)==0)
  1201.         {
  1202.          j=1;
  1203.          break;
  1204.         }
  1205.        if(j==0)
  1206.          i=symtab_ptr;
  1207.  
  1208.       symtab1[i].addr=loc_cntr;
  1209.       strcpy(symtab1[i].sym,code1[ln].label);
  1210.       j=i;
  1211.       i=0;
  1212.       do
  1213.        {
  1214.         str[i]=fgetc(fp1);
  1215.         if(str[i]==EOF)
  1216.          {
  1217.            e=1;
  1218.            i++;
  1219.            break;
  1220.          }
  1221.         }while(str[i++]!='\n'&&e==0);
  1222.       i--;
  1223.       str[i]='\0';
  1224.       iccode1[ln].operand2class='C';
  1225.       iccode1[ln].operand2value=atoi(str);
  1226.       symtab1[j].len=atoi(str);
  1227.       loc_cntr=loc_cntr+atoi(str);
  1228.       ln=ln+atoi(str);
  1229.       goto ltorg;
  1230.      }
  1231.        }
  1232.  
  1233.       loc_cntr++;
  1234.       ln++;
  1235. ltorg:
  1236.   }while(e==0);
  1237.  
  1238. end:
  1239. /*
  1240. if(strcmp(code1[ln-1].opcode,"END")!=0||strcmp(code1[ln-1].opcode,"end")!=0)
  1241. {
  1242. printf("\nno end stmt\n");
  1243. exit(0);
  1244. } */
  1245.  
  1246. printf("\nPASS 1:-\n\nlabel  opcode  operand1  operand2\n");
  1247.  
  1248. fclose(fp1);
  1249.  
  1250. fp1=fopen("pass1.txt","w");
  1251.  
  1252. if(fp1==NULL)
  1253. {
  1254.   printf("\ncann't open file\n");
  1255.   exit(0);
  1256. }
  1257. printf("\n%-6s %-7s %-9s %-15s",code1[i].label,code1[0].opcode,code1[0].operand1,code1[0].operand2);
  1258. printf("%d (%s,%d) %d (%c,%d)",iccode1[0].add,iccode1[0].opcodeclass,iccode1[0].opcodevalue,iccode1[0].operand1code,iccode1[0].operand2class,iccode1[0].operand2value);
  1259. fprintf(fp1,"%d %s %d %d %c %d\n",iccode1[0].add,iccode1[0].opcodeclass,iccode1[0].opcodevalue,iccode1[0].operand1code,iccode1[0].operand2class,iccode1[0].operand2value);
  1260.  
  1261.  
  1262. for(i=0;i<ln;i++)
  1263. {
  1264. if(iccode1[i].add!=NULL)
  1265. {
  1266. printf("\n%-6s %-7s %-9s %-15s",code1[i].label,code1[i].opcode,code1[i].operand1,code1[i].operand2);
  1267. printf("%d (%s,%d) %d (%c,%d)",iccode1[i].add,iccode1[i].opcodeclass,iccode1[i].opcodevalue,iccode1[i].operand1code,iccode1[i].operand2class,iccode1[i].operand2value);
  1268. if(i<ln-1)
  1269. fprintf(fp1,"%d %s %d %d %c %d\n",iccode1[i].add,iccode1[i].opcodeclass,iccode1[i].opcodevalue,iccode1[i].operand1code,iccode1[i].operand2class,iccode1[i].operand2value);
  1270. else
  1271. fprintf(fp1,"%d %s %d %d %c %d",iccode1[i].add,iccode1[i].opcodeclass,iccode1[i].opcodevalue,iccode1[i].operand1code,iccode1[i].operand2class,iccode1[i].operand2value);
  1272. }
  1273. }
  1274. fclose(fp1);
  1275.  
  1276.  
  1277. fp1=fopen("symtab.txt","w");
  1278.  
  1279. if(fp1==NULL)
  1280. {
  1281.   printf("\ncann't open file\n");
  1282.   exit(0);
  1283. }
  1284.  
  1285. fp2=fopen("littab.txt","w");
  1286.  
  1287. if(fp2==NULL)
  1288. {
  1289.   printf("\ncann't open file\n");
  1290.   exit(0);
  1291. }
  1292.  
  1293. fp3=fopen("pooltab.txt","w");
  1294.  
  1295. if(fp3==NULL)
  1296. {
  1297.   printf("\ncann't open file\n");
  1298.   exit(0);
  1299. }
  1300.  
  1301. printf("\n\n\nS.No. Symbol   Address Length       Lit_no Literal Address       Pool table");
  1302. for (i=0;i<symtab_ptr||i<littab_ptr||i<pooltab_ptr;i++)
  1303. {
  1304. if(i<symtab_ptr)
  1305. {printf("\n%-5d %-10s %-5d %-15d",i+1,symtab1[i].sym,symtab1[i].addr,symtab1[i].len);
  1306. fprintf(fp1,"%d %s %d %d\n",i+1,symtab1[i].sym,symtab1[i].addr,symtab1[i].len);}
  1307. if(i<littab_ptr)
  1308. {printf("%-5d %-7s %-15d",i+1,littab1[i].lit,littab1[i].lit_add);
  1309. fprintf(fp2,"%d %s %d\n",i+1,littab1[i].lit,littab1[i].lit_add);}
  1310. if(i<pooltab_ptr)
  1311. {printf("%d",pooltab[i]);
  1312. fprintf(fp3,"%d\n",pooltab[i]);}
  1313. }
  1314.  
  1315. fclose(fp1);
  1316. fclose(fp2);
  1317. fclose(fp3);
  1318.  
  1319.  
  1320. getch();
  1321.  
  1322. }
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337. //PASS 2
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344. #include<stdio.h>
  1345. #include<stdlib.h>
  1346. #include<conio.h>
  1347. #include<string.h>
  1348. #include<fstream.h>
  1349.  
  1350. struct iccode2
  1351. {
  1352.   int add,op,op1,op2;
  1353. }ic[30];
  1354.  
  1355.  
  1356.  
  1357. void main()
  1358. {
  1359.   int ad,opc,op1,opr2,ln,ptr,addr,lth,i;
  1360.   char op[3],op2,lbl[10];
  1361.   FILE  *fp1,*fp2,*fp3,*fp4;
  1362.   clrscr();
  1363.  
  1364.   fp1=fopen("pass1.txt","r");
  1365.   if(fp1==NULL)
  1366.   {
  1367.     printf("\ncann't open file\n");
  1368.     exit(0);
  1369.   }
  1370.   fp2=fopen("symtab.txt","r");
  1371.   if(fp2==NULL)
  1372.   {
  1373.     printf("\ncann't open file\n");
  1374.     exit(0);
  1375.   }
  1376.   fp3=fopen("littab.txt","r");
  1377.   if(fp3==NULL)
  1378.   {
  1379.     printf("\ncann't open file\n");
  1380.     exit(0);
  1381.   }
  1382.   fp4=fopen("pooltab.txt","r");
  1383.   if(fp4==NULL)
  1384.   {
  1385.     printf("\ncann't open file\n");
  1386.     exit(0);
  1387.   }
  1388.  
  1389.  
  1390. /*
  1391. fp1=fopen("pass1.txt","r");
  1392.  
  1393. if(fp1==NULL)
  1394. {
  1395.   printf("\ncann't open file\n");
  1396.   exit(0);
  1397. }
  1398.  
  1399. int l;
  1400. char a[10],c;
  1401. while(fscanf(fp1,"%d %s %d %d %c %d",&i,a,&j,&k,&c,&l)!=EOF)
  1402. {
  1403. printf("\n%d (%s,%d) %d (%c,%d)",i,a,j,k,c,l);
  1404. }
  1405. fclose(fp1);
  1406. */
  1407.  
  1408.  
  1409.   ln=0;
  1410.  
  1411.   while(fscanf(fp1,"%d %s %d %d %c %d",&ad,op,&opc,&op1,&op2,&opr2)!=EOF)
  1412.   {
  1413.     ic[ln].add=ad;
  1414.     if(strcmp(op,"IS")==0)
  1415.      ic[ln].op=opc;
  1416.     else
  1417.      ic[ln].op=0;
  1418.  
  1419.     ic[ln].op1=op1;
  1420.  
  1421.     if(op2=='S')
  1422.     {
  1423.       fseek(fp2,0,SEEK_SET);
  1424.       while(fscanf(fp2,"%d %s %d %d",&ptr,lbl,&addr,&lth)!=EOF)
  1425.       {
  1426.     if(opr2==ptr)
  1427.     {
  1428.       ic[ln].op2=addr;
  1429.       break;
  1430.     }
  1431.       }
  1432.     }
  1433.     else if(op2=='L')
  1434.     {
  1435.       fseek(fp3,0,SEEK_SET);
  1436.       while(fscanf(fp3,"%d %s %d",&ptr,lbl,&addr)!=EOF)
  1437.       {
  1438.     if(opr2==ptr)
  1439.     {
  1440.       ic[ln].op2=addr;
  1441.       break;
  1442.     }
  1443.       }
  1444.  
  1445.     }
  1446.     else
  1447.       ic[ln].op2=opr2;
  1448.  
  1449.     ln++;
  1450.   }
  1451.  
  1452.  
  1453. fclose(fp1);
  1454. fclose(fp2);
  1455. fclose(fp3);
  1456. fclose(fp4);
  1457.  
  1458.  
  1459. printf("\nPASS 2:-\n\naddress  opcode  operand1  operand2\n");
  1460.  
  1461.  
  1462. fp1=fopen("pass2.txt","w");
  1463.  
  1464. if(fp1==NULL)
  1465. {
  1466.   printf("\ncann't open file\n");
  1467.   exit(0);
  1468. }
  1469.  
  1470. for(i=0;i<ln;i++)
  1471. {
  1472. printf("\n%-9d %-8d %-10d %-10d",ic[i].add,ic[i].op,ic[i].op1,ic[i].op2);
  1473. fprintf(fp1,"%d %d %d %d\n",ic[i].add,ic[i].op,ic[i].op1,ic[i].op2);
  1474. }
  1475. //fclose(fp1);
  1476.  
  1477.  
  1478. getch();
  1479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement