-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.cpp
82 lines (70 loc) · 2.48 KB
/
Code.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
#include "Code.h"
using namespace std;
string Code::dest(string mnemonic)
{
bitset<3> destBinary;
bitset<3> m_dest(string("001")); // Destination of Memory[A]
bitset<3> d_dest(string("010")); // Destination of D register
bitset<3> a_dest(string("100")); // Destination of A register
// If destination contains Memory[A]
if (mnemonic.find('M') != string::npos) {
destBinary ^= m_dest;
}
// If destination contains D register
if (mnemonic.find('D') != string::npos) {
destBinary ^= d_dest;
}
// If destination contains A register
if (mnemonic.find('A') != string::npos) {
destBinary ^= a_dest;
}
string s_dest = destBinary.to_string();
return s_dest;
}
string Code::comp(string mnemonic)
{
string s_comp;
// Set a-bit to 1
if (mnemonic.find('M') != string::npos) {
replace(mnemonic.begin(), mnemonic.end(), 'M', 'A');
s_comp.append("1");
}
// Set a-bit to 0
else {
s_comp.append("0");
}
// Assigns string s_comp binary code of comp mnemonic
if (mnemonic == "0") s_comp.append("101010");
else if (mnemonic == "1") s_comp.append("111111");
else if (mnemonic == "-1") s_comp.append("111010");
else if (mnemonic == "D") s_comp.append("001100");
else if (mnemonic == "A") s_comp.append("110000");
else if (mnemonic == "!D") s_comp.append("001101");
else if (mnemonic == "!A") s_comp.append("110001");
else if (mnemonic == "-D") s_comp.append("001111");
else if (mnemonic == "-A") s_comp.append("110011");
else if (mnemonic == "D+1") s_comp.append("011111");
else if (mnemonic == "A+1") s_comp.append("110111");
else if (mnemonic == "D-1") s_comp.append("001110");
else if (mnemonic == "A-1") s_comp.append("110010");
else if (mnemonic == "D+A") s_comp.append("000010");
else if (mnemonic == "D-A") s_comp.append("010011");
else if (mnemonic == "A-D") s_comp.append("000111");
else if (mnemonic == "D&A") s_comp.append("000000");
else s_comp.append("010101");
return s_comp;
}
string Code::jump(string mnemonic)
{
string s_jump;
// Assigns string s_jump binary code of s_jump mnemonic
if (mnemonic == "JGT") s_jump.append("001");
else if (mnemonic == "JEQ") s_jump.append("010");
else if (mnemonic == "JGE") s_jump.append("011");
else if (mnemonic == "JLT") s_jump.append("100");
else if (mnemonic == "JNE") s_jump.append("101");
else if (mnemonic == "JLE") s_jump.append("110");
else if (mnemonic == "JMP") s_jump.append("111");
else s_jump.append("000");
return s_jump;
}