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: 2297

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
            {
                len=strlen(word4);
                word4[len-1]='\0';
                tmco=atoi(word4);
                finobj<<lc<<") 00 0 "<<tmco;
            }
            lc=lc+atoi(word4);
        }
        elseif(strcmp(word1,"IS")==0)
        {
            if(strcmp(word3," AX")==0)
               st_code=1;
            elseif(strcmp(word3," BX")==0)
                st_code=2;
            elseif(strcmp(word3," CX")==0)
                st_code=3;
            elseif(strcmp(word3," DX")==0)
                st_code=4;
            elseif(strcmp(word3," LT")==0)
                st_code=1;
            elseif(strcmp(word3," LE")==0)
                st_code=2;
            elseif(strcmp(word3," EQ")==0)
                st_code=3;
            elseif(strcmp(word3," GT")==0)
                st_code=4;
            elseif(strcmp(word3," GE")==0)
                st_code=5;
            elseif(strcmp(word3," ANY")==0)
                st_code=6;

            j=0;
            flag=0;
            char te_sym[8];
            if(strcmp(word2,"09")!=0)
            {
                while(j<sym_count)
                {
                    if(strcmp(symobj[j].symbol,word4)==0)
                    {
                        tm_addr=symobj[j].addr;
                        flag=1;
                    }
                    j++;
                }
                finobj<<lc<<") "<<word2<<" "<<st_code<<" "<<tm_addr;
            }
            else
            {
                while(j<sym_count)
                {
                    i=0;
                    te_sym[0]=' ';
                    i=1;
                    while(symobj[j].symbol[i-1]!=' ')
                    {
                        te_sym[i]=symobj[j].symbol[i-1];
                        i++;
                    }
                    te_sym[i]='\0';
                    if(strcmp(te_sym,word3)==0)
                    {
                        tm_addr=symobj[j].addr;
                        flag=1;
                    }
                    j++;
                }
                finobj<<lc<<") "<<word2<<" "<<tm_addr;
            }
            lc++;
        }
    }
}
/*
SOURCE PROGRAM

START 200
A. DC 2
B. DS 1
MOV AX,A
MOV BX,B
ADD AX,A
END

SYMBOL TABLE

A 200 2
B 202 1

OPERATOR TABLE

DC DL 01
DS DL 02
MOV IS 04
MOV IS 04
ADD IS 01
END AD 02

INTERMEDIATE CODE

(AD,01) (C,200)
(DL,01) (C,2)
(DL,02) (C,1)
(IS,04) AX,A
(IS,04) BX,B
(IS,01) AX,A

OBJECT FILE

200) 00 0 2
202)
203) 04 1 0
204) 04 2 0
205) 01 1 0
*/
  
Share: 


Didn't find what you were looking for? Find more on Program of an ASSEMBLER Or get search suggestion and latest updates.

James Evans
James Evans author of Program of an ASSEMBLER is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!