document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //----------------------------------------------------------
  2. // Title :  Design suitable data structures and implement
  3. //      pass-I of a two-pass macro-processor.
  4. //----------------------------------------------------------
  5.  
  6. //header file declaration
  7. #include<stdio.h>
  8. #include<conio.h>
  9. #include<string.h>
  10.  
  11. // global declaration of variables
  12. char line[100], line1[100], line2[100];
  13. // line = actual \'MACRO\' word , line1= MACRO name, line2= MACRO instruction
  14. int imnt=0;     // index for MNT (MNT = MACRO  NAME TABLE )
  15. int imdt=0; // index for MDT (MDT = MACRO DEFINATION TABLE)
  16. int iala=0;   // index for ALA (ALA= ARGUMENT LIST ARRAY)
  17.  
  18. // Function declaration
  19. void mntproc(char a1[50]);
  20. void alaproc(char a2[50]);
  21. void mdtproc(char ln1[50]);
  22.  
  23. // struct for MNT (macro name table)
  24. struct mnt
  25. {
  26.     int index;
  27.     char mname[50];
  28.     }n[50];
  29.  
  30. // struct for MDT (macro definition table)
  31. struct mdt
  32. {
  33.     char instr[50];
  34.     }d[50];
  35.  
  36. // struct for ALA (argument list array)
  37. struct ala
  38. {
  39.     char arg[50];
  40.     }a[50];
  41.  
  42. // main function
  43. void main()
  44. {
  45.     int i;
  46.     char s1[50], s2[50];
  47.     // s1 & S2 = first & Second symbol of line
  48.  
  49.     static const char filename[] = "asm.asm";
  50.     FILE *file = fopen ( filename, "r" ); // open asm file
  51.  
  52.     clrscr();
  53.  
  54.     if(file!=NULL)   // check every line of asm file still EOF
  55.     {
  56.         while(fgets(line,sizeof line,file)!=NULL) // fetching first line ( if we use while loop ,then fetching each line upto EOF)
  57.         {
  58.             sscanf(line,"%s",s1); // separate first string from line and insert into s1
  59.             if(strcmp(s1,"MACRO")==0)// check first string of first line is \'MACRO\' word or not, if YES then
  60.             {
  61.                 imnt++; // pre-increment the index of MNT
  62.                 fgets(line1,sizeof line1,file); // scan next line of the same file
  63.                 sscanf(line1,"%s%s",s1,s2); // separate MACRO NAME and arguments, insert it to the s1 & s2
  64.                 mntproc(s1); // pass s1 (MACRO NAME) to make entry in MNT
  65.                 alaproc(s2); // pass s2 (ARGUMENT LIST) to make entry in ALA
  66.                 }
  67.  
  68.             while(strcmp(s1,"MEND")!=0) // execute this loop untill we find \'MEND\' word
  69.             {
  70.                 imdt++; // pre-increment the index of MDT
  71.                 fgets(line2,sizeof line2,file); // scan next line of the same file to fetch the instructions of MACRO
  72.                 sscanf(line2,"%s%s",s1,s2); // separate INSTRUCIONS and its PARAMETER
  73.                 mdtproc(line2); // pass line2 (INSTRUCTIONS) to make entry in MDT
  74.                 }
  75.             }
  76.         }
  77.  
  78.     printf("\\n\\n------------------------------------------------------");
  79.     printf("\\n\\t\\t MNT TABLE"); // print MNT
  80.     printf("\\n------------------------------------------------------");
  81.     printf("\\nINDEX\\t\\tMACRO NAME\\tMDT INDEX");
  82.  
  83.     for(i=0;i<imnt;i++) // execute \'for loop\' upto index of MNT
  84.     {
  85.         printf("\\n%d\\t\\t%s\\t\\t%d",i+1,n[i+1].mname,n[i+1].index);
  86.         }
  87.  
  88.  
  89.     printf("\\n\\n------------------------------------------------------");
  90.     printf("\\n\\t\\t MDT TABLE");     // print MDT
  91.     printf("\\n------------------------------------------------------");
  92.     printf("\\nINDEX\\t\\tINSTRUCTIONS");
  93.  
  94.     for(i=0;i<imdt;i++) // execute \'for loop\' upto index of MDT
  95.     {
  96.         printf("\\n%d\\t\\t%s",i+1,d[i+1].instr);
  97.         }
  98.  
  99.  
  100.     printf("\\n\\n------------------------------------------------------");
  101.     printf("\\n\\t\\t ALA TABLE");     // print ALA
  102.     printf("\\n------------------------------------------------------");
  103.     printf("\\nINDEX\\t\\tARGUMENT LIST");
  104.  
  105.     for(i=0;i<iala;i++)         // execute \'for loop\' upto index of ALA
  106.     {
  107.         printf("\\n%d\\t\\t%s",i+1,a[i+1].arg);
  108.         }
  109.  
  110.     fclose(file); // close the above file that is asm file
  111.     getch();
  112.     }
  113.  
  114. // function for MNT
  115. void mntproc(char a1[50])
  116. {
  117.     strcpy(n[imnt].mname,a1); // copy a1 i.e. MACRO NAME in MNT
  118.     n[imnt].index=imdt+1; // increment index of MNT
  119.     }
  120.  
  121. // function for ALA
  122. void alaproc(char a2[50])
  123. {
  124.     iala++; // pre-increment index of ALA
  125.     strcpy(a[iala].arg,a2); // copy a2 i.e. ARGUMENTS in ALA
  126.     }
  127.  
  128. // function for MDT
  129. void mdtproc(char ln1[50])
  130. {
  131.     strcpy(d[imdt].instr,ln1); // copy l1 i.e. INSTRUCTIONS in MDT
  132.     }
  133.  
  134. // END OF THE PROGRAM
');