1
1
#include "Parser.h"
2
+ #include <stdio.h>
2
3
3
4
//
4
- inline int getPrefix (char * instruction )
5
+ inline int getPrefix (unsigned char * instruction )
5
6
{
6
7
7
8
//Check for 0x66 prefix
@@ -11,10 +12,10 @@ inline int getPrefix(char* instruction)
11
12
}
12
13
13
14
//sets the instr_args.type and returns the length of instruction opcode
14
- int decodeInstructionType (char * instruction , int prefixOffset , ParsedInstruction * instr_args )
15
+ int decodeInstructionType (unsigned char * instruction , int prefixOffset , ParsedInstruction * instr_args )
15
16
{
16
17
//Check for VEX-coded instructions
17
- if (instruction [0 ] == 0xC4 )
18
+ if (( instruction [0 ] & 0xFF ) == 0xC4 )
18
19
{
19
20
//second byte check - mandatory 11x on the front and 00010 on the back
20
21
if ((instruction [1 ] & 0b11011111 ) != 0b11000010 )
@@ -24,25 +25,25 @@ int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction
24
25
return 0 ;
25
26
}
26
27
//third byte check - mandatory 0xxxx0xx
27
- if ((instruction [2 ]) != 0 )
28
+ if ((instruction [2 ] & 0b10000100 ) != 0 )
28
29
{
29
30
instr_args -> type = INSTR_UNKNOWN ;
30
31
instr_args -> length = 0 ;
31
32
return 0 ;
32
33
}
33
34
34
35
//fourth byte check
35
- if (instruction [3 ] == 0xF2 )
36
+ if (( instruction [3 ] & 0xFF ) == 0xF2 )
36
37
{
37
38
instr_args -> type = INSTR_ANDN ;
38
39
return 4 ;
39
40
}
40
- else if (instruction [3 ] == 0xF7 )
41
+ else if (( instruction [3 ] & 0xFF ) == 0xF7 )
41
42
{
42
43
instr_args -> type = INSTR_BEXTR ;
43
44
return 4 ;
44
45
}
45
- else if (instruction [3 ] == 0xF3 )
46
+ else if (( instruction [3 ] & 0xFF ) == 0xF3 )
46
47
{
47
48
//check reg value of ModR/M
48
49
if ((instruction [4 ] & 0b00111000 ) >> 3 == 3 )
@@ -76,25 +77,25 @@ int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction
76
77
}
77
78
78
79
//check for mandatory prefixes of non-vex instructions
79
- if (instruction [0 + prefixOffset ] != 0xF3 || instruction [1 + prefixOffset ] != 0x0F )
80
+ if (( instruction [0 + prefixOffset ] & 0xFF ) != 0xF3 || ( instruction [1 + prefixOffset ] & 0xFF ) != 0x0F )
80
81
{
81
82
instr_args -> type = INSTR_UNKNOWN ;
82
83
instr_args -> length = 0 ;
83
84
return 0 ;
84
85
}
85
86
86
87
//decode non-vex instruction type
87
- if (instruction [2 + prefixOffset ] == 0xB8 )
88
+ if (( instruction [2 + prefixOffset ] & 0xFF ) == 0xB8 )
88
89
{
89
90
instr_args -> type = INSTR_POPCNT ;
90
91
return 3 ;
91
92
}
92
- else if (instruction [2 + prefixOffset ] == 0xBC )
93
+ else if (( instruction [2 + prefixOffset ] & 0xFF ) == 0xBC )
93
94
{
94
95
instr_args -> type = INSTR_LZCNT ;
95
96
return 3 ;
96
97
}
97
- else if (instruction [2 + prefixOffset ] == 0xBD )
98
+ else if (( instruction [2 + prefixOffset ] & 0xFF ) == 0xBD )
98
99
{
99
100
instr_args -> type = INSTR_TZCNT ;
100
101
return 3 ;
@@ -104,7 +105,7 @@ int decodeInstructionType(char* instruction, int prefixOffset, ParsedInstruction
104
105
return 0 ;
105
106
}
106
107
107
- void decodeInstruction (char * instruction , int offset , int op16bit , ParsedInstruction * instr_args )
108
+ void decodeInstruction (unsigned char * instruction , int offset , int op16bit , ParsedInstruction * instr_args )
108
109
{
109
110
unsigned char mod = instruction [offset ] >> 6 ;
110
111
unsigned char reg = (instruction [offset ] & 0b00111000 ) >> 3 ;
@@ -222,7 +223,7 @@ void decodeInstruction(char* instruction, int offset, int op16bit, ParsedInstruc
222
223
}
223
224
}
224
225
225
- ParsedInstruction parse (char * instruction )
226
+ ParsedInstruction parse (unsigned char * instruction )
226
227
{
227
228
ParsedInstruction instr_args ;
228
229
instr_args .mem .base = UNDEF ;
0 commit comments