-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimpleFunctions.cpp
149 lines (137 loc) · 4.15 KB
/
SimpleFunctions.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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Headers/SimpleFunctions.h"
using namespace std;
int parse(const char* string){
int i=0;
while(string[i]!=' ')
i++;
while(string[i]==' ')
i++;
return i;
}
void get_register_name(char* & string, const char* instruction){
int n=4;
int i=0;
string=new char[strlen(instruction)];
while(instruction[i]!='\0'){
string[i]=instruction[i];
i++;
}
string[i]='\0';
}
bool compare(const char* string1, const char* string2){
int i=0;
while((string1[i]!='\0')&&(string2[i]!='\0')){
if (string1[i]!=string2[i])
return false;
i++;
}
return true;
}
int condition_command(const char* command, const char* instruction){
//1 both commands are cond commands, or first and second is NULL
//0 first command is not cond command
//-1 first is true, second is false
int flag;
if((compare(command,"jz"))||(compare(command,"jnz"))||
(compare(command,"jns"))||(compare(command,"jmp"))||
(compare(command,"ja"))||(compare(command,"jna"))||
(compare(command,"jo"))||(compare(command,"jno"))||
(compare(command,"jnc"))||(compare(command,"jc"))||
(compare(command,"jl"))||(compare(command,"jnl"))||
(compare(command,"js"))||(compare(command,"jns"))||
(compare(command,"jecxz"))||(compare(command,"jp"))||
(compare(command,"jpo"))||(compare(command,"loopn"))||
(compare(command,"loope"))||(compare(command,"loopne"))||
(compare(command,"loopz"))||(compare(command,"loopnz"))||
(compare(command,"loop"))||(compare(command,"jg"))||
(compare(command,"jng"))||(compare(command,"jmpf"))){
flag=1;
}
else{
flag=0;
}
if ((instruction==NULL)||(flag==0)){
return flag;
}
if (((compare(instruction,"jz"))&&(!compare(command,"jz")))||
((compare(instruction,"jnz"))&&(!compare(command,"jnz")))||
((compare(instruction,"jns"))&&(!compare(command,"jns")))||
((compare(instruction,"jmp"))&&(!compare(command,"jmp")))||
((compare(instruction,"ja"))&&(!compare(command,"ja")))||
((compare(instruction,"jna"))&&(!compare(command,"jna")))||
((compare(instruction,"jo"))&&(!compare(command,"jo")))||
((compare(instruction,"jno"))&&(!compare(command,"jno")))||
((compare(instruction,"jc"))&&(!compare(command,"jc")))||
((compare(instruction,"jnc"))&&(!compare(command,"jnc")))||
((compare(instruction,"jl"))&&(!compare(command,"jl")))||
((compare(instruction,"js"))&&(!compare(command,"js")))||
((compare(instruction,"jns"))&&(!compare(command,"jns")))||
((compare(instruction,"jecxz"))&&(!compare(command,"jecxz")))||
((compare(instruction,"jp"))&&(!compare(command,"jp")))||
((compare(instruction,"jpo"))&&(!compare(command,"jpo")))||
((compare(instruction,"loopn"))&&(!compare(command,"loopn")))||
((compare(instruction,"loope"))&&(!compare(command,"loope")))||
((compare(instruction,"loopne"))&&(!compare(command,"loopne")))||
((compare(instruction,"loopz"))&&(!compare(command,"loopz")))||
((compare(instruction,"loopnz"))&&(!compare(command,"loopnz")))||
((compare(instruction,"loop"))&&(!compare(command,"loop")))||
((compare(instruction,"jg"))&&(!compare(command,"jg")))||
((compare(instruction,"jng"))&&(!compare(command,"jng")))||
((compare(instruction,"jmpf"))&&(!compare(command,"jmpf")))){
return -1;
}
else{
return 1;
}
}
int GetNumber2 (const char* string){
int koef=16;
int number=0;
int i=0;
while(string[i]!='\0'){
if ((string[i]>='0')&&(string[i]<='9')){
number=number*koef+(string[i]-'0');
}
else{
if ((string[i]>='a')&&(string[i]<='f'))
number=number*koef+(string[i]-'a'+10);
else{
//printf("Invalid number\n");
number=0;
break;
}
}
i++;
}
return number;
}
unsigned int GetNumber (const char* string){
unsigned int segment=0;
unsigned int offset=0;
int koef=16;
unsigned int number=0;
int i=0;
while(string[i]!='\0'){
if ((string[i]>='0')&&(string[i]<='9')){
offset=offset*koef+(string[i]-'0');
}
else{
if ((string[i]>='a')&&(string[i]<='f'))
offset=offset*koef+(string[i]-'a'+10);
else{
if (string[i]==':'){
i=i+2;
segment=offset;
}
offset=0;
break;
}
}
i++;
}
number=segment*pow(koef,4)+offset;
return number;
}