C Program for the Implementation of Pass One of a Two Pass Assembler | CS1207 - System Software Laboratory



AIM:
         To write a "C" program for the implementation of pass one of a two pass assembler in CS1207 - System Software Lab.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char opcode[10],operand[10],label[10],code[10][10],ch; char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};
 int locctr,start,len,i=0,j=0;
 FILE *fp1,*fp2,*fp3;
 clrscr();
 fp1=fopen("INPUT.DAT","r");
 fp2=fopen("SYMTAB.DAT","w");
 fp3=fopen("OUT.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);
  fcloseall();
  printf("\n\nThe contents of Input Table :\n\n");
  fp1=fopen("INPUT.DAT","r");
  ch=fgetc(fp1);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp1);
   }
  printf("\n\nThe contents of Output Table :\n\n\t");
  fp3=fopen("OUT.DAT","r");
  ch=fgetc(fp3);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp3);
   }
  len=locctr-start;
  printf("\nThe length of the program is %d.\n\n",len);
  printf("\n\nThe contents of Symbol Table :\n\n");
  fp2=fopen("SYMTAB.DAT","r");
  ch=fgetc(fp2);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp2);
   }
  fcloseall();
  getch();
}

INPUT FILE:

INPUT.DAT
** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
** END **

OUTPUT:
  


Related Posts:
  • IMPLEMENTATION OF PASS ONE OF A TWO PASS ASSEMBLER
Previous
Next Post »

2 comments

Write comments
Anonymous
AUTHOR
July 30, 2012 at 10:06 PM delete

can anyone explain what the concept of this program is??

Reply
avatar
ramu
AUTHOR
January 24, 2016 at 6:54 PM delete

Created input file but no output

Reply
avatar

Still not found what you are looking for? Try again here.