Pass 2 of SIC Assembler(updated for gcc)

C Program for Pass 2 of SIC Assembler
Note:- first run pass1 of SIC Assembler program in the blog
Output- object.txt
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int main()
{
 FILE *fint,*ftab,*flen,*fsym,*fout;
 int c,op1[10],txtlen,txtlen1,i,j=0,x,len,txtstart;
 char add[5],starttemp[5],txtrec[70]={'\0'},s[70],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],symtab[10],opmne[10];

 fint=fopen("interm.txt","r");
 flen=fopen("length.txt","r");
 ftab=fopen("optab.txt","r");
 fsym=fopen("symtab.txt","r");
 fout=fopen("objectcode.txt","w");
 
 fscanf(fint,"%s%s%s%s",add,label,mne,operand);
 if(strcmp(mne,"START")==0)
 {
  strcpy(start,operand);
  fscanf(flen,"%d",&len);
 }
 strcpy(starttemp,start);
 fprintf(fout,"H%6s%6s%06d\n",label,start,len);
 fscanf(fint,"%s%s%s%s",add,label,mne,operand);
 strcat(txtrec,"T00");
 txtstart=atoi(start);
 strcat(txtrec,start);
 strcat(txtrec,"00");
 c=8;

 while(strcmp(mne,"END")!=0)
 {
  fscanf(ftab,"%s%s",opmne,op);
  while(!feof(ftab))
  {
   if(strcmp(mne,opmne)==0)
   {
    fclose(ftab);
    fscanf(fsym,"%s%s",symadd,symtab);
    while(!feof(fsym))
    {
     if(strcmp(operand,symtab)==0)
     {       fclose(fsym);
      strcat(txtrec,op);
      strcat(txtrec,symadd);

      c=c+6;
      break;
     }
     else
      fscanf(fsym,"%s%s",symadd,symtab);
    }
    break;
   }
   else
    fscanf(ftab,"%s%s",opmne,op);
  }
  if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
  {
   if(strcmp(mne,"WORD")==0)
   {
        len=strlen(operand);
        for(j=0;j<(6-len);j++)
        strcat(txtrec,"0");
        strcat(txtrec,operand);
         c=c+6;
   }
   else
   {
    len=strlen(operand);
    for(i=2;i<len;i++)
    {
     x=toascii(operand[i]);
     sprintf(s,"%d",x);
     strcat(txtrec,s);
    }
    c=c+len*2;

   }
  }
  if(c==68)
  {
   x=(c-9)/2;
   x++;
   sprintf(s,"%d",x);
   txtrec[7]=s[0];
   txtrec[8]=s[1];
   fprintf(fout,"%s\n",txtrec);
   strcpy(txtrec,"\0");
   strcat(txtrec,"T00");
   txtstart=txtstart+x;
   sprintf(s,"%d",txtstart);
   strcat(txtrec,s);
   strcat(txtrec,"00");
   c=8;
  }

  fscanf(fint,"%s%s%s%s",add,label,mne,operand);
  ftab=fopen("optab.txt","r");
  fsym=fopen("symtab.txt","r");
  fseek(fsym,SEEK_SET,0);
  fseek(ftab,SEEK_SET,0);
 }
 x=(c-9)/2;
 x++;
 sprintf(s,"%d",x);
 txtrec[7]=s[0];
 txtrec[8]=s[1];
 fprintf(fout,"%s\n",txtrec) ;
 fprintf(fout,"E00%s",start);
 fclose(fint);

 
 fclose(fout);
 fclose(flen);
 printf("object code is generated. check objectcode.txt file\n");


       
}
Output
Objectc.txt
H  copy  1000000061
T001000304410481810531C10550C10580010481810531C10550C1058001048181053
T001030281C10550C10580010481810531C10550C10587576786769000005
E001000

No comments:

Post a Comment