-
Notifications
You must be signed in to change notification settings - Fork 1
/
sib.cpp
63 lines (52 loc) · 2.3 KB
/
sib.cpp
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
#include "sib.hpp"
#include "utility_functions.hpp"
string register_array[] = { "EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI" };
SibDecodeOutputArguments* decodeSIBByte(SibDecodeInputArguments* inputs, Reader *reader, RegisterBank *rb){
SibDecodeOutputArguments *output = new SibDecodeOutputArguments;
output->effective_addr = 0;
output->decoded_print_string = "";
uint32_t mod = (uint32_t)((inputs->modrm_byte & ((uint8_t)3<<6))>>6);
uint8_t sib_byte = reader->readNextByte();
uint32_t scale = (uint32_t)((sib_byte & ((uint8_t)3<<6))>>6);
uint32_t index = (uint32_t)((sib_byte & ((uint8_t)7<<3))>>3);
uint32_t base = (uint32_t)(sib_byte & ((uint8_t)7));
string scale_register = register_array[index];
string base_register = register_array[base];
if(index == 4){
scale_register = "none";
}
string decoded_print_string_op1 = "";
// Decode scale index
if(index == 4){
// ignore
}
else{
output->effective_addr = rb->getRegister(scale_register) * (1 << scale);
decoded_print_string_op1 = "%"+scale_register;
}
// Add base register
if (base == 5){ // special case [*]
if(mod == 0){
uint32_t displacement = readDispalcement(reader, 4);
output->effective_addr += displacement;
output->decoded_print_string = "("+intToHexStr(displacement)+"("+decoded_print_string_op1+"))";
}
else if(mod == 1){
uint32_t displacement = readDispalcement(reader, 1);
output->effective_addr += displacement;
output->effective_addr += rb->getRegister("EBP");
output->decoded_print_string = "("+intToHexStr(displacement) + "(%EBP,"+decoded_print_string_op1+"))";
}
else if(mod == 2){
uint32_t displacement = readDispalcement(reader, 4);
output->effective_addr += displacement;
output->effective_addr += rb->getRegister("EBP");
output->decoded_print_string = "("+intToHexStr(displacement) + "(%EBP,"+decoded_print_string_op1+"))";
}
}
else{
output->effective_addr += rb->getRegister(base_register);
output->decoded_print_string = "(%"+base_register+","+decoded_print_string_op1+ "," +std::to_string(1<<scale)+")";
}
return output;
}