affiliate marketing

Saturday, 17 December 2011

single pass assembler using C


IMPLEMENTATION OF SINGLE PASS ASSEMBLER
AIM
To implement single pass assembler using C.
ALGORITHM
1.Read first line from the intermediate file.
2.Check to see if the opcode from the first line read is “START”.If so then write label,opcode and operand field values of corresponding statement directly to final output files.
3.Start the following processing for other lines in intermediate file if it is not a comment line until an “END” statement is reached.
4.Start writing labels LOCCTR opcode and operand fields of corresponding statements to the output file along with the object code.The object code is found by assembling each statement opcode machine equivalent with the label address.
5.If there is no symbol or label in the operand field , then the operand address is assigned as zero and it is assembled with object code of instruction.
6.If OPCODE is BYTE,WORD,RESB etc are convert constants to object code close operand file and exit.


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
   char opcode[10],operand[10],label[10],a[10],ad[10],symbol[10],ch;
   char code[10][10],code1[10][10]={"33","44","53","57"};
   char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};
   char mnemonic1[10][10]={"LDA","STA","LDCH","STCH"};
   int locctr,start,length,i=0,j=0,k,l=0;
   int st,diff,address,add,len,actual_len,finaddr,prevaddr;
   FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7;
   clrscr();
   fp1=fopen("INPUT.DAT","r");
   fp2=fopen("SYMTAB.DAT","w");
   fp3=fopen("INTERMED.DAT","w");
   fscanf(fp1,"%s%s%s",label,opcode,operand);
   if(strcmp(opcode,"START")==0)
    {
                start=atoi(operand);
                locctr=start;
                fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
                fscanf(fp1,"%s%s%s",label,opcode,operand);
    }
   else
   locctr=0;
   while(strcmp(opcode,"END")!=0)
    {
                fprintf(fp3,"%d",locctr);
                if(strcmp(label,"**")!=0);
                fprintf(fp2,"%s\t%d\n",label,locctr);
                strcpy(code[i],mnemonic[j]);
                while(strcmp(mnemonic[j],"END")!=0)
                 {
                                if(strcmp(opcode,mnemonic[j])==0)
                                 {
                                                locctr+=3;
                                                break;
                                 }
                                strcpy(code[i],mnemonic[j]);
                                j++;
                 }
                if(strcmp(opcode,"WORD")==0)
                locctr+=3;
                else if(strcmp(opcode,"RESW")==0)
                locctr+=(3*(atoi(operand)));
                else if(strcmp(opcode,"RESB")==0)
                locctr+=(atoi(operand));
                else if(strcmp(opcode,"BYTE")==0)
                ++locctr;
                fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
                fscanf(fp1,"%s%s%s",label,opcode,operand);
    }
   fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
   length=locctr-start;
   fcloseall();
   printf("\n\nThe contents of the input file : \n\n");
   fp1=fopen("INPUTS.DAT","r");
   ch=fgetc(fp1);
   while(ch!=EOF)
    {
                printf("%c",ch);
                ch=fgetc(fp1);
    }
   printf("\n\nLength of the input program is %d",length);
   printf("\n\nThe contents of the symbol file : \n\n");
   fp2=fopen("SYMTABS.DAT","r");
   ch=fgetc(fp2);
   while(ch!=EOF)
    {
                printf("%c",ch);
                ch=fgetc(fp2);
    }
   fcloseall();
   fp4=fopen("ASSMLISTS.DAT","w");
   fp5=fopen("SYMTABS.DAT","r");
   fp6=fopen("INTERMEDS.DAT","r");
   fp7=fopen("OBJCODES.DAT","w");
   fscanf(fp6,"%s%s%s",label,opcode,operand);
   while(strcmp(opcode,"END")!=0)
    {
                prevaddr=address;
                fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
    }
   finaddr=address;
   fclose(fp6);
   fp6=fopen("INTERMEDS.DAT","r");
   fscanf(fp6,"%s%s%s",label,opcode,operand);
   if(strcmp(opcode,"START")==0)
    {
                fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);
                fprintf(fp7,"H^%s^00%s^00%d\n",label,operand,finaddr);
                fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
                st=address;
                diff=prevaddr-st;
                fprintf(fp7,"T^00%d^%d",address,diff);
    }
   while(strcmp(opcode,"END")!=0)
    {
                if(strcmp(opcode,"BYTE")==0)
                 {
                                fprintf(fp4,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
                                len=strlen(operand);
                                actual_len=len-3;
                                fprintf(fp7,"^");
                                for(k=2;k<(actual_len+2);k++)
                                 {
                                  itoa(operand[k],ad,16);
                                  fprintf(fp4,"%s",ad);
                                  fprintf(fp7,"%s",ad);
                                 }
                                fprintf(fp4,"\n");
                }
       else if(strcmp(opcode,"WORD")==0)
                {
                 len=strlen(operand);
                 itoa(atoi(operand),a,10);
                 fprintf(fp4,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
                 fprintf(fp7,"^00000%s",a);
                }
       else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))
       fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
       else
    {
   while(strcmp(operand,mnemonic1[l])!=0)
   l++;
   if(strcmp(operand,"COPY")==0)
   fprintf(fp4,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code1[l]);
   else
    {
                rewind(fp5);
                fscanf(fp5,"%s%d",symbol,&add);
                while(strcmp(operand,symbol)!=0)
                fscanf(fp5,"%s%d",symbol,&add);
                fprintf(fp4,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code1[l],add);
                fprintf(fp7,"^%s%d",code1[l],add);
    }
  }
 fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
fprintf(fp7,"\nE^00%d",st);
printf("\nObject program has been generated . ");
fcloseall();
printf("\n\nObject program : \n\n");
fp7=fopen("OBJCODES.DAT","r");
ch=fgetc(fp7);
while(ch!=EOF)
 {
   printf("%c",ch);
   ch=fgetc(fp7);
 }
fcloseall();
getch();
}
INPUT FILE:
INPUT.DAT
COPY START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'EOF'
C1 RESB 1
** END **







OUTPUT:



RESULT
Thus single pass assembler has been successfully implemented using C.

1 comment: