-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.cpp
More file actions
270 lines (207 loc) · 8.54 KB
/
asm.cpp
File metadata and controls
270 lines (207 loc) · 8.54 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "asm.hpp"
#include <sstream>
Offset::Offset(i32_t the_offset, Ptr ptr) {
std::stringstream buf;
buf << the_offset << '(' << Pointer[ptr] << ')';
_ident = buf.str();
}
Offset::Offset(i32_t the_offset, Reg reg) {
std::stringstream buf;
buf << the_offset << '(' << Register[reg] << ')';
_ident = buf.str();
}
namespace gas {
// push
void push(std::ostream& out, int num) {
out << "\tpush" << SUFFIX << "\t$" << num << std::endl;
}
void push(std::ostream& out, Reg r) {
out << "\tpush" << SUFFIX << "\t" << Register[r] << std::endl;
}
void push(std::ostream& out, Offset o) {
out << "\tpush" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
void push(std::ostream& out, Ptr p) {
out << "\tpush" << SUFFIX << "\t" << Pointer[p] << std::endl;
}
void push(std::ostream& out, const std::string& label) {
out << "\tpush" << SUFFIX << "\t$" << label << std::endl;
}
// pop
void pop(std::ostream& out, Reg r) {
out << "\tpop" << SUFFIX << "\t" << Register[r] << std::endl;
}
void pop(std::ostream& out, Offset o) {
out << "\tpop" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
void pop(std::ostream& out, Ptr p) {
out << "\tpop" << SUFFIX << "\t" << Pointer[p] << std::endl;
}
// inc
void inc(std::ostream& out, Reg r) {
out << "\tinc" << SUFFIX << "\t" << Register[r] << std::endl;
}
void inc(std::ostream& out, Offset o) {
out << "\tinc" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
// dec
void dec(std::ostream& out, Reg r) {
out << "\tdec" << SUFFIX << "\t" << Register[r] << std::endl;
}
void dec(std::ostream& out, Offset o) {
out << "\tdec" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
// mov
void mov(std::ostream& out, int num, Reg r) {
out << "\tmov" << SUFFIX << "\t$" << num << ", " << Register[r] << std::endl;
}
void mov(std::ostream& out, int num, Offset o) {
out << "\tmov" << SUFFIX << "\t$" << num << ", " << o.getIdent() << std::endl;
}
void mov(std::ostream& out, const std::string& label, Reg r) {
out << "\tmovl" << SUFFIX << "\t$" << label <<", " << Register[r] << std::endl;
}
void mov(std::ostream& out, const std::string& label, Offset o) {
out << "\tmovl" << SUFFIX << "\t$" << label <<", " << o.getIdent() << std::endl;
}
void mov(std::ostream& out, Reg r1, Reg r2) {
out << "\tmov" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void mov(std::ostream& out, Reg r, Offset o) {
out << "\tmov" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
void mov(std::ostream& out, Offset o, Reg r) {
out << "\tmov" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
void mov(std::ostream& out, Ptr p1, Ptr p2) {
out << "\tmov" << SUFFIX << "\t" << Pointer[p1] << ", " << Pointer[p2] << std::endl;
}
// add
void add(std::ostream& out, int num, Ptr p) {
out << "\tadd" << SUFFIX << "\t$" << num << ", " << Pointer[p] << std::endl;
}
void add(std::ostream& out, int num, Reg r) {
out << "\tadd" << SUFFIX << "\t$" << num << ", " << Register[r] << std::endl;
}
void add(std::ostream& out, int num, Offset o) {
out << "\tadd" << SUFFIX << "\t$" << num << ", " << o.getIdent() << std::endl;
}
void add(std::ostream& out, Reg r1, Reg r2) {
out << "\tadd" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void add(std::ostream& out, Reg r, Offset o) {
out << "\tadd" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
void add(std::ostream& out, Offset o, Reg r) {
out << "\tadd" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
// sub
void sub(std::ostream& out, int num, Ptr p) {
out << "\tsub" << SUFFIX << "\t$" << num << ", " << Pointer[p] << std::endl;
}
void sub(std::ostream& out, int num, Reg r) {
out << "\tsub" << SUFFIX << "\t$" << num << ", " << Register[r] << std::endl;
}
void sub(std::ostream& out, int num, Offset o) {
out << "\tsub" << SUFFIX << "\t$" << num << ", " << o.getIdent() << std::endl;
}
void sub(std::ostream& out, Reg r1, Reg r2) {
out << "\tsub" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void sub(std::ostream& out, Reg r, Offset o) {
out << "\tsub" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
void sub(std::ostream& out, Offset o, Reg r) {
out << "\tsub" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
// imul
void imul(std::ostream& out, Reg r1, Reg r2) {
out << "\timul" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void imul(std::ostream& out, Offset o, Reg r) {
out << "\timul" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
// idiv
void idiv(std::ostream& out, Reg r) {
out << "\tidiv" << SUFFIX << "\t" << Register[r] << std::endl;
}
void idiv(std::ostream& out, Offset o) {
out << "\tidiv" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
// lea
void lea(std::ostream& out, Offset o, Reg r) {
out << "\tlea" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
// ret
void ret(std::ostream& out) {
out << "\tret" << std::endl;
}
// call
void call(std::ostream& out, const std::string& label) {
out << "\tcall\t" << label << std::endl;
}
// jmp
void jmp(std::ostream& out, JCond jc, const std::string& label) {
out << "\tj" << JumpPostFix[jc] << "\t\t" << label << std::endl;
}
// cmp
void cmp(std::ostream& out, int num, Reg r) {
out << "\tcmp" << SUFFIX << "\t$" << num << ", " << Register[r] << std::endl;
}
void cmp(std::ostream& out, int num, Offset o) {
out << "\tcmp" << SUFFIX << "\t$" << num << ", " << o.getIdent() << std::endl;
}
void cmp(std::ostream& out, Reg r1, Reg r2) {
out << "\tcmp" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void cmp(std::ostream& out, Offset o, Reg r) {
out << "\tcmp" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
void cmp(std::ostream& out, Reg r, Offset o) {
out << "\tcmp" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
// neg
void neg(std::ostream& out, Reg r) {
out << "\tneg" << SUFFIX << "\t" << Register[r] << std::endl;
}
void neg(std::ostream& out, Offset o) {
out << "\tneg" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
// not
void logic_not(std::ostream& out, Reg r) {
out << "\tnot" << SUFFIX << "\t" << Register[r] << std::endl;
}
void logic_not(std::ostream& out, Offset o) {
out << "\tnot" << SUFFIX << "\t" << o.getIdent() << std::endl;
}
// and
void logic_and(std::ostream& out, Reg r1, Reg r2) {
out << "\tand" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void logic_and(std::ostream& out, Reg r, Offset o) {
out << "\tand" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
void logic_and(std::ostream& out, Offset o, Reg r) {
out << "\tand" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
// or
void logic_or(std::ostream& out, Reg r1, Reg r2) {
out << "\tor" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void logic_or(std::ostream& out, Reg r, Offset o) {
out << "\tor" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
void logic_or(std::ostream& out, Offset o, Reg r) {
out << "\tor" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
// xor
void logic_xor(std::ostream& out, Reg r1, Reg r2) {
out << "\txor" << SUFFIX << "\t" << Register[r1] << ", " << Register[r2] << std::endl;
}
void logic_xor(std::ostream& out, Reg r, Offset o) {
out << "\txor" << SUFFIX << "\t" << Register[r] << ", " << o.getIdent() << std::endl;
}
void logic_xor(std::ostream& out, Offset o, Reg r) {
out << "\txor" << SUFFIX << "\t" << o.getIdent() << ", " << Register[r] << std::endl;
}
}