Thursday 10 November 2011

design of two pass assemblers

A two pass assembler does two passes over the source file ( the second pass can be over a file generated in the first pass ). In the first pass all it does is looks for label definitions and introduces them in the symbol table. In the second pass, after the symbol table is complete, it does the actual assembly by translating the operations and so on.
The classical two-pass assembly process is adapted for implementation in a functional programming language similar to Backus' FP language. We show that the parallelism inherent in FP languages allows radical changes in the design of such well-known algorithms as the two-pass assembler.
-------------------
//Pass 1 of 2 pass assembler
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char opcode[10],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen(“INPUT.DAT”,”r”);
fp2=fopen(“SYMTB1.DAT”,”w”);
fp3=fopen(“OUT.DAT”,”w”);
fp4=fopen(“OPTAB.DAT”,”r”);
fscanf(fp1,”%s%s%s”,label,opcode,operand);
if(strcmp(opcode,”START”)==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,”\t%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\t”,locctr);
if(strcmp(label,”**”)!=0)
fprintf(fp2,”%s\t%d\n”,label,locctr);
while(strcmp(code,”END”)!=0)
{
if(strcmp(opcode,code)==0)
{
locctr+=3;
break;
}
fscanf(fp4,”%s”,code);
}
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);
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;
printf(“The length of the program is %d”,length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

No comments:

Post a Comment