-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinsHelp.cpp
191 lines (176 loc) · 5.26 KB
/
cinsHelp.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "cinsHelp.h"
#include <string>
#include <iostream>
using namespace std;
cinsHelp::cinsHelp() {
dest[""] = "000";
dest["M"] = "001";
dest["D"] = "010";
dest["MD"] = "011";
dest["A"] = "100";
dest["AM"] = "101";
dest["AD"] = "110";
dest["AMD"] = "111";
comp["0"] = "0101010";
comp["1"] = "0111111";
comp["-1"] ="0111010";
comp["D"] = "0001100";
comp["A"] = "0110000";
comp["M"] = "1110000";
comp["!D"] ="0001101";
comp["!A"] ="0110001";
comp["!M"] ="1110001";
comp["-D"] ="0001111";
comp["-A"] ="0110011";
comp["-M"] ="1110011";
comp["D+1"]="0011111";
comp["A+1"]="0110111";
comp["M+1"]="1110111";
comp["D-1"]="0001110";
comp["A-1"]="0110010";
comp["M-1"]="1110010";
comp["D+A"]="0000010";
comp["D+M"]="1000010";
comp["D-A"]="0010011";
comp["D-M"]="1010011";
comp["A-D"]="0000111";
comp["M-D"]="1000111";
comp["D&A"]="0000000";
comp["D&M"]="1000000";
comp["D|A"]="0010101";
comp["D|M"]="1010101";
jump[""] = "000";
jump["JGT"]="001";
jump["JEG"]="010";
jump["JGE"]="011";
jump["JLT"]="100";
jump["JNE"]="101";
jump["JLE"]="110";
jump["JMP"]="111";
compstring = "";
deststring = "";
jumpstring = "";
}
bool cinsHelp::isCompBits(string cstring) {
if(comp.find(cstring) != comp.end()) return true;
return false;
}
bool cinsHelp::isDestBits(string dstring) {
if(dest.find(dstring) != dest.end()) return true;
return false;
}
bool cinsHelp::isJumpBits(string jstring) {
if(jump.find(jstring) != jump.end()) {
return true;
}
else {
return false;
}
}
void cinsHelp::split(string instruction) {
//Test to see which of our strings should be present in the line
bool equals = false;
bool semicolon = false;
for(int i = 0; i < instruction.size(); i++) {
if(instruction[i] == '=') equals = true;
if(instruction[i] == ';') semicolon = true;
}
//If both are true, then there should be all 3 in the string
if(equals && semicolon) {
int i;
i = 0;
while(instruction[i] != '=' && instruction[i] != ';') {
deststring += instruction[i];
i++;
}
int j;
j = i;
if(instruction[j] == '=') {
j++;
//This means that there is a comp feature
while(instruction[j] != '\0'){
if(instruction[j] == ';') {
break;
}
compstring += instruction[j];
j++;
}
if(instruction[j]==';') {
j++;
//There is a jump and comp
while(instruction[j] != '\0') {
jumpstring += instruction[j];
j++;
}
}
}
else if(instruction[j] = ';') {
//This means that there is only a dest and a jump in the Cinstruction
j++;
while(instruction[j] != '\0'){
jumpstring += instruction[j];
j++;
}
}
else {
//This means there is no comp or jump
}
}
else if(equals && !semicolon) { //There is an equals sigh and no semicolon, meaning there should be no JUMP
int i = 0;
while(instruction[i] != '=') {
deststring += instruction[i];
i++;
}
int j;
j = i;
if(instruction[j] == '=') {
j++;
//This means that there is a comp feature
while(instruction[j] != '\0'){
compstring += instruction[j];
j++;
}
}
}
else if(semicolon && !equals) { //There is no equals and a semicolon, so there is no dest only a comp and a JUMP
int i = 0;
while(instruction[i] != ';') {
compstring += instruction[i];
i++;
}
int j;
j = i;
if(instruction[j] == ';') {
j++;
//This means that there is a comp feature
while(instruction[j] != '\0'){
jumpstring += instruction[j];
j++;
}
}
}
//Was running into some issues with strings adding a space to the end, so I made this to make sure there is no extra space
if(isspace(compstring[compstring.size()-1])) compstring.pop_back();
if(isspace(jumpstring[jumpstring.size()-1])) jumpstring.pop_back();
if(isspace(deststring[deststring.size()-1])) deststring.pop_back();
}
//need to check between jump or not, if there is no equals then there is only a comp and jump
bool cinsHelp::isCinstruction(string instruction) {
compstring = "";
deststring = "";
jumpstring = "";
split(instruction);
/*cout << "comp: " << compstring << " " << compstring.size() << endl;
cout << "jump: " << jumpstring << " " << jumpstring.size() << endl;
cout << "dest: " << deststring << " " << deststring.size() << endl; */
if(isCompBits(compstring) && isJumpBits(jumpstring) && isDestBits(deststring)) return true;
return false;
}
string cinsHelp::CinstructionToBits() {
string res = "111";
res += comp[compstring];
res += dest[deststring];
res += jump[jumpstring];
return res;
}