-
Notifications
You must be signed in to change notification settings - Fork 9
/
instructions_10.c
262 lines (245 loc) · 6.52 KB
/
instructions_10.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
252
253
254
255
256
257
258
259
260
261
262
#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"
/*
* adc rm8 r8: 2|3 bytes (adc byte [eax], ah;)
* Adds values of ModR/M and carry flag to REG.
* 1 byte: op (10)
* 1|2 byte: ModR/M
*/
void adc_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)is_carry(emu);
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);
}
/*
* adc rm32 r32: 2|3 bytes
* Adds values of REG and carry flag to ModR/M.
* 1 byte: op (11)
* 1|2 byte: ModR/M
*/
void adc_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) + is_carry(emu);
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);
}
/*
* adc r8 rm8: 2|3 bytes
* Adds values of ModR/M and carry flag to r8.
* 1 byte: op (12)
* 1|2 byte: ModR/M
*/
void adc_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)is_carry(emu);
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);
}
/*
* adc r32 rm32: 2|3 bytes
* Adds values of ModR/M and carry flag to r32.
* 1 byte: op (13)
* 1|2 byte: ModR/M
*/
void adc_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) + is_carry(emu);
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);
}
/*
* adc al imm8: 2 bytes
* Adds imm8 and carry flag to al.
* 1 byte: op (14)
* 1 byte: imm8
*/
void adc_al_imm8(Emulator *emu)
{
uint8_t imm8_val = get_sign_code8(emu, 1);
uint8_t al_val = get_register8(emu, AL) + (uint8_t)is_carry(emu);
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;
}
/*
* adc al imm32: 5 bytes
* Adds imm32 and carry flag to eax.
* 1 byte: op (15)
* 4 byte: imm32
*/
void adc_eax_imm32(Emulator *emu)
{
uint32_t eax_val = get_register32(emu, EAX);
uint32_t imm32_val = get_sign_code32(emu, 1) + is_carry(emu);
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 ss: 1 byte
* Pushes value of SS to stack.
* 1 byte: op (16)
*/
void push_ss(Emulator *emu)
{
push_segment_register(emu, SS);
emu->eip += 1;
}
/*
* pop es: 1 byte
* Pops value from stack to SS.
* 1 byte: op (17)
*/
void pop_ss(Emulator *emu)
{
pop_segment_register(emu, SS);
emu->eip += 1;
}
/*
* sbb rm8 r8: 2|3 bytes (sbb byte [eax], ah;)
* Subtracts values of ModR/M and carry flag from REG.
* 1 byte: op (18)
* 1|2 byte: ModR/M
*/
void sbb_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)is_carry(emu);
uint16_t result = (uint16_t)rm8_val - (uint16_t)r8_val;
set_rm8(emu, &modrm, result);
update_eflags_sub_8bit(emu, rm8_val, r8_val, result);
}
/*
* sbb rm32 r32: 2|3 bytes
* Subtracts values of REG and carry flag from ModR/M.
* 1 byte: op (19)
* 1|2 byte: ModR/M
*/
void sbb_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) + is_carry(emu);
uint64_t result = (uint64_t)rm32_val - (uint64_t)r32_val;
set_rm32(emu, &modrm, result);
update_eflags_sub(emu, rm32_val, r32_val, result);
}
/*
* sbb r8 rm8: 2|3 bytes
* Subtracts values of ModR/M and carry flag from r8.
* 1 byte: op (1A)
* 1|2 byte: ModR/M
*/
void sbb_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)is_carry(emu);
uint16_t result = (uint16_t)r8_val - (uint16_t)rm8_val;
set_r8(emu, &modrm, result);
update_eflags_sub_8bit(emu, r8_val, rm8_val, result);
}
/*
* sbb r32 rm32: 2|3 bytes
* Subtracts values of ModR/M and carry flag from r32.
* 1 byte: op (1B)
* 1|2 byte: ModR/M
*/
void sbb_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) + is_carry(emu);
uint64_t result = (uint64_t)r32_val - (uint64_t)rm32_val;
set_r32(emu, &modrm, result);
update_eflags_sub(emu, r32_val, rm32_val, result);
}
/*
* sbb al imm8: 2 bytes
* Subtracts imm8 and carry flag from al.
* 1 byte: op (1C)
* 1 byte: imm8
*/
void sbb_al_imm8(Emulator *emu)
{
uint8_t imm8_val = get_sign_code8(emu, 1);
uint8_t al_val = get_register8(emu, AL) + (uint8_t)is_carry(emu);
uint16_t result = (uint16_t)al_val - (uint16_t)imm8_val;
set_register8(emu, AL, result);
update_eflags_sub_8bit(emu, al_val, imm8_val, result);
emu->eip += 2;
}
/*
* sbb al imm32: 5 bytes
* Subtracts imm32 and carry flag from eax.
* 1 byte: op (1D)
* 4 byte: imm32
*/
void sbb_eax_imm32(Emulator *emu)
{
uint32_t eax_val = get_register32(emu, EAX);
uint32_t imm32_val = get_sign_code32(emu, 1) + is_carry(emu);
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 ds: 1 byte
* Pushes value of DS to stack.
* 1 byte: op (1E)
*/
void push_ds(Emulator *emu)
{
push_segment_register(emu, DS);
emu->eip += 1;
}
/*
* pop ds: 1 byte
* Pops value from stack to DS.
* 1 byte: op (1F)
*/
void pop_ds(Emulator *emu)
{
pop_segment_register(emu, DS);
emu->eip += 1;
}