Your Ad Here

Tuesday, August 25, 2009

Two Pass Assembler

#include< stdio.h>
#include< conio.h>
#include< string.h>
#define lines_in_program 9
void print(char *p,int loc,int len,char ra)
{printf("%s\t%d\t%d\t%c\n",p,loc,len,ra);}
void main()
{
char *p[9][4] = {{"PRG1","START","",""},{"","USING","*","15"},
{"","L","1","FIVE"}, {"","A","1","FOUR"},
{"","ST","1","TEMP"}, {"FOUR","DC","F'4'",""},
{"FIVE","DC","F'5'",""}, {"TEMP","DS","1F",""},
{"","END","",""}};
int i,j=0,location_counter=0;
clrscr();
for (i=0;i< 9;i++)
{ for(j=0;j< 4;j++)
printf("%s\t",p[i][j]);
printf("\n");
}
printf("\n\n\n\n Symbol ");
printf("Table:\nSYMBOL\tVALUE\tLENGTH\tRelocatable/Absolute\n");
printf("---------------------------------------------\n");
for(i=0;i< 9;i++)
{ if(strcmp(p[i][1],"START")==0)
print(p[i][0],location_counter,1,'R');
else if(strcmp(p[i][0],"")!=0)
{
print(p[i][0],location_counter,4,'R');
location_counter=4+location_counter;
}
else if(strcmp(p[i][1],"USING")==0){}
else{location_counter=4+location_counter;}
}
getch();
}


/*OUTPUT:
PRG1 START
USING * 15
L 1 FIVE
A 1 FOUR
ST 1 TEMP
FOUR DC F'4'
FIVE DC F'5'
TEMP DS 1F
END

Symbol Table:
SYMBOL VALUE LENGTH Relocatable/Absolute
---------------------------------------------
PRG1 0 1 R
FOUR 12 4 R
FIVE 16 4 R
TEMP 20 4 R */

Two Pass Assembler

#include< stdio.h>
#include< string.h>
#include< conio.h>


void main()
{
char *code[9][4]={
{"PRG1","START","",""},
{"","USING","*","15"},
{"","L","",""},
{"","A","",""},
{"","ST","",""},
{"FOUR","DC","F",""},
{"FIVE","DC","F",""},
{"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;
clrscr();
printf("----------------------------------------------------\n");
printf("LABEL\t\tOPCODE\n");
printf("----------------------------------------------------\n\n");
for(i=0;i< =8;i++)
{
for(j=0;j< =3;j++)
{
printf("%s\t\t",code[i][j]);
}
j=0;
printf("\n");
}
getch();
printf("-----------------------------------------------------");
printf("\nVALUES FOR LC : \n\n");
for(j=0;j< =8;j++)
{
if((strcmp(code[j][1],"START")!=0)&&(strcmp(code[j][1],"USING")!=0)&&(strcmp(code[j][1],"L")!=0))
lc[j]=lc[j-1]+4;
printf("%d\t",lc[j]);
}
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("----------------------------------------------------");

printf("\n\nBASE TABLE:\n-------------------------------------------------------\n");
printf("REG NO\t\tAVAILIBILITY\tCONTENTS OF BASE TABLE");
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");
printf("Continue..??");
getchar();
printf("PASS2 TABLE:\n\n");
printf("LABEL\t\tOP1\t\tLC\t\t");
printf("\n----------------------------------------------------\n");
loc=0;
for(i=0;i< =8;i++)
{
for(j=0;j< =3;j++)
{
printf("%s\t\t",code[i][j]);
}
j=0;
printf("\n");
}
printf("-----------------------------------------------------");

getch();

}
/*
----------------------------------------------------
LABEL OPCODE
----------------------------------------------------

PRG1 START
USING * 15
L
A
ST
FOUR DC F
FIVE DC F
TEMP DS 1F
END



-----------------------------------------------------
VALUES FOR LC :

0 0 0 4 8 12 16 20 24

SYMBOL TABLE:
----------------------------------------------------
SYMBOL VALUE LENGTH R/A
----------------------------------------------------

PRG1 0 4 R
FOUR 12 4 R
FIVE 16 4 R
TEMP 20 4 R


----------------------------------------------------

BASE TABLE:
-------------------------------------------------------
REG NO AVAILIBILITY CONTENTS OF BASE TABLE
-------------------------------------------------------
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 N
15 Y

-------------------------------------------------------
Continue..??
PASS2 TABLE:

LABEL OP1 LC
----------------------------------------------------
PRG1 START
USING * 15
L
A
ST
FOUR DC F
FIVE DC F
TEMP DS 1F
END
-----------------------------------------------------


*/

Lexical Analizer

/* TO IMPLEMENT LEXICAL ANALIZER IN C */

#include< conio.h>
#include< string.h>

void main()
{
int i,j,lc;
char *a[9][4]={"PRG","START" ," " ," ",
" " ,"USING" ,"*" ,"15",
" ","L","1","FIVE",
" ","A","1","FOUR",
" ","ST","1","TEMP",
"FOUR ","DC","F","4",
"FIVE","DC","F","5",
"TEMP","DS","1","F",
" " ,"END"," "," ", };
clrscr();
printf("\n \t\t LEXICAL ANALIZER \n");

for(i=0;i< 9;i++)
{ for(j=0;j< 4;j++)
{ if(isalpha(*a[i][j]))
printf("\n STRING : %s",a[i][j]);
if(isdigit(*a[i][j]))
printf("\n DIGIT : %s",a[i][j]);
}
printf("\n");
}

getch();
}
/* OUTPUT:-

LEXICAL ANALIZER

STRING : PRG
STRING : START

STRING : USING
DIGIT : 15

STRING : L
DIGIT : 1
STRING : FIVE

STRING : A
DIGIT : 1
STRING : FOUR

STRING : ST
DIGIT : 1
STRING : TEMP

STRING : FOUR
STRING : DC
STRING : F
DIGIT : 4

STRING : FIVE
STRING : DC
STRING : F
DIGIT : 5

STRING : TEMP
STRING : DS
DIGIT : 1
STRING : F

STRING : END

*/
Your Ad Here