Professional Documents
Culture Documents
Roll No. : 05
Batch : Batch - 1
Experiment No. : 02
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)
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.
CODE:
PASS 1:
#include <bits/stdc++.h>
using namespace std;
struct OPtab
{
string OPcode;
string Mclass;
string mNemonic;
};
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;
};
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;
};
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;
};
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;
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++;
}
}
{
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;
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 != "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";
}
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++;
}
}
cout << " " << Label << "\t" << OPcode << "\t" << OP1 << "\t" << OP2 << "\t" <<
LineCount2 << "\t" << IntCode << endl;
Inp << LineCount2 << "\t" << IntCode << endl;
}
PASS 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ifstream IntCode, SymTab, LitTab;
IntCode.open("IntermediateCode.txt"); SymTab.open("SymTable.txt");
LitTab.open("LitTable.txt");
ofstream MacCode;
MacCode.open("Machine Code.txt");
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;
INPUT:
Input.txt
OUTPUT:
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)
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
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.