generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.cpp
166 lines (152 loc) · 4.71 KB
/
assembler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <bitset>
#include<list>
#include<map>
using namespace std;
class instr_map{
map<string,string> instr;
public:
instr_map()
{string inpath="encodings/instruction.csv";
ifstream inputFile(inpath);
string instruction;
while (getline(inputFile, instruction)) {
int first_comma=instruction.find(',');
instr[instruction.substr(0,first_comma)]=(instruction.substr(first_comma+1));
}
}
string get_info(string operation){
return instr[operation];
}
}imap;
class reg_map{
map<string,string> register_mapping;
public:
reg_map(){
string inpath="encodings/register.csv";
ifstream inputFile(inpath);
string regdef;
while (getline(inputFile, regdef)) {
int first_comma=regdef.find(',');
register_mapping[regdef.substr(0,first_comma)]=(regdef.substr(first_comma+1));
}
}
string get_binary(string reg){
return register_mapping[reg];
}
}rmap;
class symtab{
public:
map<string,int> symbol_map;
int get_label_line(string label){
return symbol_map[label];
}
}symtab1;
class encoder{
public:
string rencoding(vector<string> desc_tokens,vector<string> instr_seg){
return desc_tokens[3]+rmap.get_binary(instr_seg[3])+rmap.get_binary(instr_seg[2])+desc_tokens[2]+rmap.get_binary(instr_seg[1])+desc_tokens[1];
}
string iencoding(vector<string> desc_tokens,vector<string> instr_seg){
int imme=stoi(instr_seg[3]);
string imm=bitset<12>(imme).to_string();
return imm+rmap.get_binary(instr_seg[2])+desc_tokens[2]+rmap.get_binary(instr_seg[1])+desc_tokens[1];
}
string sencoding(vector<string> desc_tokens,vector<string> instr_seg){
string imm=bitset<12>(stoi(instr_seg[2])).to_string();
return imm.substr(0,7)+rmap.get_binary(instr_seg[1])+rmap.get_binary(instr_seg[3])+desc_tokens[2]+imm.substr(7)+desc_tokens[1];
}
string bencoding(vector<string> desc_tokens,vector<string> instr_seg,int instruction_number){
string label=instr_seg[3];
int label_def=symtab1.symbol_map[label];
int immi=(label_def-instruction_number);
string imm=bitset<12>(immi).to_string();//shift by 2 in further code
// cout<<immi<<" "<<imm<<endl;
return imm[0]+imm.substr(2,6)+rmap.get_binary(instr_seg[2])+rmap.get_binary(instr_seg[1])+desc_tokens[2]+imm.substr(8)+imm[1]+desc_tokens[1];
}
//jump and u type maybe later
}encoder1;
//helper functions
//check if the instruction is label or instruction
bool is_label(string instruction){
return instruction.back()==':';
}
void make_symbol_table(string inpath){
ifstream inputFile(inpath);
string instruction;
int count=0;
//list <string> labels_need_to_add;
//cout<<"yes"<<endl;
while (getline(inputFile, instruction)) {
//cout<<"yes"<<endl;
//cout<<count<<endl;
if(is_label(instruction)){
instruction.pop_back();
symtab1.symbol_map[instruction]=count+1;
}else{
count++;
}
}
}
string encoding(string desc,vector<string> instr_seg,int instruction_count){
//tokenize desc
vector <string> desc_tokens;
stringstream ss(desc);
string token;
while (getline(ss, token, ',')) {
desc_tokens.push_back(token);
}
if(desc[0]=='R'){
return encoder1.rencoding(desc_tokens,instr_seg);
}
else if(desc[0]=='I'){
return encoder1.iencoding(desc_tokens,instr_seg);
}
else if(desc[0]=='S'){
return encoder1.sencoding(desc_tokens,instr_seg);
}
else if(desc[0]='B'){
//patch the labels->need line number
return encoder1.bencoding(desc_tokens,instr_seg,instruction_count);
}
}
vector<string> tokenize(string instruction){
vector <string> instr_seg;
int first_space=instruction.find(' ');
instr_seg.push_back(instruction.substr(0,first_space));
string operant=instruction.substr(first_space+1);
stringstream ss(operant);
string token;
while (getline(ss, token, ',')) {
instr_seg.push_back(token);
}
return instr_seg;
}
string encode(string instruction,int instruction_count){
vector<string> instr_seg=tokenize(instruction);
//cout<<instr_seg[0]<<" "<<typeid(instr_seg[0]).name()<<endl;
string desc=imap.get_info(instr_seg[0]);
return encoding(desc,instr_seg,instruction_count);
}
void assemble(string inpath,string opath){
make_symbol_table(inpath);
//string fileContents;
ifstream inputFile(inpath);
ofstream outputFile(opath);
string instruction;
int instruction_count=0;
while (getline(inputFile, instruction)) {
if(!is_label(instruction)){
instruction_count++;
outputFile <<encode(instruction,instruction_count)<<endl;
}
}
}
int main() {
assemble("editor.txt","machinecode.txt");
cout<<"successfully assembeled"<<endl;
}