The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Thursday, November 22, 2012

Create a 2-Pass 8085 Assembler


CODING :-

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

void gethex(int v,char*str)
{ str[3]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  v=v/16;
  str[2]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  v=v/16;
  str[1]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  v=v/16;
  str[0]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  str[4]='\0';
}

int main()
{
  char codes[100][2][100];
  int csize[100];
  int opos[100];
  int codemem[100];
  int tot=0,num=0,l=0;
  char str[100]="",ch;
  char*ptr=NULL;
  int i=0,j=0,k=0,fpos=0;

  for(i=1;i<=5;i++)
  for(j=1;j<=5;j++)
  if(i!=j)
  { strcpy(str,"MOV ");
    str[4]='A'+i-1;
    str[5]=',';
    str[6]='A'+j-1;
    str[7]='\0';
    strcpy(codes[tot][0],str);
    str[0]='A'+i-1;
    str[1]='A'+j-1;
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=1;
    opos[tot]=0;
    tot++;
  }

  for(i=1;i<=5;i++)
  { strcpy(str,"ADD ");
    str[4]='A'+i-1;
    str[5]='\0';
    strcpy(codes[tot][0],str);
    str[0]='1';
    str[1]='A'+i-1;
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=1;
    opos[tot]=0;
    tot++;
  }




  for(i=1;i<=5;i++)
  { strcpy(str,"LOAD ");
    str[5]='A'+i-1;
    str[6]='\0';
    strcpy(codes[tot][0],str);
    str[0]='2';
    str[1]='A'+i-1;
    //str[4]='H';
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=2;
    opos[tot]=7;
    tot++;
  }  

  for(i=1;i<=5;i++)
  { strcpy(str,"SUB ");
    str[4]='A'+i-1;
    str[5]='\0';
    strcpy(codes[tot][0],str);
    //str[0]='0';
    //str[1]='4';
    str[0]='3';
    str[1]='A'+i-1;
    //str[4]='H';
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=1;
    opos[tot]=0;
    tot++;
  }

  int jump=tot;
  strcpy(str,"JMP ");
  strcpy(codes[tot][0],str);
  strcpy(str,"06");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=4;
  tot++;

  strcpy(str,"JNZ ");
  strcpy(codes[tot][0],str);
  strcpy(str,"07");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=4;
  tot++;

  strcpy(str,"JZ ");
  strcpy(codes[tot][0],str);
  strcpy(str,"08");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=3;
  tot++;

  strcpy(str,"JNC ");
  strcpy(codes[tot][0],str);
  strcpy(str,"09");
  strcpy(codes[tot][1],str);


  csize[tot]=3;
  opos[tot]=4;
  tot++;

  strcpy(str,"JC ");
  strcpy(codes[tot][0],str);
  strcpy(str,"0A");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=3;
  tot++;
 
  char code[100][100];
  printf("\nTo end the code enter END\n");
  int size=0;
  while(1)
  { gets(code[size]);
    if(strcmp(code[size],"END")==0)
    break;
    size++;
  }

  char symb[100][2][100];
  int symbsize=0;
  int mempos=8192;

  for(i=0;i<size;i++)
  { int type=-1;
    int islabel=-1;
    for(j=0;code[i][j]!='\0';j++)
    if(code[i][j]==':')
    { islabel=j;
      break;
    }
    if(islabel!=-1)
    {
      for(j=0;j<islabel;j++)
      str[j]=code[i][j];
      str[j]='\0';
      strcpy(symb[symbsize][0],str);
      sprintf(str,"%d",mempos);
      strcpy(symb[symbsize][1],str);
      symbsize++;
    }
    else
    { for(j=0;j<tot;j++)
      { ptr=strstr(code[i],codes[j][0]);
        if(ptr!=NULL)
        { type=j;
         
          break;
        }
      }
      if(type==-1)
      { printf("\nIllegal Code\n");
        getch();
        exit(0);
      }
      mempos+=csize[type];
    }      
  }  



  mempos=8192;
  char output[100][100];
  int curs=0;

  for(i=0;i<size;i++)
  { int type=-1,islabel=-1;
    for(j=0;code[i][j]!='\0';j++)
    if(code[i][j]==':')
    { islabel=j;
      break; }
    if(islabel==-1)
    {
      for(j=0;j<tot;j++)
      { ptr=strstr(code[i],codes[j][0]);
        if(ptr!=NULL)
        { type=j;
          if(opos[j]==0)
          { strcpy(output[curs],codes[j][1]);
            codemem[curs]=mempos;
            mempos+=csize[j];
            curs++;
          }
          else if(opos[j]==7)
          { strcpy(output[curs],codes[j][1]);
            strcat(output[curs]," ");
            str[0]=code[i][opos[j]];
            str[1]=code[i][opos[j]+1];
            str[2]='\0';
            strcat(output[curs],str);
            codemem[curs]=mempos;
            mempos+=csize[j];
           
            curs++;
          }  
          else if(j>=jump)
          { strcpy(output[curs],codes[j][1]);
            strcat(output[curs]," ");
            for(k=opos[j];code[i][k]!='\0';k++)
            str[k-opos[j]]=code[i][k];
            str[k-opos[j]]='\0';
            fpos=-1;
            for(k=0;k<symbsize;k++)
            if(strcmp(symb[k][0],str)==0)
            { fpos=k;
              break;
            }
            if(fpos==-1)
            { printf("\nLabel named %s not defined\n",str);
              getch();
              exit(0);
            }
            num=0;
            for(l=0;l<4;l++)
            num=num*10+symb[k][1][l]-'0';
            gethex(num,str);
            ch=str[0];
            str[0]=str[2];
            str[2]=ch;
            ch=str[1];
            str[1]=str[3];
            str[3]=ch;
            strcat(output[curs],str);



            codemem[curs]=mempos;
            mempos+=csize[j];
            curs++;
          }
          break;  
        }
      }
    }
  }

  printf("\nMachine Code is\nMemory  Opcode+Operand\n");
  for(i=0;i<curs;i++)
  { gethex(codemem[i],str);
    printf("%s        %s\n",str,output[i]);
  }
  getch();
}


Input File:-

MOV A,B
MOV C,D
LOAD E,10H
MOV A,E
TEST:
ADD C
SUB D
JMP TEST
RUN:
ADD B
JC RUN
MOV D,E
JNC RUN
SUB A
JZ TEST
JNZ RUN
END

OUTPUT SCREEN:-

Machine Code is
Memory  Opcode+Operand
2000        AB
2001        CD
2002        2E 10
2004        AE
2005        1C
2006        3D
2007        06 0520
200A        1B
200B        0A 0A20
200E        DE
200F        09 0A20
2012        3A
2013        08 0520
2016        07 0A20


No comments:

Post a Comment