-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModRM.cpp
175 lines (144 loc) · 3.11 KB
/
ModRM.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
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
#include <iostream>
#include "ModRM.h"
using namespace std;
#define DEBUG() cout<<"Mod:"<<(uint32_t)Mod<<" RM:"<<(uint32_t)RM<<endl;
ModRM::ModRM(){
}
ModRM::ModRM(Emulator *emu){
this->emu = emu;
Parse(emu);
}
void ModRM::Parse(Emulator *emu){ //cout<<"parse"<<endl;
uint8_t code = emu->GetCode8(0);
cout<<"code="<<(uint32_t)code<<endl;
Mod = ((code & 0xC0) >> 6);
opecode = ((code & 0x38) >> 3);
RM = code & 0x07; DEBUG()
emu->EIP++;
if(Mod != 3 && RM == 4){
SIB = emu->GetCode8(0);
emu->EIP++;
}
if((Mod == 0 && RM == 5) || Mod == 2){
disp32 = emu->GetSignCode32(0);
emu->EIP += 4;
}else if(Mod == 1){
disp8 = emu->GetSignCode8(0);
emu->EIP++;
}
return;
}
void ModRM::Parse(){
if(emu != NULL)
Parse(emu);
}
uint8_t ModRM::GetRM8(Emulator *emu){
if(Mod == 3){
return emu->GetRegister8(RM);
}else{
uint32_t addr = CalcMemAddr(emu);
return emu->GetMemory8(addr);
}
}
uint8_t ModRM::GetRM8(){
if(emu != NULL) return GetRM8(emu);
}
void ModRM::SetRM8(Emulator *emu, uint8_t val){
if(Mod == 3){
emu->SetRegister8(RM, val);
}else{
uint32_t addr = CalcMemAddr(emu);
emu->SetMemory8(addr, val);
}
}
void ModRM::SetRM8(uint8_t val){
if(emu != NULL) SetRM8(emu, val); return;
}
uint32_t ModRM::GetRM32(Emulator *emu){
if(Mod == 3){
return emu->GetRegister32(RM);
}else{
uint32_t addr = CalcMemAddr(emu);
return emu->GetMemory32(addr);
}
}
uint32_t ModRM::GetRM32(){
if(emu != NULL) return GetRM32(emu);
}
void ModRM::SetRM32(Emulator *emu, uint32_t val){
if(Mod == 3){
emu->SetRegister32(RM, val);
}else{
uint32_t addr = CalcMemAddr(emu);
emu->SetMemory32(addr, val);
cout<<"setrm addr="<<addr;
cout<<" val="<<val<<hex;
cout<<" Mod="<<(uint32_t)Mod;
cout<<" RM="<<(uint32_t)RM;
cout<<endl;
}
}
void ModRM::SetRM32(uint32_t val){
if(emu != NULL)
SetRM32(emu, val);
}
uint8_t ModRM::GetR8(Emulator *emu){
return emu->GetRegister32(reg_index);
}
uint8_t ModRM::GetR8(){
if(emu != NULL) return GetR8(emu);
}
void ModRM::SetR8(Emulator *emu, uint8_t val){
emu->SetRegister8(reg_index, val);
}
void ModRM::SetR8(uint8_t val){
if(emu != NULL) return SetR8(val);
}
uint32_t ModRM::GetR32(Emulator *emu){
return emu->GetRegister32(reg_index);
}
uint32_t ModRM::GetR32(){
if(emu != NULL) return GetR32(emu);
}
void ModRM::SetR32(Emulator *emu, uint32_t val){
emu->SetRegister32(reg_index, val);
}
void ModRM::SetR32(uint32_t val){
if(emu != NULL){
SetR32(emu, val);
}
}
uint32_t ModRM::CalcMemAddr(Emulator *emu){
if(Mod == 0){
if(RM == 4){
cout<<"not implemented ModRM Mod = 0, RM = 4"<<endl;
return 0;
}else if(RM == 5){
return disp32;
}else{
return emu->GetRegister32(RM);
}
}else if(Mod == 1){
if(RM == 4){
cout<<"not implemented ModRM Mod = 1, RM = 4"<<endl;
return 0;
}else{
return emu->GetRegister32(RM) + disp8;
}
}else if(Mod == 2){
if(RM == 4){
cout<<"not implemented ModRM Mod = 2, RM = 4"<<endl;
return 0;
}else{
return emu->GetRegister32(RM) + disp32;
}
}else{
cout<<"not implemented ModRM Mod = 3"<<endl;
return 0;
}
}
uint32_t ModRM::CalcMemAddr(){
if(emu == NULL)
return 0;
return CalcMemAddr(emu);
}