-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.hpp
244 lines (210 loc) · 4.71 KB
/
asm.hpp
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
#ifndef ALPHA_ASM_HPP
#define ALPHA_ASM_HPP
#include <iostream>
#include "types.hpp"
#define BIT_SIZE 32
#define GAS 1
#if BIT_SIZE == 64
const char SUFFIX = 'q';
#elif BIT_SIZE == 32
const char SUFFIX = 'l';
#elif BIT_SIZE == 16
const char SUFFIX = 'w';
#elif BIT_SIZE == 8
const char SUFFIX = 'b';
#else
const char SUFFIX = 0;
#endif
enum Ptr {
#if BIT_SIZE == 32
E_SP,
E_BP,
#elif BIT_SIZE == 64
R_SP,
R_BP,
#endif
P_STACK = 0,
P_BASE = 1
};
enum Reg {
E_AX,
E_BX,
E_CX,
E_DX,
#if BIT_SIZE == 64
R_AX,
R_BX,
R_CX,
R_DX
#endif
};
enum Idx {
E_SI,
E_DI,
#if BIT_SIZE == 64
R_SI,
R_DI
#endif
};
enum JCond {
JMP_IMMEDIATE,
JMP_IF_ZERO,
JMP_IF_EQUAL,
JMP_IF_NOT_EQUAL,
JMP_IF_GREATER,
JMP_IF_GREATER_OR_EQUAL,
JMP_IF_LOWER,
JMP_IF_LOWER_OR_EQUAL,
JMP_IF_ABOVE,
JMP_IF_BELOW
};
const std::string Pointer[] = {
#if GAS
#if BIT_SIZE == 32
// 32 Bit
"%esp",
"%ebp",
#elif BIT_SIZE == 64
// 64 Bit
"%rsp",
"%rbp"
#endif
#else
#error "Unsupported Assembler"
#endif
};
const std::string Register[] = {
#if GAS
// 32 Bit
"%eax",
"%ebx",
"%ecx",
"%edx",
#if BIT_SIZE == 64
// 64 Bit
"%rax",
"%rbx",
"%rcx",
"%rdx"
#endif
#else
#error "Unsupported Assembler"
#endif
};
const std::string Index[] = {
#if GAS
// 32 Bit
"%esi",
"%edi",
#if BIT_SIZE == 64
// 64 Bit
"%rsi",
"%rdi"
#endif
#else
#error "Unsupported Assembler"
#endif
};
class Offset {
private:
std::string _ident;
public:
// Offset() = default;
explicit Offset(i32_t offset, Ptr ptr);
explicit Offset(i32_t offset, Reg reg);
const std::string& getIdent() const {
return _ident;
}
};
const std::string JumpPostFix[] = {
"mp",
"z",
"e",
"ne",
"g",
"ge",
"l",
"le",
"a",
"b",
};
namespace gas {
// push
void push(std::ostream&, i32_t num);
void push(std::ostream&, Reg r);
void push(std::ostream&, Offset o);
void push(std::ostream&, Ptr p);
void push(std::ostream&, const std::string&);
// pop
void pop(std::ostream&, Reg r);
void pop(std::ostream&, Offset o);
void pop(std::ostream&, Ptr p);
// inc
void inc(std::ostream&, Reg r);
void inc(std::ostream&, Offset o);
// dec
void dec(std::ostream&, Reg r);
void dec(std::ostream&, Offset o);
// mov
void mov(std::ostream&, i32_t num, Reg r);
void mov(std::ostream&, i32_t num, Offset o);
void mov(std::ostream&, const std::string&, Reg r);
void mov(std::ostream&, const std::string&, Offset o);
void mov(std::ostream&, Reg r1, Reg r2);
void mov(std::ostream&, Reg r, Offset o);
void mov(std::ostream&, Offset o, Reg r);
void mov(std::ostream&, Ptr p1, Ptr p2);
// add
void add(std::ostream&, i32_t num, Ptr p);
void add(std::ostream&, i32_t num, Reg r);
void add(std::ostream&, i32_t num, Offset o);
void add(std::ostream&, Reg r1, Reg r2);
void add(std::ostream&, Reg r, Offset o);
void add(std::ostream&, Offset o, Reg r);
// sub
void sub(std::ostream&, i32_t num, Ptr p);
void sub(std::ostream&, i32_t num, Reg r);
void sub(std::ostream&, i32_t num, Offset o);
void sub(std::ostream&, Reg r1, Reg r2);
void sub(std::ostream&, Reg r, Offset o);
void sub(std::ostream&, Offset o, Reg r);
// imul
void imul(std::ostream&, Reg r1, Reg r2);
void imul(std::ostream&, Offset o, Reg r);
// idiv
void idiv(std::ostream&, Reg r);
void idiv(std::ostream&, Offset o);
// lea
void lea(std::ostream&, Offset o, Reg r);
// ret
void ret(std::ostream&);
// call
void call(std::ostream&, const std::string&);
// jmp
void jmp(std::ostream&, JCond jc, const std::string&);
// cmp
void cmp(std::ostream&, i32_t num, Reg r);
void cmp(std::ostream&, i32_t num, Offset o);
void cmp(std::ostream&, Reg r1, Reg r2);
void cmp(std::ostream&, Offset o, Reg r);
void cmp(std::ostream&, Reg r, Offset o);
// neg
void neg(std::ostream&, Reg r);
void neg(std::ostream&, Offset o);
// not
void logic_not(std::ostream&, Reg r);
void logic_not(std::ostream&, Offset o);
// and
void logic_and(std::ostream&, Reg r1, Reg r2);
void logic_and(std::ostream&, Reg r, Offset o);
void logic_and(std::ostream&, Offset o, Reg r);
// or
void logic_or(std::ostream&, Reg r1, Reg r2);
void logic_or(std::ostream&, Reg r, Offset o);
void logic_or(std::ostream&, Offset o, Reg r);
// xor
void logic_xor(std::ostream&, Reg r1, Reg r2);
void logic_xor(std::ostream&, Reg r, Offset o);
void logic_xor(std::ostream&, Offset o, Reg r);
}
#endif