-
Notifications
You must be signed in to change notification settings - Fork 9
/
instructions_70.c
107 lines (97 loc) · 2.7 KB
/
instructions_70.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
#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"
/*
* Macro for jump with eflag.
* Replaces the matching text and ## concatenates strings.
* Defined until unescaped line break.
*/
#define DEFINE_JX(flag, is_flag) \
void j##flag(Emulator *emu) \
{ \
int diff = is_flag(emu) ? get_sign_code8(emu, 1) : 0; \
emu->eip += (diff + 2); \
} \
void jn##flag(Emulator *emu) \
{ \
int diff = is_flag(emu) ? 0 : get_sign_code8(emu, 1); \
emu->eip += (diff + 2); \
}
/*
* jc (72), jnc (73), jz (74), jnz (75), js (78), jns (79), jo (70), jno (71): 2 bytes
* 1 byte: op code
* 1 byte: offset to jump
*/
DEFINE_JX(c, is_carry)
DEFINE_JX(z, is_zero)
DEFINE_JX(s, is_sign)
DEFINE_JX(o, is_overflow)
#undef DEFINE_JX
/*
* jna (76)
* Jumps if above (CF=1 or ZF=1)
* 1 byte: op code
* 1 byte: offset to jump
*/
void jna(Emulator *emu)
{
int diff = (is_zero(emu) || is_carry(emu)) ? get_sign_code8(emu, 1) : 0;
emu->eip += (diff + 2);
}
/*
* ja (77)
* Jumps if above (CF=0 and ZF=0)
* 1 byte: op code
* 1 byte: offset to jump
*/
void ja(Emulator *emu)
{
int diff = (!is_zero(emu) && !is_carry(emu)) ? get_sign_code8(emu, 1) : 0;
emu->eip += (diff + 2);
}
/*
* jl (7C) and jle (7E)
* Assuming there's no overflow, which means overflow_flag == 0,
* if sign_flag is 0, left is bigger. Ex:
* 3, 2: 3 - 2 = 1 => sign_flag: 0 => larger
* -3, 2: -3 - (2) = -5 => sign_flag: 1 => smaller
* -1, -4: -1 - (-4) = 3 => sign_flag: 0 => larger
*/
void jl(Emulator *emu)
{
int diff = (is_sign(emu) != is_overflow(emu)) ? get_sign_code8(emu, 1) : 0;
emu->eip += (diff + 2);
}
void jle(Emulator *emu)
{
int diff = (is_zero(emu) || (is_sign(emu) != is_overflow(emu))) ? get_sign_code8(emu, 1) : 0;
emu->eip += (diff + 2);
}
/*
* jge (7D)
* Jumps if greater or equal (SF=OF)
* 1 byte: op code
* 1 byte: offset to jump
*/
void jge(Emulator *emu)
{
int diff = ((is_sign(emu) == is_overflow(emu))) ? get_sign_code8(emu, 1) : 0;
emu->eip += (diff + 2);
}
/*
* jg (7f)
* Jumps if greater (ZF=0 and SF=OF)
* 1 byte: op code
* 1 byte: offset to jump
*/
void jg(Emulator *emu)
{
int diff = (!is_zero(emu) && (is_sign(emu) == is_overflow(emu))) ? get_sign_code8(emu, 1) : 0;
emu->eip += (diff + 2);
}