-
Notifications
You must be signed in to change notification settings - Fork 9
/
instructions_00.c
251 lines (235 loc) · 6.01 KB
/
instructions_00.c
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
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "instruction_defs.h"
#include "emulator_functions.h"
#include "modrm.h"
#include "io.h"
/*
* add rm8 r8: 2 bytes (add byte [eax], ah;)
* Adds value of ModR/M to REG.
* 1 byte: op (00)
* 1 byte: ModR/M
*/
void add_rm8_r8(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint8_t rm8_val = get_rm8(emu, &modrm);
uint8_t r8_val = get_r8(emu, &modrm);
uint16_t result = (uint16_t)rm8_val + (uint16_t)r8_val;
set_rm8(emu, &modrm, result);
update_eflags_add_8bit(emu, rm8_val, r8_val, result);
}
/*
* add rm32 r32: 2 bytes
* Adds value of REG to ModR/M.
* 1 byte: op (01)
* 1 byte: ModR/M
*/
void add_rm32_r32(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint32_t rm32_val = get_rm32(emu, &modrm);
uint32_t r32_val = get_r32(emu, &modrm);
uint64_t result = (uint64_t)rm32_val + (uint64_t)r32_val;
set_rm32(emu, &modrm, result);
update_eflags_add(emu, rm32_val, r32_val, result);
}
/*
* add r8 rm8: 2 bytes
* Adds value of ModR/M to r8.
* 1 byte: op (02)
* 1/2 byte: ModR/M
*/
void add_r8_rm8(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint8_t rm8_val = get_rm8(emu, &modrm);
uint8_t r8_val = get_r8(emu, &modrm);
uint16_t result = (uint16_t)r8_val + (uint16_t)rm8_val;
set_r8(emu, &modrm, result);
update_eflags_add_8bit(emu, r8_val, rm8_val, result);
}
/*
* add r32 rm32: 2 - 3 bytes
* Adds value of ModR/M to r32.
* 1 byte: op (03)
* 1 - 2 byte: ModR/M
*/
void add_r32_rm32(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint32_t r32_val = get_r32(emu, &modrm);
uint32_t rm32_val = get_rm32(emu, &modrm);
uint64_t result = (uint64_t)r32_val + (uint64_t)rm32_val;
set_r32(emu, &modrm, result);
update_eflags_add(emu, r32_val, rm32_val, result);
}
/*
* add al imm8: 2 bytes
* Adds imm8 to al.
* 1 byte: op (04)
* 1 byte: imm8
*/
void add_al_imm8(Emulator *emu)
{
uint8_t imm8_val = get_sign_code8(emu, 1);
uint8_t al_val = get_register8(emu, AL);
uint16_t result = (uint16_t)al_val + (uint16_t)imm8_val;
set_register8(emu, AL, result);
update_eflags_add_8bit(emu, al_val, imm8_val, result);
emu->eip += 2;
}
/*
* add eax imm32: 5 bytes
* Adds imm32 to eax.
* 1 byte: op (05)
* 4 byte: imm32
*/
void add_eax_imm32(Emulator *emu)
{
uint32_t eax_val = get_register32(emu, EAX);
uint32_t imm32_val = get_sign_code32(emu, 1);
uint64_t result = (uint64_t)eax_val + (uint64_t)imm32_val;
set_register32(emu, EAX, result);
update_eflags_add(emu, eax_val, imm32_val, result);
emu->eip += 5;
}
/*
* push es: 1 byte
* Pushes value of ES to stack.
* 1 byte: op (06)
*/
void push_es(Emulator *emu)
{
push_segment_register(emu, ES);
emu->eip += 1;
}
/*
* pop es: 1 byte
* Pops value from stack to ES.
* 1 byte: op (07)
*/
void pop_es(Emulator *emu)
{
pop_segment_register(emu, ES);
emu->eip += 1;
}
/*
* or rm8 r8: 2/3 bytes (or byte [eax], ah;)
* Logical inclusive OR between value of ModR/M and REG, storing result to destination.
* 1 byte: op (08)
* 1/2 byte: ModR/M
*/
void or_rm8_r8(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint8_t rm8_val = get_rm8(emu, &modrm);
uint8_t r8_val = get_r8(emu, &modrm);
uint8_t result = rm8_val | r8_val;
set_rm8(emu, &modrm, result);
update_eflags_logical_ops_8bit(emu, result);
}
/*
* or rm32 r32: 2/3 bytes
* Logical inclusive OR between value of REG and ModR/M, storing result to destination.
* 1 byte: op (09)
* 1/2 byte: ModR/M
*/
void or_rm32_r32(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint32_t rm32_val = get_rm32(emu, &modrm);
uint32_t r32_val = get_r32(emu, &modrm);
uint32_t result = rm32_val | r32_val;
set_rm32(emu, &modrm, result);
update_eflags_logical_ops(emu, result);
}
/*
* or r8 rm8: 2/3 bytes
* Logical inclusive OR between value of ModR/M and r8, storing result of destination.
* 1 byte: op (0A)
* 1/2 byte: ModR/M
*/
void or_r8_rm8(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint8_t rm8_val = get_rm8(emu, &modrm);
uint8_t r8_val = get_r8(emu, &modrm);
uint8_t result = r8_val | rm8_val;
set_r8(emu, &modrm, result);
update_eflags_logical_ops_8bit(emu, result);
}
/*
* or r32 rm32: 2/3 bytes
* Logical inclusive OR between value of ModR/M and r32, storing result of destination.
* 1 byte: op (0B)
* 1/2 byte: ModR/M
*/
void or_r32_rm32(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint32_t r32_val = get_r32(emu, &modrm);
uint32_t rm32_val = get_rm32(emu, &modrm);
uint32_t result = r32_val | rm32_val;
set_r32(emu, &modrm, result);
update_eflags_logical_ops(emu, result);
}
/*
* or al imm8: 2 bytes
* Logical inclusive OR between imm8 and al, storing result to destination.
* 1 byte: op (0C)
* 1 byte: imm8
*/
void or_al_imm8(Emulator *emu)
{
uint8_t imm8_val = get_code8(emu, 1);
uint8_t al_val = get_register8(emu, AL);
uint8_t result = al_val | imm8_val;
set_register8(emu, AL, result);
update_eflags_logical_ops_8bit(emu, result);
emu->eip += 2;
}
/*
* or al imm32: 5 bytes
* Logical inclusive OR between imm32 to eax, storing result to destination.
* 1 byte: op (0D)
* 4 byte: imm32
*/
void or_eax_imm32(Emulator *emu)
{
uint32_t eax_val = get_register32(emu, EAX);
uint32_t imm32_val = get_code32(emu, 1);
uint32_t result = eax_val | imm32_val;
set_register32(emu, EAX, result);
update_eflags_logical_ops(emu, result);
emu->eip += 5;
}
/*
* push cs: 1 byte
* Pushes value of CS to stack.
* 1 byte: op (0E)
*/
void push_cs(Emulator *emu)
{
push_segment_register(emu, CS);
emu->eip += 1;
}