One Pass Assembler [ SIC Load and Go Assembler Simulation]


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct node
{
int addr;
struct node *link;
};
struct symbol
{
char label[10];
int addr;
struct node *ptr;
}symtab[100];
void inserttosymtab(char *,int,int);
void resolve(int,int);
void modifyobjectcode(int,int);
int checkresolved(void) ;
int searchsymbol(char *,int*);
int searchopcode(char *);
int current=0,start;
char objectcode[500]={'\0'} ;
void main()
{
FILE *fin;
int i,locctr,found,det,x,nloc,len,addr,loadaddr;
char s[10],operand[10],opcode[10],label[10];
fin=fopen("input.txt","r");
fscanf(fin,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{

locctr=atoi(operand);
start=locctr;
}
else
locctr=0;

fscanf(fin,"%s%s%s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)
{
if(strcmp(label,"-")!=0)
{
      found=searchsymbol(label,&addr);
      if(found==1)
      {
printf("Duplicate label found\n");
exit(0);
      }
      else if(found==0)
      {
inserttosymtab(label,locctr,0);
       }
      else
resolve(locctr,addr);

}
det=searchopcode(opcode );
if(det==1)
{
strcat(objectcode,opcode);
locctr=locctr+1;
found=searchsymbol(operand,&addr);
if(found==0|| found==2)
{
strcat(objectcode,"0000");
if(strcmp(operand,"-")!=0)
inserttosymtab(operand,locctr,1);
locctr=locctr+2;
}
else if(found==1)
{

sprintf(operand,"%d",addr);
strcat(objectcode,operand);
locctr=locctr+2;
}
  }
else if((strcmp(opcode,"BYTE")==0)||(strcmp(opcode,"WORD")==0))
{
if(strcmp(opcode,"WORD")==0)
{
    len=strlen(operand);
    for(i=0;i<(6-len);i++)
    strcat(objectcode,"0");
    strcat(objectcode,operand);
    locctr=locctr+3;

}
else
{
len=strlen(operand);
for(i=2;i<len-1;i++)
{
x=toascii(operand[i]);
sprintf(s,"%d",x);
strcat(objectcode,s);
}
locctr=locctr+len-3;

}
}
else if(strcmp(opcode,"RESW")==0)
{
nloc=3*atoi(operand);
locctr=locctr+3*atoi(operand);
for(i=0;i<nloc;i++)
strcat(objectcode,"X");
}
else if(strcmp(opcode,"RESB")==0)
{
nloc=atoi(operand);
locctr=locctr+atoi(operand);
for(i=0;i<nloc;i++)
strcat(objectcode,"XX");
}


    fscanf(fin,"%s%s%s",label,opcode,operand);

}
fclose(fin);
if(checkresolved())
{
i=0;
loadaddr=start;
printf("\n......Load and Go One Pass assembler loaded memory map....\n");

while(objectcode[i]!='\0')
{
if(i%32==0)
{
printf("\n%d",loadaddr);
loadaddr=loadaddr+16;
}
if(i%8==0 )
printf("  ");
printf("%c",objectcode[i]);
i++;
}
}

}

void inserttosymtab(char *label,int locctr,int flag)
{
struct node *temp;
int det,pos;

if(flag==0)
{
strcpy(symtab[current].label,label);
symtab[current].addr=locctr;
symtab[current].ptr=NULL;
current++;
}
else
{
det=searchsymbol(label,&pos);
if(det==0)
{
pos=current;
current++;
}
strcpy(symtab[pos].label,label);
temp=(struct node *)malloc(sizeof(struct node));
temp->addr=locctr;
temp->link=symtab[pos].ptr;
symtab[pos].ptr=temp;
}

}
int searchsymbol(char *label,int *addr)
{
int i=0;
for(i=0;i<current;i++)
{
if(strcmp(symtab[i].label ,label)==0)
{
if(symtab[i].ptr==NULL)
{
*addr=symtab[i].addr;
return 1;
}
else
{ *addr=i;
return 2;
}
}
}
return 0;
}
void resolve(int locctr,int loc)
{
struct node *temp;
temp=symtab[loc].ptr;
while(temp!=NULL)
{
modifyobjectcode(temp->addr,locctr);
temp=temp->link;
}
symtab[loc].ptr=NULL;
symtab[loc].addr=locctr;
}

void modifyobjectcode(int addr,int locctr)
{

char operand[10];
int i=0;
sprintf(operand,"%d",locctr);
addr=(addr-start)*2;
while(operand[i]!='\0')
{
objectcode[addr]=operand[i];
addr++;
i++;
}
}

int searchopcode(char *opcode)
{
char opmne[10],op[10];
int flag=0;
FILE *optab;
optab=fopen("optab.txt","r");

fscanf(optab,"%s%s",opmne,op);

while(!feof(optab))
{
if(strcmp(opcode,opmne)==0)
{
flag=1;
strcpy(opcode,op);
break;


}
else
fscanf(optab,"%s%s",opmne,op);
}
fclose(optab);
return flag;
}

int checkresolved(void)
{
int i=0,flag=1;
for(i=0;i<current;i++)
{
if(symtab[i].ptr!=NULL)
{
printf("%s is not resoved,",symtab[i].label);
flag=0;
}
}
return(flag);
}
INPUT FILES
input.txt
optab.txt
Output

No comments:

Post a Comment