Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <map>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #define HEX 1
- #define BIN 0
- using namespace std;
- class Instruction
- {
- private:
- char format;
- string name;
- string line;
- string operand1;
- string operand2;
- string destination;
- int opcode;
- int shamt,funct,address,jaddress,base,currentPC;
- map<string,char> NameToFormat;
- map<string,int> NameToOpcode;
- map<string,int> RegisterToCode;
- map<string,string> BinToHex;
- map<string,int> NameToFunct;
- static map<std::string,int> JumpLabels;
- string code;
- int strToInt(string s)
- {
- int num=0;
- int i=0;
- if(s[i]=='-')i++;
- while(i<int(s.size()))
- {
- num*=10;num+=s[i++]-'0';
- }
- if(s[0]=='-')num=-num;
- return num;
- }
- string bits(int x,int b)
- {
- string ret="";
- if(x>=0)
- {
- while(b--)
- {
- char c='0';
- if(x%2)c='1';
- ret=c+ret;
- x/=2;
- }
- }
- else
- {
- x=-x;
- while(b--)
- {
- char c='0';
- if(x%2)c='1';
- ret=c+ret;
- x/=2;
- }
- for(int i=0;i<int(ret.size());i++)ret[i]='1'-ret[i]+'0';
- for(int i=ret.size()-1;i>=0;i--)
- {
- if(ret[i]=='1')ret[i]='0';
- else{ret[i]='1';break;}
- }
- }
- return ret;
- }
- string Hex(string s)
- {
- string ret="";
- for(int i=0;i<int(s.size());i+=4)
- {
- string cur="";
- for(int j=i;j<i+4;j++)cur+=s[j];
- ret+=BinToHex[cur];
- }
- return ret;
- }
- bool validChar(char c)
- {
- if(c==' ' || c=='\n' || c=='\r' || c=='\t' || c==',' || c=='(' || c==')')return false;
- return true;
- }
- void setBase(int code_base)
- {
- base=code_base;
- }
- void setData()
- {
- NameToOpcode["add"]=0x00;
- NameToOpcode["addi"]=0x08;
- NameToOpcode["and"]=0x00;
- NameToOpcode["andi"]=0x0C;
- NameToOpcode["beq"]=0x04;
- NameToOpcode["jal"]=0x03;
- NameToOpcode["jr"]=0x00;
- NameToOpcode["j"]=0x02;
- NameToOpcode["nor"]=0x00;
- NameToOpcode["or"]=0x00;
- NameToOpcode["ori"]=0x0D;
- NameToOpcode["slt"]=0x00;
- NameToOpcode["sll"]=0x00;
- NameToOpcode["lw"]=0x23;
- NameToOpcode["sw"]=0x2B;
- //=======================//
- NameToFormat["add"]='R';
- NameToFormat["addi"]='I';
- NameToFormat["and"]='R';
- NameToFormat["andi"]='I';
- NameToFormat["beq"]='I';
- NameToFormat["jal"]='J';
- NameToFormat["jr"]='R';
- NameToFormat["j"]='J';
- NameToFormat["nor"]='R';
- NameToFormat["or"]='R';
- NameToFormat["ori"]='I';
- NameToFormat["slt"]='R';
- NameToFormat["sll"]='R';
- NameToFormat["lw"]='I';
- NameToFormat["sw"]='I';
- //======================//
- RegisterToCode["$zero"]=0;
- RegisterToCode["$at"]=1;
- RegisterToCode["$v0"]=2;
- RegisterToCode["$v1"]=3;
- RegisterToCode["$a0"]=4;
- RegisterToCode["$a1"]=5;
- RegisterToCode["$a2"]=6;
- RegisterToCode["$a3"]=7;
- RegisterToCode["$t0"]=8;
- RegisterToCode["$t1"]=9;
- RegisterToCode["$t2"]=10;
- RegisterToCode["$t3"]=11;
- RegisterToCode["$t4"]=12;
- RegisterToCode["$t5"]=13;
- RegisterToCode["$t6"]=14;
- RegisterToCode["$t7"]=15;
- RegisterToCode["$s0"]=16;
- RegisterToCode["$s1"]=17;
- RegisterToCode["$s2"]=18;
- RegisterToCode["$s3"]=19;
- RegisterToCode["$s4"]=20;
- RegisterToCode["$s5"]=21;
- RegisterToCode["$s6"]=22;
- RegisterToCode["$s7"]=23;
- RegisterToCode["$t8"]=24;
- RegisterToCode["$t9"]=25;
- RegisterToCode["$k0"]=26;
- RegisterToCode["$k1"]=27;
- RegisterToCode["$gp"]=28;
- RegisterToCode["$sp"]=29;
- RegisterToCode["$s8"]=30;
- RegisterToCode["$fp"]=30;
- RegisterToCode["$ra"]=31;
- //=======================//
- NameToFunct["add"]=0x20;
- NameToFunct["and"]=0x24;
- NameToFunct["jr"]=0x08;
- NameToFunct["nor"]=0x27;
- NameToFunct["or"]=0x25;
- NameToFunct["slt"]=0x2A;
- NameToFunct["sll"]=0x00;
- //======================//
- BinToHex["0000"]="0";
- BinToHex["0001"]="1";
- BinToHex["0010"]="2";
- BinToHex["0011"]="3";
- BinToHex["0100"]="4";
- BinToHex["0101"]="5";
- BinToHex["0110"]="6";
- BinToHex["0111"]="7";
- BinToHex["1000"]="8";
- BinToHex["1001"]="9";
- BinToHex["1010"]="A";
- BinToHex["1011"]="B";
- BinToHex["1100"]="C";
- BinToHex["1101"]="D";
- BinToHex["1110"]="E";
- BinToHex["1111"]="F";
- }
- void readLine(string s)
- {
- line = s;
- //getline(cin,line);
- }
- void setJumpLabels(map<string, int> map)
- {
- JumpLabels = map;
- }
- string readItem()
- {
- string item="";
- int i=0;
- while(i<int(line.size()) && !validChar(line[i]))i++;
- while(i<int(line.size()) && validChar(line[i]))item+=line[i++];
- while(i<int(line.size()) && !validChar(line[i]))i++;
- line=line.substr(i);
- return item;
- }
- void setName()
- {
- name=readItem();
- }
- void setFormat()
- {
- format=NameToFormat[name];
- }
- void setOpcode()
- {
- opcode=NameToOpcode[name];
- }
- void setOperands()
- {
- if(name=="lw" || name=="sw")
- {
- operand2=readItem();
- address=strToInt(readItem());
- operand1=readItem();
- }
- else if(name=="sll")
- {
- destination=readItem();
- operand2=readItem();
- shamt=strToInt(readItem());
- }
- else if(name == "jr")
- {
- operand1=readItem();
- }
- else if(name == "beq")
- {
- operand1=readItem();
- operand2=readItem();
- address=JumpLabels[readItem()] - (currentPC / 4) - 1;
- }
- else if(format=='R')
- {
- destination=readItem();
- operand1=readItem();
- operand2=readItem();
- }
- else if(format=='I')
- {
- operand2=readItem();
- operand1=readItem();
- address=strToInt(readItem());
- }
- else if(format == 'J')
- {
- jaddress = JumpLabels[readItem()];
- }
- }
- void setEncoding()
- {
- code="";
- if(format=='I')
- {
- code+=bits(opcode,6);
- code+=bits(RegisterToCode[operand1],5);
- code+=bits(RegisterToCode[operand2],5);
- code+=bits(address,16);
- }
- else if(format=='R')
- {
- code+=bits(opcode,6);
- code+=bits(RegisterToCode[operand1],5);
- code+=bits(RegisterToCode[operand2],5);
- code+=bits(RegisterToCode[destination],5);
- code+=bits(shamt,5);
- code+=bits(NameToFunct[name],6);
- }
- else if (format == 'J')
- {
- code+=bits(opcode, 6);
- code+=bits(jaddress,26);
- }
- }
- void setCurrentPC(int x)
- {
- currentPC = x;
- }
- public:
- Instruction()
- {
- shamt=0;
- opcode=0;
- operand1 = "";
- operand2 = "";
- destination = "";
- name = "";
- format = 0;
- funct = 0;
- address = 0;
- jaddress = 0;
- }
- string getCode()
- {
- if(base==BIN)return code;
- else return Hex(code);
- }
- void process(string readline, map<std::string, int> map, int code_base, int pc) //helper function to call of the private functions
- {
- setBase(code_base);
- setCurrentPC(pc);
- setData();
- readLine(readline);
- setJumpLabels(map);
- setName();
- setFormat();
- setOpcode();
- setOperands();
- setEncoding();
- }
- };
- std::map<std::string, int> Instruction::JumpLabels; //that line was necessary for the map to work
- int main()
- {
- freopen("Input.txt", "r", stdin);
- freopen("Output.list","w",stdout);
- map<std::string,int> JumpLabels;
- std::vector<string> v;
- string line;
- int n =0;
- while(!cin.eof()) //get the jump labels then populate the instruction vector
- {
- getline(cin, line);
- int i=0;
- i = line.find(":");
- if(i != -1)
- {
- JumpLabels[line.substr(0,i)] = n;
- line=line.substr(i+1);
- }
- if(line == "" || line == " ") continue;
- v.push_back(line);
- n++;
- }
- int i = 0; //represents instruction number
- while(i < int(v.size()))
- {
- Instruction X;
- X.process(v[i], JumpLabels, HEX, i*4); //we only pass the instruction line and then jump labels map
- cout<<X.getCode()<<endl;
- i++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement