You are on page 1of 16

DEPARTMENT OF COMPUTER ENGINEERING

CSL601 System Programming and Compiler Construction Lab


Sixth Semester, 2022-2023 (Even Semester)

Name of Student : HET ANAND DAVE

Roll No. : 05

Batch : Batch - 1

Experiment No. : 02

Title of Experiment : To Study and Implement Assembler with Both Pass

Date of Conduction : 25/1/23

Date of Submission : 1/2/23

Max. Marks
Particulars
Marks Obtained
Preparedness and Efforts (PE) 3
Knowledge of Tools (KT) 3
Debugging and Results (DR) 3
Documentation (DN) 3
Punctuality & Lab Ethics (PL) 3
Total 15

Grades – Meet Expectations (3 Marks), Moderate Expectations (2 Marks), Below Expectations (1 Mark)

Checked and verified by

Name of Faculty : Mrs. Harsha Dave


Signature :
Date :

Dept. of Computer Engineering, Shree L. R. Tiwari College of Engineering, Thane-401107.


DEPARTMENT OF COMPUTER ENGINEERING

EXPERIMENT NO. 2
AIM: To Study and Implement Assembler with Both Pass.
THEORY:
Assembler’s Functions:
1) Convert mnemonic operation codes to their machine language equivalents
„ STL to 14
2) Convert symbolic operands (referred label) to their equivalent machine addresses
„ RETADR to 1033
3) Build the machine instructions in the proper format
4) Convert the data constants to internal machine representations
5) Write the object program and the assembly listing

SUMMARY:
Assembler directives (pseudo-instructions):
1) START, END, BYTE, WORD, RESB, RESW.
2) These statements are not translated into machine instructions.
3) Instead, they provide instructions to the assembler itself. Print user and group information for the
specified USERNAME, or (when USERNAME omitted) for the current user.
Data transfer (RD, WD):
1) A buffer is used to store record.
2) Buffering is necessary for different I/O rates.
3) The end of each record is marked with a null character (0016).
4) Buffer length is 4096 Bytes.
5) The end of the file is indicated by a zero-length record.
6) When the end of file is detected, the program writes
EOF on the output device and terminates by RSUB.

HET ANAND DAVE 3 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

Subroutines (JSUB, RSUB):


1) RDREC, WRREC
2) Save link (L) register first before nested jump
Translation functions:
1) Translate STL to 14.
2) Translate RETADR to 1033.
3) Build the machine instructions in the proper format (,X).
4) Translate EOF to 454F46.
5) Write the object program and assembly listing.
The functions of the two passes assembler:
Pass 1 (Define Symbol)
1) Assign addresses to all statements (generate LOC).
2) Check the correctness of Instruction (check with OP table).
3) Save the values (address) assigned to all labels into
4) SYMBOL table for Pass 2.
5) Perform some processing of assembler directives.
Pass 2
1) Assemble instructions (op code from OP table, address from SYMBOL table).
2) Generate data values defined by BYTE, WORD.
3) Perform processing of assembler directives not done during Pass 1.

CODE:
PASS 1:
#include <bits/stdc++.h>
using namespace std;

struct OPtab
{
string OPcode;
string Mclass;
string mNemonic;
};

struct OPtab optab[18] = {


{"MOVER", "IS", "01"},
{"MOVEM", "IS", "02"},
{"ADD", "IS", "03"},
{"SUB", "IS", "04"},
{"MULT", "IS", "05"},
{"DIV", "IS", "06"},
{"BI", "IS", "07"},
{"COMP", "IS", "08"},
{"PRINT", "IS", "09"},
{"READ", "IS", "10"},
{"START", "AD", "01"},

HET ANAND DAVE 4 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

{"END", "AD", "02"},


{"EQU", "AD", "03"},
{"ORIGIN", "AD", "04"},
{"LTORG", "AD", "05"},
{"DS", "DL", "01"},
{"DC", "DL", "02"}};

int getOP(string s)
{
for (int i = 0; i < 18; ++i)
{
if (optab[i].OPcode == s)
return i;
}
return -1;
}
int getRegID(string s)
{
if (s == "AREG")
return 1;
else if (s == "BREG")
return 2;
else if (s == "CREG")
return 3;
else if (s == "DREG")
return 4;
else
return (-1);
}
int getConditionCode(string s)
{
if (s == "LT")
return 1;
else if (s == "LE")
return 2;
else if (s == "EQ")
return 3;
else if (s == "GT")
return 4;
else if (s == "GE")
return 5;
else if (s == "ANY")
return 6;
else
return (-1);
}

struct SymTable
{
int No;
string SymbolName;
string Address;
};

struct SymTable ST[10];

HET ANAND DAVE 5 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

bool presentST(string s)
{
for (int i = 0; i < 10; ++i)
{
if (ST[i].SymbolName == s)
return true;
}
return false;
}
int getSymID(string s)
{
for (int i = 0; i < 10; ++i)
{
if (ST[i].SymbolName == s)
return i;
}
return (-1);
}
struct LitTable
{
int No;
string LiteralName;
string Address;
};

struct LitTable LT[10];

bool presentLT(string s)
{
for (int i = 0; i < 10; ++i)
{
if (LT[i].LiteralName == s)
return true;
}
return false;
}
int getLitID(string s)
{
for (int i = 0; i < 10; ++i)
{
if (LT[i].LiteralName == s)
return i;
}
return (-1);
}

struct poolTable
{
int no;
string L_No;
};

struct poolTable PT[10];


int main()
{
ifstream fin;

HET ANAND DAVE 6 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

fin.open("Input.txt");
ofstream Inp, SymTab, LitTab;
const char *path1 = "IntermediateCode.txt"; // empty space is filled with "NAN"
const char *path2 = "SymTable.txt";
const char *path3 = "LitTable.txt";
Inp.open(path1);
SymTab.open(path2);
LitTab.open(path3);
string Label, OPcode, OP1, OP2;
int SymCount = 0, LitCount = 0, N1Count = 0, PCount = 0, LineCount = 0;

cout << "\n -- ASSEMBLER PASS 1 OUTPUT --" << endl;


cout << "\n <LABEL\tOPCODE\tOP1\t\tOP2\tLine Count\tINTERMEDIATE CODE>" << endl;

while (!fin.eof())
{
fin >> Label >> OPcode >> OP1 >> OP2;
int id;
string IntCode, LineCount2;
id = getOP(OPcode);
IntCode = "(" + optab[id].Mclass + "," + optab[id].mNemonic + ") ";

if (OPcode == "START")
{
LineCount2 = "---";
if (OP1 != "NAN")
{
LineCount = stoi(OP1);
IntCode += "(C," + OP1 + ") NAN";
}
}

if (OPcode == "EQU")
{
LineCount2 = "---";
IntCode += " NAN NAN";
if (presentST(Label))
{
ST[getSymID(Label)].Address = ST[getSymID(OP1)].Address;
}
else
{
ST[SymCount].No = SymCount + 1;
ST[SymCount].SymbolName = Label;
ST[SymCount].Address = ST[getSymID(OP1)].Address;
SymCount++;
}
}

else if (Label != "NAN")


{
if (presentST(Label))
{
ST[getSymID(Label)].Address = to_string(LineCount);
}
else

HET ANAND DAVE 7 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

{
ST[SymCount].No = SymCount + 1;
ST[SymCount].SymbolName = Label;
ST[SymCount].Address = to_string(LineCount);
SymCount++;
}
}
if (OPcode == "ORIGIN")
{
string Token1, Token2;
char Operand;
stringstream ss(OP1);
size_t Found = OP1.find('+');
if (Found != string::npos)
Operand = '+';
else
Operand = '-';
getline(ss, Token1, Operand);
getline(ss, Token2, Operand);
LineCount2 = "---";
if (Operand == '+')
{
LineCount = stoi(ST[getSymID(Token1)].Address) + stoi(Token2);
IntCode += "(S,0" + to_string(ST[getSymID(Token1)].No) + ")+" +
Token2 + " NAN";
}
else
{
LineCount = stoi(ST[getSymID(Token1)].Address) - stoi(Token2);
IntCode += "(S,0" + to_string(ST[getSymID(Token1)].No) + ")-" +
Token2 + " NAN";
}
}

if (OPcode == "LTORG")
{
cout << " " << Label << "\t" << OPcode << "\t" << OP1 << "\t" << OP2 << "\t";
for (int i = LitCount - N1Count; i < LitCount; ++i)
{
LineCount2 = to_string(LineCount);
IntCode = "(DL,01) (C,";
string c(1, LT[i].LiteralName[2]);
IntCode += c + ") NAN";
LT[i].Address = to_string(LineCount);
LineCount++;
if (i < LitCount - 1)
cout << LineCount2 << "\t" << IntCode << "\n\t\t\t\t";
else
cout << LineCount2 << "\t" << IntCode << endl;
Inp << LineCount2 << "\t" << IntCode << endl;
}
PT[PCount].L_No = "#" + to_string(LT[LitCount - N1Count].No);
PT[PCount].no = PCount + 1;
PCount++;
N1Count = 0;
continue;

HET ANAND DAVE 8 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

if (OPcode == "END")
{
LineCount2 = "---";
IntCode += " NAN NAN";
cout << " " << Label << "\t" << OPcode << "\t" << OP1 << "\t" << OP2 <<
"\t" << LineCount2 << "\t" << IntCode << endl;
Inp << LineCount2 << "\t" << IntCode << endl;
if (N1Count)
{
for (int i = LitCount - N1Count; i < LitCount; ++i)
{
LineCount2 = to_string(LineCount);
IntCode = "(DL,01) (C,";
string c(1, LT[i].LiteralName[2]);
IntCode += c + ") NAN";
LT[i].Address = to_string(LineCount);
LineCount++;
cout << "\t\t\t\t" << LineCount2 << "\t" << IntCode << endl;
Inp << LineCount2 << "\t" << IntCode << endl;
}
PT[PCount].L_No = "#" + to_string(LT[LitCount - N1Count].No);
PT[PCount].no = PCount + 1;
PCount++;
}
break;
}

if (OPcode == "DC" || OPcode == "DS")


{
LineCount2 = to_string(LineCount);
if (OPcode == "DS")
{
IntCode += "(C," + OP1 + ") NAN";
LineCount += stoi(OP1);
}
else
{
string c(1, OP1[1]);
IntCode += "(C," + c + ") NAN";
LineCount++;
}
}

if (OPcode != "START" && OPcode != "END" && OPcode != "ORIGIN" && OPcode != "EQU"
&& OPcode != "LTORG" && OPcode != "DC" && OPcode != "DS")
{
if (OP2 == "NAN")
{
if (OP1 == "NAN")
{
LineCount2 = to_string(LineCount);
LineCount++;
IntCode += " NAN NAN";
}

HET ANAND DAVE 9 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

else
{
if (presentST(OP1))
{
IntCode += "(S,0" + to_string(ST[getSymID(OP1)].No) +
")";
LineCount2 = to_string(LineCount);
LineCount++;
}
else
{
ST[SymCount].No = SymCount + 1;
ST[SymCount].SymbolName = OP1;
SymCount++;
IntCode += "(S,0" + to_string(ST[getSymID(OP1)].No) +
")";
LineCount2 = to_string(LineCount);
LineCount++;
}
}
}
else
{
if (OPcode == "BC")
IntCode += "(" + to_string(getConditionCode(OP1)) + ") ";
else
IntCode += "(" + to_string(getRegID(OP1)) + ") ";

if (OP2[0] == '=')
{
LT[LitCount].No = LitCount + 1;
LT[LitCount].LiteralName = OP2;
LitCount++;
N1Count++;
IntCode += "(L,0" + to_string(LT[getLitID(OP2)].No) + ")";
}
else
{
if (presentST(OP2))
{
IntCode += "(S,0" + to_string(ST[getSymID(OP2)].No) +
")";
}
else
{
ST[SymCount].No = SymCount + 1;
ST[SymCount].SymbolName = OP2;
SymCount++;
IntCode += "(S,0" + to_string(ST[getSymID(OP2)].No) +
")";
}
}
LineCount2 = to_string(LineCount);
LineCount++;
}
}

HET ANAND DAVE 10 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

cout << " " << Label << "\t" << OPcode << "\t" << OP1 << "\t" << OP2 << "\t" <<
LineCount2 << "\t" << IntCode << endl;
Inp << LineCount2 << "\t" << IntCode << endl;
}

cout << "\n----------------------------------------------------------------" << endl;


cout << " -- SYMBOL TABLE --" << endl;
cout << "\n <NO.\tSYMBOL\tADDRESS>" << endl;
for (int i = 0; i < SymCount; ++i)
{
cout << " " << ST[i].No << "\t " << ST[i].SymbolName << "\t " << ST[i].Address
<< endl;
SymTab << ST[i].No << "\t " << ST[i].SymbolName << "\t" << ST[i].Address << endl;
}

cout << "\n----------------------------------------------------------------" << endl;


cout << " -- LITERAL TABLE --" << endl;
cout << "\n <NO.\tLITERAL\tADDRESS>" << endl;
for (int i = 0; i < LitCount; ++i)
{
cout << " " << LT[i].No << "\t " << LT[i].LiteralName << "\t " <<
LT[i].Address << endl;
LitTab << LT[i].No << "\t" << LT[i].LiteralName << "\t" << LT[i].Address << endl;
}

cout << "\n----------------------------------------------------------------" << endl;


cout << " -- POOL TABLE --" << endl;
cout << "\n <NO.\tLITERAL_NO.>" << endl;
for (int i = 0; i < PCount; ++i)
{
cout << " " << PT[i].no << "\t " << PT[i].L_No << endl;
}
return 0;
}

PASS 2:
#include <bits/stdc++.h>
using namespace std;

string Table(ifstream &fin, string n)


{
string Number, Name, Address;
while(fin >> Number >> Name >> Address)
{
if(Number == n)
{
fin.seekg(0, ios::beg);
return Address;
}
}
fin.seekg(0, ios::beg);
return "NAN";
}

int main()

HET ANAND DAVE 11 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

{
ifstream IntCode, SymTab, LitTab;
IntCode.open("IntermediateCode.txt"); SymTab.open("SymTable.txt");
LitTab.open("LitTable.txt");

ofstream MacCode;
MacCode.open("Machine Code.txt");

string LineCount, Inp1, Inp2, Inp3;


cout << "\n -- ASSEMBLER PASS 2 OUTPUT --" << endl;
cout << "\n LineCount\t <INTERMEDIATE CODE>\t\t\tLineCount\t <MACHINE CODE>" << endl;

while(IntCode >> LineCount >> Inp1 >> Inp2 >> Inp3)


{
string MachineCode;

if(Inp1.substr(1, 2) == "AD" || (Inp1.substr(1, 2) == "DL" && Inp1.substr(4, 2) ==


"02"))
MachineCode = " -No Machine Code-";

else if(Inp1.substr(1, 2) == "DL" && Inp1.substr(4, 2) == "01")


{
MachineCode = "00\t0\t00" + Inp2.substr(3, 1);
}

else
{
if(Inp1 == "(IS,00)")
MachineCode = Inp1.substr(4, 2) + "\t0\t000";
else if(Inp2.substr(1, 1) == "S")
MachineCode = Inp1.substr(4, 2) + "\t0\t" + Table(SymTab,
Inp2.substr(4, 1));
else
{
if(Inp3.substr(1, 1) == "S")
MachineCode = Inp1.substr(4, 2) + "\t" + Inp2.substr(1, 1) +
"\t" + Table(SymTab, Inp3.substr(4, 1));
else
MachineCode = Inp1.substr(4, 2) + "\t" + Inp2.substr(1, 1) +
"\t" + Table(LitTab, Inp3.substr(4, 1));
}
}

if(Inp1 == "(AD,03)")
{
cout << " " << LineCount << "\t" << Inp1 << "\t" << Inp2 << " " << Inp3 <<
"\t\t\t" << LineCount << "\t" << MachineCode << endl;
MacCode << LineCount << "\t" << MachineCode << endl;
continue;
}
cout << " " << LineCount << "\t" << Inp1 << "\t" << Inp2 << "\t " << Inp3 <<
"\t\t\t" << LineCount << "\t" << MachineCode << endl;
MacCode << LineCount << "\t" << MachineCode << endl;
}

return 0;

HET ANAND DAVE 12 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

INPUT:
Input.txt

NAN START 100 NAN


NAN MOVER AREG A
LOOP PRINT NAN BREG
NAN ADD BREG ='9'
NAN SUB BREG D
NAN COMP CREG ='23'
NAN LTORG NAN NAN
A DS 3 NAN
LABEL EQU LOOP NAN
L1 MULT CREG ='7'
NAN SUB BREG ='93'
NAN LTORG NAN NAN
B DC NAN 10
NAN MOVEM CREG ='7'
D DC NAN 8
NAN END NAN NAN

OUTPUT:

HET ANAND DAVE 13 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

HET ANAND DAVE 14 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

IntermediateCode.txt
--- (AD,01) (C,100) NAN
100 (IS,01) (1) (S,01)
101 (IS,09) (-1) (S,03)
102 (IS,03) (2) (L,01)
103 (IS,04) (2) (S,04)
104 (IS,08) (3) (L,02)
105 (DL,01) (C,9) NAN
106 (DL,01) (C,2) NAN
107 (DL,01) (C,3) NAN
--- (AD,03) NAN NAN
110 (IS,05) (3) (L,03)
111 (IS,04) (2) (L,04)

HET ANAND DAVE 15 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

112 (DL,01) (C,7) NAN


113 (DL,01) (C,9) NAN
114 (DL,02) (C,A) NAN
115 (IS,02) (3) (L,03)
116 (DL,02) (C,A) NAN
--- (AD,02) NAN NAN
117 (DL,01) (C,7) NAN

LitTable.txt
1 ='9' 105
2 ='23' 106
3 ='7' 112
4 ='93' 113
5 ='7' 117

SymTable.txt
1 A 107
2 LOOP 101
3 BREG
4 D 116
5 LABEL 101
6 L1 110
7 B 114

MachineCode.txt
--- -No Machine Code-
100 01 1 107
101 09 - 4
102 03 2 105
103 04 2 NAN

HET ANAND DAVE 16 ROLL NO-05


DEPARTMENT OF COMPUTER ENGINEERING

104 08 3 106
105 00 0 009
106 00 0 002
107 00 0 003
--- -No Machine Code-
110 05 3 112
111 04 2 113
112 00 0 007
113 00 0 009
114 -No Machine Code-
115 02 3 112
116 -No Machine Code-
--- -No Machine Code-
117 00 0 007

CONCLUSION: Hence, we have studied and implemented Assembler with Both Pass.

HET ANAND DAVE 17 ROLL NO-05

You might also like