affiliate marketing

Saturday 17 December 2011

PASS1_OF_2_PASS_ASSEMBLER


IMPLEMENTATION OF PASS-1 OF TWO PASS ASSEMBLER
AIM
To write a C-program to implement pass-1 assembler ie object code.
ALGORITHM
1.Read first line from the input 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 find output files.
3.Start the following processing for other lines in input file if it is not a comment line until an “END” statement is reached.
4.Start writing label LOCCTR opcode and operand fields of corresponding statement to the output file along with object code.The object code is found by assembling each statement opcode machine equivalent with 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.Ip 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],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("SYMBTAB.DAT","r");
 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,"%s\t%s\t%s\n",label,opcode,operand);
    fprintf(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 tehe input table");
 fp1=fopen("INPUT.DAT","r");
 ch=fgetc(fp1);
 while(ch!=EOF)
  {
                printf("%c",ch);
                ch=fgetc(fp1);
  }
 printf("\n\nThe Contents of the Output table");
 fp1=fopen("OUT.DAT","r");
 ch=fgetc(fp3);
 while(ch!=EOF)
  {
                printf("%c",ch);
                ch=fgetc(fp3);
  }
 len=locctr-start;
 printf("\nLength of the program is %d\n\n",len);
 printf("\nContents  of the symbol table\n");
 fp2=fopen("SYMBTAB.DAT","r");
 ch=fgetc(fp2);
 while(ch!=EOF)
  {
                printf("%c",ch);
                ch=fgetc(fp2);
  }
 fcloseall();
 getch();
}

INPUT FILE:
** 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:


RESULT:
Thus pass-1 assembler has been implemented in C successfully.

1 comment: