document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //----------------------------------------------------------
  2. // Title : Implement pass-II of a two-pass assembler for
  3. //     8-bit microprocessor/ pseudo-machine. The
  4. //         output of PASS-1 (intermediate file and
  5. //     symbol table) should be input for this assignment.
  6. //----------------------------------------------------------
  7.  
  8. //header file declaration
  9. #include<stdio.h>
  10. #include<conio.h>
  11. #include<string.h>
  12.  
  13. // global declaration of variables
  14. char line[100]; // line ( intermediate file )
  15. int oindex=0; // index for object code
  16.  
  17. // struct for object code table
  18. struct objcode
  19. {
  20.     int add; // address field
  21.     char hexcode[20]; // hexcode field
  22.     int  op1, op2;   // op1 = operand1, operand2
  23.     }o[50]; // array for opject code file
  24.  
  25. // functions declatations
  26. void opid(char aaa1[50],char aaa2[50]); // aaa1 = for op1, and aaa2 = for op2
  27. void opcproc(char aa[50]);
  28. void symopen(char sym[20]);
  29. void litopen(char lit[20]);
  30.  
  31. // function for inserting the value of op1 & op2 in object code struct
  32. void opid(char aaa1[50],char aaa2[50])
  33. {
  34.     int i;
  35.  
  36.     if(strcmp(aaa1,"AREG")==0) // if op1 = AREG then insert \'1\' in struct of obj table
  37.     {
  38.         o[oindex].op1=1;
  39.         }
  40.     else if(strcmp(aaa1,"BREG")==0) // if op1 = BREG then insert \'2\' in struct of obj table
  41.     {
  42.         o[oindex].op1=2;
  43.         }
  44.     else if(strcmp(aaa1,"CREG")==0) // if op1 = CREG then insert \'3\' in struct of obj table
  45.     {
  46.         o[oindex].op1=3;
  47.         }
  48.     else if(strcmp(aaa1,"DREG")==0) // if op1 = DREG then insert \'4\' in struct of obj table
  49.     {
  50.         o[oindex].op1=4;
  51.         }
  52.     else
  53.     {
  54.         symopen(aaa1);  // if op1 is not regi. then call to fn for symbol check processing
  55.         }
  56.  
  57.     if(strcmp(aaa2,"AREG")==0) // if op2 = AREG then insert \'1\' in struct of obj table
  58.     {
  59.         o[oindex].op2=1;
  60.         }
  61.     else if(strcmp(aaa2,"BREG")==0) // if op2 = BREG then insert \'2\' in struct of obj table
  62.     {
  63.         o[oindex].op2=2;
  64.         }
  65.     else if(strcmp(aaa2,"CREG")==0) // if op2 = CREG then insert \'3\' in struct of obj table
  66.     {
  67.         o[oindex].op2=3;
  68.         }
  69.     else if(strcmp(aaa2,"DREG")==0) // if op2 = DREG then insert \'4\' in struct of obj table
  70.     {
  71.         o[oindex].op2=4;
  72.         }
  73.     else if(aaa2[0]==\'=\')   // IF OP2 IS LITERAL, then call to fn for literal check processing
  74.     {
  75.         litopen(aaa2);
  76.         }
  77.     else
  78.     {
  79.         symopen(aaa2); // if op2 is not regi. then call to fn for symbol check processing
  80.         }
  81.     }
  82.  
  83. // function for literal check processing
  84. void litopen(char lit[20])
  85. {
  86.     int i, j;
  87.     char tmp2[50];
  88.     char line1[200]; // line1 = each line of literal file
  89.     char s1[100], s2[100]; // s1=first string of literal file
  90.     // s2=second string of literal file
  91.  
  92.  
  93.     static const char filename1[] = "l2.txt";
  94.     FILE *file1 = fopen ( filename1, "r" ); // open literal file
  95.  
  96.     if(file1!=NULL)   // check every line of LITERAL file still EOF
  97.     {
  98.         while(fgets(line1,sizeof line1,file1)!=NULL) // fetching first line ( if we use while loop ,then fetching each line upto EOF)
  99.         {
  100.             sscanf(line1,"%s%s",s1,s2); // separate first & second string from line and insert into s1 & s2
  101.  
  102.             i=0;  // initialize i=0 to maintain of tmp2
  103.             for(j=2; j<(strlen(lit)-1); j++)  // execute this loop from position 2 to string lengh minus 1 ( to avoid last char)
  104.             {
  105.                 tmp2[i]=lit[j]; // copy the literal value to tmp2
  106.                 i++;
  107.                 }
  108.             tmp2[i]=\'\\0\'; // to avoid garbage value , insert null char
  109.  
  110.             if(strcmp(s1,tmp2)==0) // if first string of literal file and literal of IC file is equal
  111.             {
  112.                 o[oindex].op2=atoi(s2); // convert address of resp. literal and store in struct of obj file
  113.                 }
  114.             }
  115.         }
  116.  
  117.     fclose(file1); // close the above file that is literal file
  118.     }
  119.  
  120. // function for symbol check processing
  121. void symopen(char sym[20])
  122. {
  123.     char line2[200];
  124.     char s1[100], s2[100], s3[100];// s1=first string of symbol file
  125.     // s2=second string of symbol file,  s3=third string of symbol file
  126.  
  127.     static const char filename2[] = "s2.txt";
  128.     FILE *file2 = fopen ( filename2, "r" ); // open symbol file
  129.  
  130.     if(file2!=NULL)   // check every line of IC file still EOF
  131.     {
  132.         while(fgets(line2,sizeof line2,file2)!=NULL) // fetching first line ( if we use while loop ,then fetching each line upto EOF)
  133.         {
  134.             sscanf(line2,"%s%s%s",s1,s2,s3); // separate first, second and third string from line and insert into s1 ,s2 and s3
  135.             if(strcmp(s1,sym)==0) // is first string of symbol table and symbol of IC file is equal
  136.             {
  137.                 if(strcmp(sym,"AGAIN")==0) // if symbol is \'AGAIN\' then to avoid NULL values insert its value to op1
  138.                 {
  139.                     o[oindex].op1=atoi(s3); // conversion of string to int.
  140.                     }
  141.                 else // if symbol is NOT \'AGAIN\' then insert its value to op2
  142.                 {
  143.                     o[oindex].op2=atoi(s3); // conversion of string to int.
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.     fclose(file2); // close the above file that is SYMBOL file
  149.     }
  150.  
  151. // this function is to separate the third string (operands) into op1 and op2
  152. void opcproc(char aa[50])
  153. {
  154.     char op1[20], op2[20]; // op1= operand1 and op2=operand2
  155.     int flag3=0;      // to maintain the index of op1 we intialize it to \'zero\'
  156.     int i,j=0;
  157.  
  158.     for(i=0; i<strlen(aa); i++) // execute upto the length of array \'aa\' (operand)
  159.     {
  160.         if(aa[i] == \',\') // if separator found then copy the remaining string to string2 i.e. op2 with the help of while loop
  161.         {
  162.             i++; // pre-increment to skip the separator
  163.             while(i<strlen(aa))
  164.             {
  165.                 op2[j]=aa[i];
  166.                 i++;
  167.                 j++;
  168.                 }
  169.             op2[j]=\'\\0\'; // to avoid garbage value , insert null char
  170.             break; // use for checking the all characher in op2
  171.             }
  172.         else if(aa[i] == \' \') // if space is found then copy string to string1 i.e. op1 with the help of while loop
  173.         {
  174.             while(i<strlen(aa))
  175.             {
  176.                 op1[i]=aa[i];
  177.                 i++;
  178.                 }
  179.             op1[i]=\'\\0\'; // to avoid garbage value , insert null char
  180.             break; // use for checking the all characher in op2
  181.             }
  182.         else
  183.         {
  184.             op1[i]=aa[i]; // copy the whole string \'aa\' in op1
  185.             flag3++;
  186.             }
  187.         }
  188.     op1[flag3]=\'\\0\'; // use for checking the all characher in op1
  189.     opid(op1,op2); // pass the op1 and op2 to fn \'opid\' to inserting particular value
  190.     }
  191.  
  192. // main function
  193. int main()
  194. {
  195.     int i, ij;
  196.     char s1[100], s2[100], s3[100];  // s1=first string of IC file
  197.     // s2=second string of IC file,  s3=third string of IC file
  198.  
  199.     static const char filename[] = "i2.txt";
  200.     FILE *file = fopen ( filename, "r" ); // open intermediate file
  201.  
  202.     clrscr();
  203.  
  204.     if(file!=NULL)   // check every line of IC file still EOF
  205.     {
  206.  
  207.         while(fgets(line,sizeof line,file)!=NULL) // fetching first line ( if we use while loop ,then fetching each line upto EOF)
  208.         {
  209.             sscanf(line,"%s%s%s",s1,s2,s3); // separate first, second and third string from line and insert into s1 ,s2 and s3
  210.             oindex++; // pre-increment the index of the object table
  211.             o[oindex].add=atoi(s1); // insert address of IC file into address field of obj file
  212.             strcpy(o[oindex].hexcode,s2); // insert hexcode of IC file into hexcode code field of obj file
  213.             opcproc(s3); // call this fn to separate the operands
  214.             }
  215.         }
  216.  
  217.     // print the Object Table
  218.     printf("\\n---------------------------------------------------------");
  219.     printf("\\n\\t\\t OBJECT TABLE");
  220.     printf("\\n---------------------------------------------------------");
  221.     printf("\\nADDRESS\\t\\tHEXCODE\\t\\tOPERAND1\\tOPERAND2");
  222.     for(ij=1;ij<=oindex;ij++)       // execute \'for loop\' upto index of object table
  223.     {
  224.         printf("\\n%d\\t\\t%s\\t\\t%d\\t\\t%d\\t",o[ij].add,o[ij].hexcode,o[ij].op1,o[ij].op2);
  225.         }
  226.     printf("\\n---------------------------------------------------------");
  227.  
  228.     fclose(file); // close the above file that is IC file
  229.  
  230.     getch();
  231.     return 0;
  232.     }
  233.  
  234. // end of the program
');