Question: Need the DECIMAL-TO-BINARY CONVERSION #include #include #include #include #include #include using namespace std; using namespace boost; #define NumberOfInst 100 int length = 0; vector ASM;
Need the DECIMAL-TO-BINARY CONVERSION
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace boost;
#define NumberOfInst 100
int length = 0;
vector
vector
int find_str(string a, string keyword, int nth)
{
iterator_range
return distance(a.begin(), r.begin());
}
string R_name(string r)
{
string R;
int r_int;
int r_ones;
int r_tens;
if (r[1] == ',')//Reg_name has one digit
{
r_int = (int)r[0] - 48; //char to int, by subtracting ASCII of '0'
}
else //Reg_name has two digit
{
r_ones = (int)r[1] - 48;
r_tens = (int)r[0] - 48;
r_int = 10*r_tens + r_ones;
}
bitset<5> b(r_int);
R = b.to_string();
return R;
}
void readASM()
{
ASM.resize(NumberOfInst);
string line;
ifstream myfile ("Assembly_Code.txt");
int i = 0;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
ASM[i] = line;
i++;
}
myfile.close();
}
else cout << "Unable to open file";
length = i;
}
int writeBIN(vector
{
ofstream myfile;
myfile.open("Bin_code.txt",std::ios_base::app);
int j=0;
if (myfile.is_open())
{
while(j { myfile << BIN[j] << endl; j++; } } else cout<<"Unable to open file"; myfile.close(); return 0; } string Addr_extend(string Addr) { return (bitset<26> (Addr)).to_string(); } string Imm_extend(string Imm) { return (bitset<16> (Imm)).to_string(); } int main () { string InstName; int Blank_pos;//index of ' ' int Rs_pos;//index of Rs int Rt_pos; int Rd_pos; int Imm_pos; string opcode; string Rs; string Rt; string Rd; string Shamt; string Func; string Imm; string Addr; string temp; int i=0; readASM(); for(i=0;i { Blank_pos = find_str(ASM[i], " ", 0); InstName = ASM[i].substr(0,Blank_pos); if (InstName == "ADD" | InstName == "SUB" | InstName == "AND" | InstName == "OR" | InstName == "NOR") {//R-Type Rs_pos = find_str(ASM[i], " R", 0)+2; Rt_pos = find_str(ASM[i], " R", 1)+2; Rd_pos = find_str(ASM[i], " R", 2)+2; //Syntax Check, to be done, optional, using str.find_first_not_of opcode = "000000"; Rs = R_name(ASM[i].substr(Rs_pos,Rs_pos+1)); Rt = R_name(ASM[i].substr(Rt_pos,Rt_pos+1)); Rd = R_name(ASM[i].substr(Rd_pos,Rd_pos+1)); Shamt = "00000"; if(InstName == "ADD") Func = "000001"; else if(InstName == "SUB") Func = "000011"; else if(InstName == "AND") Func = "000101"; else if(InstName == "OR") Func = "000111"; else if(InstName == "NOR") Func = "001001"; BIN.resize(32); BIN[i] = opcode.append(Rs).append(Rt).append(Rd).append(Shamt).append(Func); } else if(InstName == "JMP") { opcode = "001100"; Addr = ASM[i].substr(5, ASM[i].length() - 1); BIN.resize(32); BIN[i] = opcode + Addr_extend(Addr); } else if(InstName == "HAL") { BIN.resize(32); BIN[i] = "11111111111111111111111111111111"; } else {//I-Type Rs_pos = find_str(ASM[i], " R", 0)+2; Rt_pos = find_str(ASM[i], " R", 1)+2; Rs = R_name(ASM[i].substr(Rs_pos,Rs_pos+1)); Rt = R_name(ASM[i].substr(Rt_pos,Rt_pos+1)); Imm_pos = find_str(ASM[i], ", ", 1)+2; Imm = ASM[i].substr(Imm_pos, ASM[i].length() - 1); if(InstName == "ADDI") opcode = "000001"; else if(InstName == "SUBI") opcode = "000010"; else if(InstName == "ANDI") opcode = "000011"; else if(InstName == "ORI") opcode = "000100"; else if(InstName == "SHL") opcode = "000101"; else if(InstName == "LB") opcode = "000111"; else if(InstName == "SB") opcode = "001000"; else if(InstName == "BLT") opcode = "001001"; else if(InstName == "BEQ") opcode = "001010"; else if(InstName == "BNE") opcode = "001011"; BIN.resize(32); BIN[i] = opcode + Rs + Rt + Imm_extend(Imm); } } writeBIN(BIN); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
