Logo 
Search:

Assembly Language Articles

Submit Article
Home » Articles » Assembly Language » Homework HelpRSS Feeds

Program of an ASSEMBLER

Posted By: James Evans     Category: Assembly Language     Views: 5982

Write a Program of an ASSEMBLER.

Code for Program of an ASSEMBLER in Assembly Language

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
ofstream stab;
ofstream finobj;
ofstream opp1;
ofstream ini;
ifstream iin;
class symtab
{
    public:
        char symbol[10];
        int addr,length;
};

class optab
{
    public:
        char opcode[10],opclass[3];
        int code;
};
void main()
{
    symtab symobj[20];
    optab opobj[20];
    char str[20],label[8],op_code[10],str3[10],str4[10],temp[10];
    int len,i=0,j=0,add,lc,dj,mem,lmem,line=1,flag=0,com_flag=0,sym_count=0,op_count=0;
    int sym_temp, temp_flag = 0;
    fstream fin;
    clrscr();
    stab.open("sym.tab");
    fin.open("temp.asm",ios::in);
    opp1.open("OPP.TAB");
    ini.open("inter");
    fin.getline(str,80);
    len=strlen(str);
    i=8;
    j=0;
    while(str[i]!=' ')     //FINDING FOR "START"
    {
        temp[j]=str[i];
        j++;
        i++;
    }
    temp[j]='\0';
    line++;
    if(strcmp(temp,"START")!=0)
    {
        cout<<"\nERROR : line : " << line << " : \"START\" is not define";
        com_flag=1;
    }
    else
        flag=1;
    i=14;
    j=0;
    while(i<len )
    {
        temp[j]=str[i];
        i++;
        j++;
    }
    add=atoi(temp);
    lc=add;
    ini<<"(AD,01)  (C,"<<add<<")"<<endl;
    j=0;
    while(fin.eof()==0)
    {
        fin.getline(str,80);
        if(fin.eof())
            break;
        len=strlen(str);
        j=0;
        while(j<8 && str[j]!='.')           // FINDING LABEL
        {
            label[j]=str[j];
            j++;
        }
        label[j] = '\0';
        dj=8;
        j=8;
        if(strcmp(label,"        ")!=0)
        {
            sym_temp = 0;
            while(sym_temp != sym_count)
            {
                if(strcmp(symobj[sym_temp].symbol,label)==0)
                {
                    cout << "\nERROR : line : " << line << " : Duplicate declaration of \"" << label << "\"";
                    com_flag=1;
                }
                sym_temp++;
            }
            strcpy(symobj[sym_count].symbol,label);
            symobj[sym_count].addr=lc;
            while(str[dj]!=' ')
                dj++;
            dj++;
            i=0;
            while(str[dj]!=' ' && str[dj]!='\0')
            {
                temp[i]=str[dj];
                dj++;
                i++;
            }
            temp[i]='\0';
            lmem=atoi(temp);
            symobj[sym_count].length=lmem;
            stab<<symobj[sym_count].symbol<<" "<<symobj[sym_count].addr<<" "<<symobj[sym_count].length<<"\n";
            sym_count++;
        }
        i=0;
        while(str[j]!=' ' && str[j]!='\0')
        {
            op_code[i]=str[j];
            j++;
            i++;
        }
        dj=j;
        op_code[i]='\0';
        flag=0;
        if(strcmp(op_code,"MOV")==0 || strcmp(op_code,"BC")==0 || strcmp(op_code,"READ")==0)
        {
            strcpy(opobj[op_count].opcode,op_code);
            strcpy(opobj[op_count].opclass,"IS");
            if(strcmp(op_code,"MOV")==0)
                opobj[op_count].code=4;
            elseif(strcmp(op_code,"BC")==0)
                opobj[op_count].code=7;
            elseif(strcmp(op_code,"READ")==0)
                opobj[op_count].code=9;
            i=0;
            while(str[j]!=',' && str[j]!='\0')
            {
                str3[i]=str[j];
                j++;
                i++;
            }
            str3[i]='\0';
            if(strcmp(op_code,"MOV")==0)
            {
                if(str3[2]!='X')
                {
                    cout<<"\nERROR : line : "<<line<<" : In \""<<op_code<<"\" instruction first operator is always register";
                    com_flag=1;
                }
            }
            elseif(strcmp(op_code,"BC")==0)
            {
               if(strcmp(str3," LT")!=0 && strcmp(str3," LE")!=0 && strcmp(str3," EQ")!=0 && strcmp(str3," GT")!=0 && strcmp(str3," GE")!=0 && strcmp(str3," ANY")!=0)
                {
                    cout<<"\nERROR : line : "<<line<<" : In \""<<op_code<<"\" instruction first operator is always condition code";
                    com_flag=1;
                }
            }
            i=0;
            while(str[j]!='\0')
            {
                str4[i]=str[j+1];
                j++;
                i++;
            }
            flag=1;
            str4[i]='\0';
            sym_temp = 0;
            while(sym_temp != sym_count )
            {
                if(str4[0] != '\0')
                {
                    if(strcmp(symobj[sym_temp].symbol,str4)!=0)
                    {
                        temp_flag = 1;
                     }
                    else
                    {
                        temp_flag = 0;
                        goto temp_label;
                    }
                }
                sym_temp++;
            }
            temp_label:
            if(temp_flag ==1)
            {
                cout << "\nERROR : line : "<< line << " : The second operand \"" << str4 << "\" not define";
                com_flag = 1;
            }
            ini<<"(IS,0"<<opobj[op_count].code<<")  "<<str3<<"," << str4<<endl;
            opp1<<op_code<<" IS 0"<<opobj[op_count].code;
            mem=1;
            op_count++;
        }
        elseif(strcmp(op_code,"ADD")==0 || strcmp(op_code,"SUB")==0 || strcmp(op_code,"MUL")==0 || strcmp(op_code,"DIV")==0)
        {
            strcpy(opobj[op_count].opcode,op_code);
            strcpy(opobj[op_count].opclass,"IS");
            if(strcmp(op_code,"ADD")==0)
                opobj[op_count].code=1;
            elseif(strcmp(op_code,"SUB")==0)
                opobj[op_count].code=2;
            elseif(strcmp(op_code,"MUL")==0)
                opobj[op_count].code=3;
            elseif(strcmp(op_code,"DIV")==0)
                opobj[op_count].code=5;
            i=0;
            while(str[j]!=',')
            {
                str3[i]=str[j];
                j++;
                i++;
            }
            str3[i]='\0';
            if(str3[2]!='X')
            {
                cout<<"\nERROR : line : "<<line<<" : In \""<<op_code<<"\" Instruction first operator is always register";
                com_flag=1;
            }
            i=0;
            while(str[j]!='\0')
            {
                str4[i]=str[j+1];
                j++;
                i++;
            }
            flag=1;
            str4[i]='\0';
            sym_temp = 0;
            while(sym_temp != sym_count || symobj[0].symbol == " ")
            {
                if(str4[0] != '\0')
                {
                    if(strcmp(symobj[sym_temp].symbol,str4)!=0)
                    {
                        temp_flag = 1;
                     }
                    else
                    {
                        temp_flag = 0;
                        goto label_temp;
                    }
                }
                sym_temp++;
            }
            label_temp:
            if(temp_flag ==1)
            {
                cout << "\nERROR : line : " << line << " : The second operand \"" << str4 << "\"not define";
                com_flag = 1;
            }

            ini<<"(IS,0"<<opobj[op_count].code<<")  "<<str3<<"," <<str4<<endl;
            opp1<<op_code<<" IS 0"<<opobj[op_count].code;
            mem=1;
            op_count++;
        }
        elseif(strcmp(op_code,"END")==0)
        {
            strcpy(opobj[op_count].opcode,op_code);
            strcpy(opobj[op_count].opclass,"AD");
            opobj[op_count].code=2;
            opp1<<op_code<<" AD 02";
            mem=1;
            flag=1;
            op_count++;
        }
        elseif(strcmp(op_code,"STOP")==0)
        {
            strcpy(opobj[op_count].opcode,op_code);
            strcpy(opobj[op_count].opclass,"AD");
            opobj[op_count].code=3;
            opp1<<op_code<<" AD 03";
            mem=1;
            flag=1;
            op_count++;
        }
        elseif(strcmp(op_code,"DS")==0)
        {
            strcpy(opobj[op_count].opcode,op_code);
            strcpy(opobj[op_count].opclass,"DL");
            opobj[op_count].code=2;
            i=0;
            dj++;
            while(str[dj]!=' ' && str[dj]!='\0')
            {
                temp[i]=str[dj];
                i++;
                dj++;
            }
            mem=atoi(temp);
            ini<<"(DL,02)  (C,"<<mem<<")"<<endl;
            opp1<<op_code<<" DL 02";
            mem=atoi(temp);
            flag=1;
            op_count++;
        }
        elseif(strcmp(op_code,"DC")==0)
        {
            strcpy(opobj[op_count].opcode,op_code);
            strcpy(opobj[op_count].opclass,"DL");
            opobj[op_count].code=1;
            i=0;
            while(str[dj]!=' ' && str[dj]!='\0')
            {
                temp[i]=str[dj];
                i++;
                dj++;
            }
            mem=atoi(temp);
            flag=1;
            ini<<"(DL,01)  (C,"<<mem<<")"<<endl;
            opp1<<op_code<<" DL 01";
            op_count++;
        }
        opp1<<"\n";
        lc=lc+mem;
        line++;
        if(flag==0)
        {
            cout<<"\nERROR : line : "<<line-1<<" : Undefine symbol \""<<op_code<<"\"";
            com_flag=1;
            flag=0;
        }
    }
    if(com_flag==1)
    {
        cout<<"\nERROR : Do not create temp.obj";
        ini.close();
        ini.open("inter");
    }
    else
        cout<<"\nNo Error Found.... \nCompile Sccessfully.....";

    fin.close();
    ini.close();
/////////////////////////////////////////////////// TO CREATE OBJECT CODE FROM INTERMEDIATE CODE///////////////////////////////////////////////////

    iin.open("inter");
    finobj.open("temp.obj");
    char word1[10],word2[10],word3[10],word4[10],opp_code[2];
    int st_code,tm_addr,tmco;
    lc=add;
    iin.getline(str,80);
    while(iin.eof()==0)
    {
        iin.getline(str,80);
        if(iin.eof()!=0)
            break;
        i=1;
        j=0;
        while(str[i]!=',')
        {
            word1[j]=str[i];
            i++;
            j++;
        }
        word1[j]='\0';
        i++;
        j=0;
        while(str[i]!=')')
        {
            word2[j]=str[i];
            i++;
            j++;
        }
        word2[j]='\0';
        i=i+3;
        j=0;
        while(str[i]!=','&& str[i]!='\0')
        {
            word3[j]=str[i];
            i++;
            j++;
        }
        word3[j]='\0';
        i++;
        j=0;
        while(str[i]!='\0')
        {
            word4[j]=str[i];
            i++;
            j++;
        }
        while(j<8)
        {
            word4[j]=' ';
            j++;
        }
        word4[j]='\0';
        finobj<<"\n";
        if(strcmp(word1,"DL")==0)
        {
            if(strcmp(word2,"02")==0)
            {
                finobj<<lc<<")";
            }
            else