document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //----------------------------------------------------------
  2. // Title :  Design suitable data structures and implement pass-I
  3. //  of a two-pass assembler for 8 bit microprocessor/
  4. //  pseudo-machine. Implementation should consist of a few
  5. //  instructions from each category and few assembler directives
  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 ( asm file )
  15. int lc,fck=0, flagl=0;   // lc=location counter , fck = work as flag, flagl=flag for label
  16. int indexir=0;  // index for intermadiate code
  17. int indexsym=0; // index for symbol table
  18. int mindex=0;   // index for mnemonic table
  19. int lindex=0;   // index for litral table
  20.  
  21. // struct for literal table
  22. struct littable
  23. {
  24.     int add, val;
  25.     }l[50];
  26.  
  27. // struct for symbol table
  28. struct symtable
  29. {
  30.     char sname[50];
  31.     int add, length1;
  32.     }s[50];
  33.  
  34. // struct for mnemonic opcode table
  35. struct mot
  36. {
  37.     int addr, len;
  38.     char code[20], hexcode[20];
  39.     }m[50];
  40.  
  41. //struct for Intermediate code
  42. struct ir
  43. {
  44.     int add;
  45.     char hexcode[50];
  46.     char op[50];
  47.     }ic[50];
  48.  
  49. // struct for pseudo opcode table
  50. struct pseudo
  51. {
  52.     char inst;
  53.     int length;
  54.     };
  55.  
  56. // functions declatations
  57. void opid(char aaa[50]);
  58. void opcproc(char aa[50]);
  59. void chekps(char a[20]);
  60. void mneopchk(char b[20]);
  61. void psuproc();
  62.  
  63. // this function is use to check whether operand is register. or not and making entry in symbol table if it is not register.
  64. void opid(char aaa[50])
  65. {       // aaa[50]= it contains op1 or op2
  66.     int i;
  67.  
  68.     if(strcmp(aaa,"AREG")==0)
  69.     {
  70.     //  printf("AREG..!");
  71.         }
  72.     else if(strcmp(aaa,"BREG")==0)
  73.     {
  74.     //      printf("BREG..!");
  75.         }
  76.     else if(strcmp(aaa,"CREG")==0)
  77.     {
  78.     //  printf("CREG..!");
  79.         }
  80.     else if(strcmp(aaa,"DREG")==0)
  81.     {
  82.     //  printf("DREG..!");
  83.         }
  84.     else if(aaa[0]==\'=\')     //if op2 is literal then check whether first character of string2 i.e. op2 is \'=\' or not if it is present then
  85.     {
  86.         l[lindex].val=aaa[2];   // put that literal value in index of literal table
  87.         lindex++; // increment the index of literal table
  88.         }
  89.  
  90.     else
  91.     {
  92.         for(i=0;i=indexsym;i++) // use \'for loop\' upto index of symbol table that is last entry of the symbol table
  93.         {
  94.             if(strcmp(s[i].sname,aaa)==0) // checking the entry of operand in symbol table
  95.             {
  96.                 // if it is already exists then break this condn
  97.                 break;
  98.                 }
  99.             else
  100.             {
  101.                 indexsym++; // increment the index of symbol table
  102.                 strcpy(s[indexsym].sname,aaa); // create new entry for operand in symbol table
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.  
  109. // this function is use for symbol checking
  110. void symchk()
  111. {
  112.     int i, templen1;
  113.     char label[10], opc[10], field[50];
  114.     // label=label, opc=opcode, field = operands
  115.  
  116.     sscanf(line,"%s%s%s",label,opc,field); // split the line into label, opc, field
  117.  
  118.     flagl=1;    // assign flagl=1 if label is present.
  119.  
  120.     chekps(opc); // case 2 : label \'opcode\' op1,op2  AND AGAIN check the \'opc\' with pseudo and mnemonic opcode and if it is not there then again return to symchk fn.
  121.  
  122.     if(fck==1) // if flag=1 i.e. if it is pseudo code then
  123.     {
  124.         indexsym++; // preincrement the index of symbol table
  125.         fck=0;      // first load 0 in flag \'fck\'
  126.         opcproc(field);  // pass the operands present in \'field\' to opcode process
  127.         flagl=0;   // again set flagl=0 for checking the label
  128.  
  129.         strcpy(s[indexsym].sname,label); // copy label (name) into \'sname\' that is, this is new entry for symbol table
  130.         s[indexsym].add=lc-m[mindex-1].len; // decrement the value of lc to get the length of previous instruction
  131.         s[indexsym].length1=1;  // set the length of label = 1
  132.         }
  133.     }
  134.  
  135. // this funtion is used to separate the operands (into op1 and op2)
  136. void opcproc(char aa[50])
  137. {
  138.     char op1[20], op2[20];
  139.     int flag3=0;
  140.     int i,j=0;
  141.  
  142.     strcpy(ic[indexir].op,aa); // create entry for operand in intermediate code table
  143.     indexir++; // increment the index of intermediate code
  144.  
  145.     for(i=0; i<strlen(aa); i++)   // execute upto the length of array \'aa\' (operand)
  146.     {
  147.         if(aa[i] == \',\')    // if separator found then copy remaining string to string2 i.e. op2 with the help of while loop
  148.         {
  149.             i++;
  150.             while(i<strlen(aa))
  151.             {
  152.                 op2[j]=aa[i];
  153.                 i++;
  154.                 j++;
  155.                 }
  156.             op2[j]=\'\\0\'; // to avoid garbage value , insert null char
  157.             opid(op2); // call this fn for checking whether op2 is regi. or not
  158.             break; // use for checking the all character in op2
  159.             }
  160.         else if(aa[i] == \' \')     // if space is found then copy string to string1 i.e. op1 with the help of while loop
  161.         {
  162.             while(i<strlen(aa))
  163.             {
  164.                 op1[i]=aa[i];
  165.                 i++;
  166.                 }
  167.             op1[i]=\'\\0\';  // to avoid garbage value , insert null char
  168.             opid(op1); // call this fn for checking whether op2 is regi. or not
  169.             break; // use for checking the all character in op2
  170.             }
  171.         else
  172.         {
  173.             op1[i]=aa[i]; // copy the whole string \'aa\' in op1
  174.             flag3++;
  175.             }
  176.         }
  177.     op1[flag3]=\'\\0\';     // use for checking the all character in op2
  178.     }
  179.  
  180. //this function is use for pseudo processing
  181. void psuproc()
  182. {
  183.     char ps[20]="START";      // declare ps as \'START\'
  184.     char s2[50], s3[20], s4[20];  // declare s2, s3 and s4 for first , second and third string of each line
  185.     int i,a,flag2=0;
  186.  
  187.     sscanf(line,"%s%s",s2,s3);    // separate the first and second string from line and insert into s2 and s3
  188.  
  189.     if(strcmp(s2,ps)==0)    // compare first string with ps i.e. \'START\' and if AFTER START \'DC\' OR ANY OTHER STRING IS PRESENT THEN error occurs ( here we are not handling the error )
  190.     {
  191.         lc=atoi(s3);    // convert length (string) to integer
  192.                 // if \'START\' is found then second string after \'START\' si compulsory \'lc\'
  193.                 // load the value as initial value of \'lc\'
  194.         }
  195.  
  196.     else if(strcmp(s3,"DS")==0) // check whether second string is DS or not
  197.     {
  198.         sscanf(line,"%s%s%s",s2,s3,s4); // separate first, second and third string from line and insert it into s2, s3 and s4
  199.  
  200.         for(i=0;i<=indexsym;i++) // use \'for loop\' upto index of symbol table that is last entry of the symbol table
  201.         {
  202.             if(strcmp(s2,s[i].sname)==0) // check whether first string \'s2\' is already in the symbol table or not
  203.             {
  204.                 flag2=1; // if flag2=1 then label is already present in symbol table
  205.                 s[i].add=lc; // load value of lc into address field
  206.                 a=atoi(s4); // convert third string \'s4\' into integer
  207.                 a=a*4;      // multiply value of \'a\'*4 to convert word into byte
  208.                 s[i].length1=a;     // load the value of \'a\' into length1 in symbol table
  209.                 lc=lc+a; // load the new value of lc by adding value of \'a\' in the old value of \'lc\'
  210.                 fck=0;  // if DS or DC is processed , label should not be re-entered in the symbol table
  211.                 break;
  212.                 }
  213.             }
  214.  
  215.         if(flag2==0) // if flag2=0 it is not found in symbol table thats why we create new entry for symbol table
  216.         {
  217.             indexsym++;  // before entry, increment the index of the symbol table
  218.             strcpy(s[indexsym].sname,s2); // check whether first string \'s2\' is already in the symbol table or not
  219.             s[indexsym].add=lc;       // load value of lc in address field of symbol table
  220.             a=atoi(s4);   // convert operand (string ) to integer
  221.             a=a*4;        // conversion of word to byte ( * 4 )
  222.             s[indexsym].length1=a;      // load value of \'a\' in length field of symbol table
  223.             lc=lc+a;  // update old value of lc with previous value of \'a\'
  224.             fck=0;  // if DS or DC is processed , label should not be re-entered in the symbol table
  225.             }
  226.         }
  227.  
  228.     // for DC , logic is same as above
  229.     else if(strcmp(s3,"DC")==0) // check whether second string is DS or not
  230.     {
  231.         sscanf(line,"%s%s%s",s2,s3,s4); // separate first, second and third string from line and insert it into s2, s3 and s4
  232.  
  233.         for(i=0;i<=indexsym;i++) // use \'for loop\' upto index of symbol table that is last entry of the symbol table
  234.         {
  235.             if(strcmp(s2,s[i].sname)==0) // check whether first string \'s2\' is already in the symbol table or not
  236.             {
  237.                 flag2=1; // if flag2=1 then label is already present in symbol table
  238.                 s[i].add=lc; // load value of lc into address field
  239.                 s[i].length1=4;       // \'load by default 4\' in length field
  240.                 lc=lc+4 ; // update value of lc \'by adding 4\'
  241.                 fck=0;  // if DS or DC is processed , label should not be re-entered in the symbol table
  242.                 break;
  243.                 }
  244.             }
  245.  
  246.  
  247.         if(flag2==0)
  248.         {
  249.             indexsym++;  // before entry, increment the index of the symbol table
  250.             strcpy(s[indexsym].sname,s2);   // check whether first string \'s2\' is already in the symbol table or not
  251.             s[indexsym].add=lc;     // load value of lc into address field
  252.             s[indexsym].length1=4;  // load value \'4\' in length field of symbol table
  253.             lc=lc+4;  // update lc with adding \'4\' with old value of \'lc\'
  254.             fck=0;  // if DS or DC is processed , label should not be re-entered in the symbol table
  255.             }
  256.         }
  257.  
  258.     else if(strcmp(s2,"END")==0) // if END is found print all the data present is the table
  259.     {
  260.         printf("\\n\\n------------------------------------------------------");
  261.         printf("\\n\\t\\t MNEMONIC TABLE");    // print the Mnemonic Table
  262.         printf("\\n------------------------------------------------------");
  263.         printf("\\nOPCODE\\t\\tHEX CODE\\tLENGTH\\t\\tADDRESS");
  264.         for(i=0;i<mindex;i++)         // execute \'for loop\' upto index of mnemonic table
  265.         {
  266.             printf("\\n%s\\t\\t%s\\t\\t%d\\t\\t%d",m[i].code,m[i].hexcode,m[i].len,m[i].addr);
  267.             }
  268.  
  269.         printf("\\n\\n------------------------------------------------------");
  270.         printf("\\n\\t\\t SYMBOL TABLE");      // print the Symbol Table
  271.         printf("\\n------------------------------------------------------");
  272.         printf("\\nSYMBOL\\t\\tLENGTH\\t\\tADDRESS");
  273.         for(i=1;i<=indexsym;i++)       // execute \'for loop\' upto index of symbol table
  274.         {
  275.             printf("\\n%s\\t\\t%d\\t\\t%d\\t",s[i].sname,s[i].length1,s[i].add);
  276.             }
  277.  
  278.         printf("\\n\\n------------------------------------------------------");
  279.         printf("\\n\\t\\t LITERAL TABLE ");     // print the Literal Code
  280.         printf("\\n------------------------------------------------------");
  281.         printf("\\nVALUE\\t\\tADDRESS");
  282.         for(i=0;i<lindex;i++)         // execute \'for loop\' upto index of Literal table
  283.         {
  284.             l[i].add=lc;        // load the value of \'lc\' in address field in literal table
  285.             lc=lc+4;        // update the value of \'lc\' by adding 4 in old value
  286.             printf("\\n%c\\t\\t%d\\t",l[i].val,l[i].add); // print the actual value and address of the literal
  287.             }
  288.  
  289.         printf("\\n\\n------------------------------------------------------");
  290.         printf("\\n\\t\\t INTERMEDIATE CODE "); // print the Intermediate Table
  291.         printf("\\n------------------------------------------------------");
  292.         printf("\\nADDRESS\\t\\tHEX CODE\\tOPREANDS");
  293.         for(i=0;i<indexir;i++)  // execute \'for loop\' upto index of Intermediate Code
  294.         {
  295.             printf("\\n%d\\t\\t%s\\t\\t%s\\t",ic[i].add,ic[i].hexcode,ic[i].op);
  296.             }
  297.         }
  298.     }
  299.  
  300. // main function
  301. int main()
  302. {
  303.     int i;
  304.     char s1[100];  // s1=first symbol of first file
  305.  
  306.     static const char filename[] = "asm.asm";
  307.     FILE *file = fopen ( filename, "r" ); // open asm file
  308.  
  309.     clrscr();
  310.  
  311.     if(file!=NULL)   // check every line of asm file still EOF
  312.     {
  313.         while(fgets(line,sizeof line,file)!=NULL) // fetching first line ( if we use while loop ,then fetching each line upto EOF)
  314.         {
  315.             sscanf(line,"%s",s1); // separate first string from line and insert into s1
  316.             chekps(s1); // pass string s1 to check whether it is in pseudo code file or Mnemonic code file and if it is not in both table then it is in symbol table
  317.             }
  318.         }
  319.  
  320.     fclose(file); // close the above file that is asm file
  321.     getch();
  322.     return 0;
  323.     }
  324.  
  325. // this fn is used for checking the string is present in Pseudo table or not
  326. void chekps(char a[20])
  327. {
  328.     char line1[100], ss1[20];  // line1= just for fetching the line from pseudo code file
  329.     // ss1[20]= it contain the first string of the line present in pseudo code
  330.     // a[20]= it contain the first string of the line in asm file
  331.  
  332.     int flag=0;
  333.  
  334.     static const char filename1[] = "pseudo.txt";
  335.     FILE *file1 = fopen ( filename1, "r" ); // open pseudo code file
  336.  
  337.     while(fgets(line1,sizeof line1,file1)!=NULL)   // fetching the line from pseudo code still EOF (if we use while loop ,then fetching each line upto EOF)
  338.     {
  339.         sscanf(line1,"%s",ss1);    // separate the first string of line from pseudo code file and insert in \'ss1\'
  340.         if(strcmp(a,ss1)==0)       // checking \'ss1\'(string from pseudo code file) with \'a\'(string from asm file)
  341.         {
  342.             flag=1; // if strings are matched the flag=1
  343.             psuproc(); // call this function for checking whether the string is START, DC, DS, and END or any other.
  344.             fclose(file1); // close the pseudo code file
  345.             }
  346.         }
  347.  
  348.     if(flag==0) // if string is not found in pseudo code table ( POT ) then call to mnemonic function (MOT)
  349.     {
  350.         mneopchk(a);  // pass the string to check whether it is mnemonic or not
  351.         fclose(file1); // close the pseudo code file
  352.         }
  353.     }
  354.  
  355. // this fn is used for checking the string is present in mnemonic table or not
  356. void mneopchk(char b[20])
  357. {
  358.     char line2[100], ss1[20], clen[20], opr[50];
  359.       // line2= just for fetching the line from mnemonic file
  360.       // clen= store the length
  361.     int flag1=0;
  362.  
  363.     static const char filename2[] = "mnemonic.txt";
  364.     FILE *file2 = fopen ( filename2, "r" ); //open mnemonic opcode file
  365.  
  366.     while(fgets(line2,sizeof line2,file2)!=NULL)  // fetching the line from mnemonic code still EOF ( if we use while loop ,then fetching each line upto EOF)
  367.     {
  368.         sscanf(line2,"%s",ss1);    // separate the first string of line from mnemonic code file and insert in \'ss2\'
  369.  
  370.         if(strcmp(b,ss1)==0)       // checking \'ss1\'(string from mnemonic code file) with \'b\'(string from asm file)
  371.         {
  372.             flag1=1;       // if match found then flag1=1
  373.             fck=1;//  declare this because to create entry for label after processing mnemonic and pseudo code
  374.             sscanf(line2,"%s%s%s",m[mindex].code,clen,m[mindex].hexcode);
  375.             //insert name in \'code\' , length in \'clen\' and hexcode in \'hexcode\'
  376.             m[mindex].addr=lc; // load value of lc to \'address field of mnemonic table\'
  377.             m[mindex].len=atoi(clen) ;  // first convert the length from char array to integer  and then load it into \'len\'
  378.             ic[indexir].add=lc;  // load the value of lc to \'intermediate code\'
  379.             strcpy(ic[indexir].hexcode,m[mindex].hexcode);  // create entry for hexcode in intermediate code i.e.
  380.             // copy the hexcode from MOT to IC
  381.             lc=lc+m[mindex].len; // increment the value of lc by adding old value of lc + length of the particular index
  382.             mindex++ ; // increment the index
  383.  
  384.             if(flagl==0) // if string is not mnemonic then given string is operands ( op1,op2)
  385.             {
  386.                 sscanf(line,"%s %s",ss1,opr); // separate the operands from the given line and inset into \'opr\'
  387.                 opcproc(opr); // call this function to separate the opearands (opr) into op1 and op2
  388.                 }
  389.             fclose(file2); // close the mnemonic code file
  390.             }
  391.         }
  392.  
  393.     if(flag1==0) // if string is not found in \'pseudo\' and \'mnemonic\' then it is a symbol
  394.     {
  395.         symchk(b);  // pass the first string of asm file to create entry for symbol table or updating.
  396.         fclose(file2);  // close the mnemonic code file
  397.         }
  398.     }
  399.  
  400. // end of the program
');