Wednesday, April 13, 2011

Two Pass Assembler


This is a program for two pass assembler. This program uses static input source program. Shortage of time did not allow us to go for dynamic input. Dynamic program will be published after some time. The static input source program is in code[][] array. Field 1: Label, Field 2: Opcode, Field 3: Operand1, Field 4: Operand2. Please do comment for any queries.

Two-Pass Assembler

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char *ptr;


char *code[9][4]={
{"","START","0",""},
{"","USING","*","14"},
{"","L","1","FIVE"},
{"","A","1","FOUR"},
{"","ST","1","TEMP"},
{"FOUR","DC","F","\"4\""},
{"FIVE","DC","F","\"5\""},
{"TEMP","DS","1F",""},
{"","END","",""}
};
char av[2],avail[15]={'N','N','N','N','N','N','N','N','N','N','N','N','N','N','N'};
int i,j,k,count[3],lc[9]={0,0,0,0,0,0,0,0,0},loc=0,f=0;
clrscr();
printf("Input Program\n");
for(i=0;i<=8;i++)
{
for(j=0;j<=3;j++)
{
printf("%s\t",code[i][j]);
}
printf("\n");
}
printf("PASS 1 TABLE:\n---------------------------------------\n");
printf("LOC CNT\tLABEL\tOPCODE\tOP1\tOP2\n");
printf("---------------------------------------\n");
for(i=0;i<=8;i++)
{
if((strcmp(code[i][1],"START")!=0)&&(strcmp(code[i][1],"USING")!=0)&&(strcmp(code[i][1],"L")!=0))
lc[i]=lc[i-1]+4;
printf("%d\t",lc[i]);
if(strchr(code[i][3],'\"'))
printf("%s\t%s\t%s\t%s\n",code[i][0],code[i][1],code[i][2],code[i][3]);
else
printf("%s\t%s\t%s\n",code[i][0],code[i][1],code[i][2]);
}
printf("--------------------------------------");
getch();
printf("\n\nSYMBOL TABLE:\n----------------------------------------------------\n");
printf("SYMBOL\t\tVALUE\t\tLENGTH\t\tR/A");
printf("\n----------------------------------------------------\n");

for(i=0;i<9;i++)
{
/*if(strcmp(code[i][1],"START")==0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
}
else*/ if(strcmp(code[i][0],"")!=0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
loc=4+loc;
}
else if(strcmp(code[i][1],"USING")==0){}
else
{loc=4+loc;}
}
printf("----------------------------------------------------");
getch();
printf("\nLITERAL TABLE :\n----------------------------------------------------");
printf("\nLIT NAME\tVALUE\t\tLENGTH\t\tR/A\n");
printf("----------------------------------------------------\n");
for(i=0;i<=8;i++)
{
ptr=strchr(code[i][3],'\"');
if(ptr)
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][3],lc[i],4,'R');
}
printf("----------------------------------------------------");
getch();
clrscr();
printf("\n\nBASE TABLE:\n----------------------------------------\n");
printf("REG NO\t\tAVAILIBILITY\t");
printf("\n----------------------------------------\n");
for(j=0;j<=8;j++)
{
if(strcmp(code[j][1],"USING")!=0)
{}
else
{
strcpy(av,code[j][3]);
}
}
count[0]=(int)av[0]-48;
count[1]=(int)av[1]-48;
count[2]=count[0]*10+count[1];
avail[count[2]-1]='Y';

for(k=0;k<16;k++)
{
printf(" %d\t\t %c\n",k,avail[k-1]);
}
printf("----------------------------------------\n");
getch();
clrscr();
printf("PASS2 TABLE:\n-------------------------------------\n");
printf("LOC CNT\tLABEL\tOPCODE\tOP1\tOP2");
printf("\n-------------------------------------\n");
loc=0;
for(i=0;i<=8;i++)
{
f=0;
for(j=i;j<=7;j++)
{
if(strcmp(code[i][3],code[j][0])==0)
{
printf("%d\t%s\t%s\t%s\t%d\n",lc[i],code[i][0],code[i][1],code[i][2],lc[j]);
f=1;
break;
}
}
if(f==0)
printf("%d\t%s\t%s\t%s\t%s\n",lc[i],code[i][0],code[i][1],code[i][2],code[i][3]);
}
printf("-------------------------------------");
getch();
}


Output

Input Program
        START   0
        USING   *       14
        L       1       FIVE
        A       1       FOUR
        ST      1       TEMP
FOUR    DC      F       "4"
FIVE    DC      F       "5"
TEMP    DS      1F
        END
PASS 1 TABLE:
---------------------------------------
LOC CNT LABEL   OPCODE  OP1     OP2
---------------------------------------
0               START   0
0               USING   *
0               L       1
4               A       1
8               ST      1
12      FOUR    DC      F       "4"
16      FIVE    DC      F       "5"
20      TEMP    DS      1F
24              END
--------------------------------------

SYMBOL TABLE:
----------------------------------------------------
SYMBOL          VALUE           LENGTH          R/A
----------------------------------------------------
FOUR            16              4               R
FIVE            20              4               R
TEMP            24              4               R
----------------------------------------------------
LITERAL TABLE :
----------------------------------------------------
LIT NAME        VALUE           LENGTH          R/A
----------------------------------------------------
"4"             12              4               R
"5"             16              4               R
----------------------------------------------------


BASE TABLE:
----------------------------------------
REG NO          AVAILIBILITY
----------------------------------------
 0
 1               N
 2               N
 3               N
 4               N
 5               N
 6               N
 7               N
 8               N
 9               N
 10              N
 11              N
 12              N
 13              N
 14              Y
 15              N
----------------------------------------
PASS2 TABLE:
-------------------------------------
LOC CNT LABEL   OPCODE  OP1     OP2
-------------------------------------
0               START   0       0
0               USING   *       14
0               L       1       16
4               A       1       12
8               ST      1       20
12      FOUR    DC      F       "4"
16      FIVE    DC      F       "5"
20      TEMP    DS      1F
24              END
-------------------------------------

No comments:

Post a Comment