#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char POT[][7]={"START","USING","END"};
char MOT[][6]={"L","ST","A","S","M"};
char DEC[][4]={"DC","DS"};
typedef struct symbol{
char symbol[10];
int addr;
}SYMTAB;
SYMTAB sym[10];
int startaddr,lc,symcount=0,length;
char line[20],label[20],operand[20],opcode[20],prgname[20],op[4];
int search_POT(char opcode[]){
int i,j;
for(i=0;i<5;i++){
if(strcmp(opcode,POT[i])==0)
return i;
}
return -1;
}
int search_DEC(char opcode[]){
int i,j;
for(i=0;i<5;i++){
if(strcmp(opcode,DEC[i])==0)
return i;
}
return -1;
}
int calc_DEC(char opcode[],char operand[]){
int i=0,l,k=0;
char copy[10];
int j=0,m=0;
if(strcmp(opcode,"DC")==0){
switch(operand[0]){
case 'B':i=1;break;
case 'H':i=2;break;
case 'F':i=4;break;
case 'D':i=8;break;
}
for(j=2;operand[j]!='\'';j++){
op[m++]=operand[j];
}
op[m]='\0';
//printf(" %s ",op);
}
else{
l=strlen(operand);
switch(operand[l-1]){
case 'B':i=1;break;
case 'H':i=2;break;
case 'F':i=4;break;
case 'D':i=8;break;
}
for(j=0;j<=l-2;j++){
copy[k++]=operand[j];
}
copy[k]='\0';
//printf("%s",copy);
k=atoi(copy);
i=i*k;
}
return i;
}
void READ_LINE(){
char buff[20],word1[20],word2[20],word3[20];
int i,j=0,count=0;
label[0]=operand[0]=opcode[0]=word1[0]=word2[0]=word3[0]='\0';
for(i=0;line[i]!='\0';i++){
if(line[i]!=' '){
buff[j++]=line[i];
}
else{
buff[j]='\0';
strcpy(word3,word2);
strcpy(word2,word1);
strcpy(word1,buff);
j=0;
count++;
}
}
buff[j-1]='\0';
strcpy(word3,word2);
strcpy(word2,word1);
strcpy(word1,buff);
switch(count){
case 0: strcpy(opcode,word1);
break;
case 1: strcpy(opcode,word2);
strcpy(operand,word1);
break;
case 2: strcpy(label,word3);
strcpy(opcode,word2);
strcpy(operand,word1);
break;
}
}
void check_label(){
int k,dupsym=0;
for(k=0;k<symcount;k++){
if(strcmp(label,sym[k].symbol)==0){
sym[k].addr=-1;
dupsym=1;
break;
}
}
if(!dupsym){
strcpy(sym[symcount].symbol,label);
sym[symcount++].addr=lc;
}
//printf("%s %d",sym[symcount-1].symbol,sym[symcount-1].addr);
}
PASS1(){
FILE *input,*inter;
int p,m,d,l;
input=fopen("input.txt","r");
inter=fopen("inter.txt","w+");
if(input==NULL){
printf("Error");
exit(0);
}
if(inter==NULL){
printf("Error");
exit(0);
}
fgets(line,20,input);
READ_LINE();
printf("\nlabel:%s opcode:%s operand:%s",label,opcode,operand);
if(strcmp(opcode,"START")==0){
startaddr=atoi(operand);
lc=startaddr;
strcpy(prgname,label);
fprintf(inter,"%d",lc);
fgets(line,20,input);
}
else{
prgname[0]='\0';
startaddr=0;
lc=0;
fgets(line,20,input);
}
while(strcmp(line,"END\n")!=0){
READ_LINE();
printf("\nlabel:%s opcode:%s operand:%s",label,opcode,operand);
p=search_POT(opcode);
if(p!=-1){
l=0;
fprintf(inter,"\n%d",lc);
}
else{
d=search_DEC(opcode);
if(d!=-1){
l=calc_DEC(opcode,operand);
//if(label[0]!='\0')check_label();
if(strcmp(opcode,"DC")==0)fprintf(inter,"\n%d %s",lc,op);
else fprintf(inter,"\n%d --",lc);
//printf("%d",l);
}
else{
l=4;
fprintf(inter,"\n%d %s %s",lc,opcode,operand);
}
}
if(label[0]!='\0')check_label();
lc=lc+l;
fgets(line,20,input);
}
/*READ_LINE();
printf("\nopcode:%s",opcode);
*/
fprintf(inter,"\n%d",lc);
fclose(input);
fclose(inter);
}
char line1[20],lc1[5],op1[5],op2[5],opcode1[20];
void READ_LINE1(){
char buff[20],word1[20],word2[20],word3[20],word4[20];
int i,j=0,count=0;
lc1[0]=op1[0]=op2[0]=opcode1[0]=word1[0]=word2[0]=word3[0]=word4[0]='\0';
//printf("\n%s",line1);
for(i=0;line1[i]!='\0';i++){
if((line1[i]!=' ')&&(line1[i]!=',')){
buff[j++]=line1[i];
}
else{
buff[j]='\0';
strcpy(word4,word3);
strcpy(word3,word2);
strcpy(word2,word1);
strcpy(word1,buff);
j=0;
count++;
}
}
buff[j]='\0';
strcpy(word4,word3);
strcpy(word3,word2);
strcpy(word2,word1);
strcpy(word1,buff);
if(count==0){
strcpy(lc1,word1);
}
else if(count==1){
strcpy(lc1,word2);
strcpy(op1,word1);
}
else{
strcpy(lc1,word4);
strcpy(opcode1,word3);
strcpy(op1,word2);
strcpy(op2,word1);
}
//printf("lc=%s opcode=%s op1=%s op2=%s",lc1,opcode1,op1,op2);
}
int search_SYMTAB(){
int i,l,j,k=0;
char temp[5];
l=strlen(op2);
for(j=0;op2[j]!='\n';j++){
temp[k++]=op2[j];
}
//temp[k]='\0';
for(i=0;i<symcount;i++){
if(strcmp(temp,sym[i].symbol)==0){
return i;
}
}
return -1;
}
void PASS2(){
FILE *inter,*output;
int c;
inter=fopen("inter.txt","r");
output=fopen("output.txt","w+");
while(fgets(line1,20,inter)!=NULL){
//printf("%s",line1);
READ_LINE1();
if(op2[0]=='\0')
fprintf(output,"%s",line1);
else{
c=search_SYMTAB();
if(c==-1)printf("Undefined symbol");
else{
fprintf(output,"%s %s %s,%d\n",lc1,opcode1,op1,sym[c].addr);
}
}
}
}
void main(){
PASS1();
PASS2();
printf("\n");
}