Escolar Documentos
Profissional Documentos
Cultura Documentos
txt
START 200
MOVER AREG , ='5'
MOVEM AREG , A
LOOP MOVER AREG , A
MOVER CREG , B
ADD CREG , ='1'
LTORG
='5'
='1'
NEXT1 SUB AREG , ='1'
ORIGIN LOOP+6
MUL CREG , B
A DS 2
B DC 3
NEXT2 EQU LOOP
END
='1'
//Pass 1 Assembler
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
#include<ctype.h>
//----------------------------------------------------------------------------//
char label[4][10];
//To store all words in a line
int location_counter=0;
int literal_no1=1;
int literal_no2=1;
int L=0;
//To keep track of Literals
int C=0;
//To keep track of Constants
int cnt=0;
int s=0;
//For symbols
FILE *fp2,*fp3;
//----------------------------------------------------------------------------//
//Structure for Mnemonic Table
typedef struct mnemonic_table mnemonic_table;
struct mnemonic_table
{
int mno;
char mcode[10]; //Assembly mnemonic
int ocode;
//Instruction Opcode
int length;
//Default 1
char type[5];
//AD,DL,IS
};
mnemonic_table mtable[15];
//----------------------------------------------------------------------------//
//Structure for Symbol Table
typedef struct symbol symbol;
struct symbol
{
int sno;
//Symbol No
char sname[10]; //Symbol
int saddr;
//Symbol Addresss
int slength;
};
symbol sym_table[10];
//----------------------------------------------------------------------------//
void initialize_mtable();
int find(char []);
void label_initialize();
void pass1();
void labels();
//----------------------------------------------------------------------------//
main(void)
{
FILE *fp1,*fp2;
char temp[30];
fp1=fopen("C:\input.txt","r");
if(!fp1)
printf("\t\t FILE NOT OPENED !!!\n");
printf("\n------------------------INPUT-------------------------------\n
");
while(!feof(fp1))
{
fgets(temp,30,fp1);
printf("%s",temp);
}
printf("-------------------------------------------------------------\n"
);
fp2=fopen("C:\icode.txt","w");
fputs("LC\tIC\tOP\tOPE1\tIC->OPE2\n\n",fp2);
fp3=fopen("C:\literal.txt","w");
fputs("No\tLiteral\tAddress\n\n",fp3);
fcloseall();
labels();
printf("\nInput Code--->input.txt\n");
printf("\nIntermediate Code--->icode.txt\n");
printf("\nSymbol Table--->symbol.txt\n");
printf("\nLiteral Table--->literal.txt\n");
getch();
}
//----------------------------------------------------------------------------//
void initialize_mtable()
{
int i;
int j=1;
//------------------------------------------------------------------------//
//Assembler Directives
strcpy(mtable[0].mcode,"START");
strcpy(mtable[1].mcode,"END");
strcpy(mtable[2].mcode,"ORIGIN");
strcpy(mtable[3].mcode,"LTORG");
strcpy(mtable[4].mcode,"EQU");
for(i=0;i<=4;i++)
{
mtable[i].mno=i;
mtable[i].ocode=i+1;
mtable[i].length=0;
strcpy(mtable[i].type,"AD");
}
//------------------------------------------------------------------------//
//Declarative Statements
strcpy(mtable[5].mcode,"DC");
strcpy(mtable[6].mcode,"DS");
//Declare Constant
//Declare Storage
while(i<=6)
{
mtable[i].mno=i+1;
mtable[i].ocode=i+1;
strcpy(mtable[i].type,"DL");
i++;
}
//------------------------------------------------------------------------//
//Imperative Statements
strcpy(mtable[7].mcode,"MOVER");
strcpy(mtable[8].mcode,"MOVEM");
strcpy(mtable[9].mcode,"ADD");
strcpy(mtable[10].mcode,"MUL");
strcpy(mtable[11].mcode,"SUB");
strcpy(mtable[12].mcode,"DIV");
strcpy(mtable[13].mcode,"BC");
strcpy(mtable[14].mcode,"COMP");
while(i<=14)
{
mtable[i].mno=i;
mtable[i].ocode=j;
mtable[i].length=1;
strcpy(mtable[i].type,"IS");
i++;
j++;
}
}
//----------------------------------------------------------------------------//
void label_initialize()
{
int i;
for(i=0;i<4;i++)
{
strcpy(label[i]," ");
}
}
//----------------------------------------------------------------------------//
int find(char check[])
{
int i;
for(i=0;i<15;i++)
{
if(strcmp(check,mtable[i].mcode)==0)
return i;
}
if(strcmp(check,"AREG")==0)
return 51;
if(strcmp(check,"BREG")==0)
return 52;
if(strcmp(check,"CREG")==0)
return 53;
if(strcmp(check,"DREG")==0)
return 54;
return -1;
}
//----------------------------------------------------------------------------//
void labels()
{
FILE *fp1,*fp4;
char temp[30],tmp[10];
int i=0,j=0;
int flag=0;
initialize_mtable();
fp1=fopen("C:\input.txt","r");
fp2=fopen("C:\icode.txt","a+");
fp3=fopen("C:\literal.txt","a+");
while(!feof(fp1)) //while(2)
{
i=0;
cnt=0;
L=0;C=0;
fgets(temp,30,fp1);
label_initialize();
while(temp[i]!='\n') //while(1)
{
j=0;
while(isalnum(temp[i]))
{
flag=1;
tmp[j]=temp[i];
if(isdigit(tmp[0]) && temp[i-1]=='\'') //For l
iterals
L=1;
else if(isdigit(tmp[0]) && temp[i-1]==' ') //
For Constants
C=1;
i++;
j++;
}
if(flag==1)
{
flag=0;
tmp[j]='\0';
strcpy(label[cnt],tmp);
cnt++;
j=0;
}
if(temp[i]=='\n')
break;
i++;
}//end of while(1)
pass1();
}//end of while(2)
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp4=fopen("C:\symbol.txt","w");
fputs("No\tSymbol\tlength\tAddress\n\n",fp4);
for(i=0;i<s;i++)
{
fprintf(fp4,"%d\t",sym_table[i].sno);
fprintf(fp4,"%s\t\t",sym_table[i].sname);
fprintf(fp4,"%d\t\t",sym_table[i].slength);
fprintf(fp4,"%d\n\n",sym_table[i].saddr);
}
fclose(fp4);
}
//----------------------------------------------------------------------------//
//To generate intermediate code
void pass1()
{
int check,count=0,value=0,length=1,flag=0,i=0;
char c;
while(count<cnt)
{
check=find(label[count]);
if(isdigit(label[count][0])) //To convert a string with digits
value=atoi(label[count]); //into integer value
if(check==-1 && isalpha(label[count][0]) ) //for symbols
{
for(i=0;i<s;i++)
{
if(strcmp(label[count],sym_table[i].sname)==0)
{
sym_table[i].saddr=location_coun
ter;
flag=1;
break;
}
}
if(flag==0)
{
sym_table[s].sno=s+1;
strcpy(sym_table[s].sname,label[count]);
sym_table[s].saddr=location_counter;
sym_table[s].slength=length;
s++;
}
if(count!=0)
{
//To print symbols
c='S';
fprintf(fp2,"\t%c\t",c);
if(flag==0)
fprintf(fp2,"%d",s);
else
fprintf(fp2,"%d",sym_table[i].sno);
}
}
else if(check==0) //for Start(AD)
{
fprintf(fp2,"\t");
fprintf(fp2,"%s\t",mtable[check].type);
fprintf(fp2,"%d",mtable[check].ocode);
count++;
value=atoi(label[count]);
location_counter=value;
c='C';
fprintf(fp2,"\t\t%c\t",c);
fprintf(fp2,"%d",value);
location_counter--;
}
else if(check>=1 && check<=4)//for Assembler Directives
{
if(check==4)//for equ AD
{
count++;
for(i=0;i<s;i++)
{
if(strcmp(label[count],sym_table[i].sn
ame)==0)
break;
}
sym_table[s-1].saddr=sym_table[i].saddr;
}
location_counter--;
break;
}
else if(check>=7 && check<=14)//for Imperative Statements
{
fprintf(fp2,"%d",location_counter); //location counte
r
fprintf(fp2,"\t");
fprintf(fp2,"%s\t",mtable[check].type);
//IS
fprintf(fp2,"%d",mtable[check].ocode); //Opcode
}
else if(check==5 || check==6)//for Declarative Statements
{
fprintf(fp2,"%d",location_counter);
//location coun
ter
fprintf(fp2,"\t");
fprintf(fp2,"%s\t",mtable[check].type); //DL
fprintf(fp2,"%d",mtable[check].ocode); //Opcode
}
else if(check>=50 && check<=54) //for registers(A,B,C,D)
{
//register number
fprintf(fp2,"\t");
fprintf(fp2,"%d",check-50);
}
else if(isdigit(label[count][0]) && count==0) //For literal tab
le
{
fprintf(fp3,"%d \t",literal_no2);
al text file
fprintf(fp3,"%d\t\t",value);
fprintf(fp3,"%d\n\n",location_counter);
IC
OP
AD
200
IS
201
IS
202
IS
OPE1
IC->OPE2
C
200
203
IS
204
IS
205
005
206
001
207
IS
208
IS
209
DL
210
DL
211
001
No
209
LOOP
202
210
NEXT1
207
NEXT2
202
No
Literal Address
205
206
211
//Pass 2
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
//input
//output.txt-->Intetmediate code
//literal.txt-->Literal table
//symbol.txt--->symbol table
//output
//ic_t.txt file
//Generating target code
//----------------------------------------------------------------------------//
void
void
void
void
void
void
read_output();
read_literal();
read_symbol();
write_target();
find_literal(int);
find_symbol(int);
FILE *fp1,*fp2;
char label[5][20];
int cnt=0;
//----------------------------------------------------------------------------//
main(void)
{
read_output();
getch();
}
//----------------------------------------------------------------------------//
void read_output()
{
char temp[50],tmp[10];
int i=0,j=0,flag=0;
fp1=fopen("C:\output.txt","r");
fp2=fopen("C:\ic_t.txt","w");
printf("\nThe output is in ic_t.txt file\n");
fputs("Mnemonic
while(!feof(fp1)) //while(2)
{
i=0;
cnt=0;
fgets(temp,30,fp1);
if(isalpha(temp[i]))
{
//Ignore the line
}
else if(isdigit(temp[i]))
{
while(temp[i]!='\n') //while(1)
{
j=0;
while(isalnum(temp[i]))
{
flag=1;
tmp[j]=temp[i];
i++;
j++;
}
if(flag==1)
{
flag=0;
tmp[j]='\0';
strcpy(label[cnt],tmp);
cnt++;
j=0;
}
if(temp[i]=='\n')
break;
i++;
}
//end of while(1)
write_target();
//end of else
}//end of while(2)
fclose(fp1);
fclose(fp2);
}
//----------------------------------------------------------------------------//
void write_target()
{
int i=0,flag=0;
char store[10];
while(i<cnt)
{
//if(i==0 && isdigit(label[i][0]))
//Ignore(location counter)
if(strcmp(label[i],"IS")==0 && i!=0)
{
i++;
//Put the mnemonic in target file
fprintf(fp2,"%s\t\t",label[i]);
i++;
if(strcmp(label[k],store2)==0)
{
j=0;
while(lit[i]!='\n')
{
j=0;
flag=1;
while(isdigit(lit[i]))
{
store2[j]=lit[i];
j++;
i++;
}
store2[j]='\0';
if(lit[i]=='\n')
break;
i++;
}
}
} // end of else
}
if(flag==1)
{
fprintf(fp2,"\t%s\n\n",store2);//location counter of literal
}
fclose(fp3);
}
//----------------------------------------------------------------------------//
void find_symbol(int k)
{
int j=0,i=0,flag=0;
char store2[10];
char sym[30];
FILE *fp3;
fp3=fopen("C:\symbol.txt","r");
while(!feof(fp3))
{
if(flag==1)
break;
fgets(sym,30,fp3);
i=0;
j=0;
if(isalpha(sym[i]))
{
//Ignore the line
}
else
{
while(sym[i]==' ')
i++;
while(isdigit(sym[i]))
{
store2[j]=sym[i];
j++;
i++;
}
store2[j]='\0';
if(strcmp(label[k],store2)==0)
{
j=0;
while(sym[i]!='\n')
{
j=0;
flag=1;
while(isdigit(sym[i]))
{
store2[j]=sym[i];
j++;
i++;
}
store2[j]='\0';
if(sym[i]=='\n')
break;
i++;
}
}
} // end of else
}
if(flag==1)
{
fprintf(fp2,"\t%s\n\n",store2);//location counter of symbol
}
fclose(fp3);
}
//----------------------------------------------------------------------------//
//output of pass2
Mnemonic
205
209
209
210
206
00
005
00
001
211
210
00
001