-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path448_oops.cpp
142 lines (135 loc) · 2.88 KB
/
448_oops.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
// Xiaoyan Wang 9/30/2016
#include <iostream>
#include <string>
// #include <deque>
#include <iterator>
using namespace std;
istream_iterator<char> iter(cin), the_end;
unsigned chartoint(const char& c);
string operand();
int main() {
// ios::sync_with_stdio(false);
while(iter != the_end) {
if(*iter == '\n') {
++iter;
continue;
}
char operation = *iter++;
// cout << "operation = " << operation << endl;
switch(operation) {
case '0':
cout << "ADD " << operand();
cout << ',' << operand() << '\n'; // seperate into two lines to enfore order of execution
break;
case '1':
cout << "SUB " << operand();
cout << ',' << operand() << '\n';
break;
case '2':
cout << "MUL " << operand();
cout << ',' << operand() << '\n';
break;
case '3':
cout << "DIV " << operand();
cout << ',' << operand() << '\n';
break;
case '4':
cout << "MOV " << operand();
cout << ',' << operand() << '\n';
break;
case '5':
cout << "BREQ " << operand() << '\n';
break;
case '6':
cout << "BRLE " << operand() << '\n';
break;
case '7':
cout << "BRLS " << operand() << '\n';
break;
case '8':
cout << "BRGE " << operand() << '\n';
break;
case '9':
cout << "BRGR " << operand() << '\n';
break;
case 'A':
cout << "BRNE " << operand() << '\n';
break;
case 'B':
cout << "BR " << operand() << '\n';
break;
case 'C':
cout << "AND " << operand();
cout << ',' << operand();
cout << ',' << operand() << '\n';
break;
case 'D':
cout << "OR " << operand();
cout << ',' << operand();
cout << ',' << operand() << '\n';
break;
case 'E':
cout << "XOR " << operand();
cout << ',' << operand();
cout << ',' << operand() << '\n';
break;
case 'F':
cout << "NOT " << operand() << '\n';
break;
}
}
cout << flush;
return 0;
}
string operand() {
string result;
char first;
if(iter == the_end)
first = '0';
else if(*iter == '\n') {
++iter;
first = *iter++;
}
else
first = *iter++;
// first -= '0';
// cout << "First = " << first << endl;
unsigned firstint = chartoint(first);
// cout << "firstint = " << (firstint >> 2) << endl;
switch(firstint >> 2) {
case 0:
result.push_back('R');
break;
case 1:
result.push_back('$');
break;
case 2:
result += "PC+";
break;
}
unsigned num = (firstint & 0x3) << 12;
// cout << "num = " << num << endl;
for(int i = 8; i >= 0; i -= 4) {
char temp;
if(iter == the_end)
temp = '0';
else if(*iter == '\n') {
++iter;
temp = *iter++;
}
else
temp = *iter++;
// cout << *iter << endl;
num |= (chartoint(temp) << i);
// cout << num << endl;
}
return result + to_string(num);
}
inline unsigned chartoint(const char& c) {
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'A' && c <= 'F')
return c - 'A' + 10;
else
return c - 'a' + 10;
}