MgAsmCom.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // MgDisasmBase.h: interface for the CMgDisasmBase class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_MGDISASMBASE_H__6DCEBE26_A3B4_476C_ACA1_1B56BA3F0401__INCLUDED_)
  5. #define AFX_MGDISASMBASE_H__6DCEBE26_A3B4_476C_ACA1_1B56BA3F0401__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include <math.h>
  10. //-------------------------------------------------------------------------------------------------------------------------
  11. //专有类型声明:
  12. typedef unsigned char uchar; // Unsigned character (byte)
  13. typedef unsigned short ushort; // Unsigned short
  14. typedef unsigned int uint; // Unsigned integer
  15. typedef unsigned long ulong; // Unsigned long
  16. //-------------------------------------------------------------------------------------------------------------------------
  17. //常量声明:
  18. //
  19. #define NEGLIMIT (-16384) // Limit to display constans as signed
  20. #define PSEUDOOP 128 // Base for pseudooperands
  21. #define TEXTLEN 256 // Maximal length of text string
  22. #define DISASM_SIZE 0 // Determine command size only
  23. #define DISASM_DATA 1 // Determine size and analysis data
  24. #define DISASM_FILE 3 // Disassembly, no symbols
  25. #define DISASM_CODE 4 // Full disassembly
  26. //------------------------------------------------------------------------------
  27. //------------------ ASSEMBLER, DISASSEMBLER AND EXPRESSIONS -------------------
  28. //------------------汇编,反汇编与表达-------------------
  29. #define MAXCMDSIZE 16 // Maximal length of 80x86 command 80x86的命令的最大长度
  30. #define MAXCALSIZE 8 // Max length of CALL without prefixes 不带前缀的最大长度CALL
  31. #define NMODELS 8 // Number of assembler search models 搜索模式
  32. //-------------------------------------------------------------------------------------------------------------------------
  33. //结构声明
  34. //
  35. typedef struct _tagMgAsmCom_CommandData_
  36. {
  37. ulong mask; // Mask for first 4 bytes of the command
  38. ulong code; // Compare masked bytes with this
  39. uchar len; // Length of the main command code
  40. uchar bits; // Special bits within the command
  41. uchar arg1, arg2, arg3; // Types of possible arguments
  42. uchar type; // C_xxx + additional information
  43. char* name; // Symbolic name for this command
  44. } _MGASM_CMDDATA_, t_cmddata;
  45. //
  46. typedef struct _tagMgAsmCom_AddrDec_
  47. {
  48. int defseg;
  49. char* descr;
  50. } _MGASM_ADDRDEC_, t_addrdec;
  51. //-------------------------------------------------------------------------------------------------------------------------
  52. //汇编基础类声明:
  53. class CMgAsmBase
  54. {
  55. public:
  56. CMgAsmBase();
  57. virtual ~CMgAsmBase();
  58. protected:
  59. private:
  60. public: //-------------------------------------- 结构部分 ----------------------------------
  61. // Model to search for assembler command
  62. typedef struct _tagAsmModel
  63. {
  64. unsigned char code[MAXCMDSIZE]; // Binary code
  65. unsigned char mask[MAXCMDSIZE]; // Mask for binary code (0: bit ignored)
  66. int length; // Length of code, bytes (0: empty)
  67. int jmpsize; // Offset size if relative jump
  68. int jmpoffset; // Offset relative to IP
  69. int jmppos; // Position of jump offset in command
  70. } _ASMMODEL_, t_asmmodel;
  71. //
  72. typedef struct _tagAsmOperand {
  73. int type; // Operand type, see beginning of file
  74. int size; // Operand size or 0 if yet unknown
  75. int index; // Index or other register
  76. int scale; // Scale
  77. int base; // Base register if present
  78. long offset; // Immediate value or offset
  79. int anyoffset; // Offset is present but undefined
  80. int segment; // Segment in address if present
  81. int jmpmode; // Specified jump size
  82. } _ASMOPERAND_, t_asmoperand;
  83. /*
  84. //
  85. typedef struct _tagAddrDec_
  86. {
  87. int defseg;
  88. char *descr;
  89. } _ADDRDEC_,t_addrdec;
  90. */
  91. //
  92. /*
  93. typedef struct _tagCommandData_
  94. {
  95. ulong mask; // Mask for first 4 bytes of the command
  96. ulong code; // Compare masked bytes with this
  97. uchar len; // Length of the main command code
  98. uchar bits; // Special bits within the command
  99. uchar arg1,arg2,arg3; // Types of possible arguments
  100. uchar type; // C_xxx + additional information
  101. char *name; // Symbolic name for this command
  102. } _CMDDATA_,t_cmddata;
  103. */
  104. public: //-------------------------------------- 变量部分 ----------------------------------
  105. char* m_pAsmCmd; // Pointer to 0-terminated source line
  106. int m_nScan; // Type of last scanned element
  107. int m_nPrio; // Priority of operation (0: highest)
  108. char m_sdata[TEXTLEN]; // Last scanned name (depends on type)
  109. long m_idata; // Last scanned value
  110. long double m_fdata; // Floating-point number
  111. char* m_pAsmError; // Explanation of last error, or NULL
  112. public:
  113. int m_nIDEAL; // Force IDEAL decoding mode
  114. int m_nSizeSens; // How to decode size-sensitive mnemonics
  115. protected: //-------------------------------------- 函数部分 ----------------------------------
  116. void Scanasm(int mode);
  117. void Parseasmoperand(t_asmoperand* op);
  118. public:
  119. int Assemble(char* cmd, ulong ip, t_asmmodel* model, int attempt, int constsize, char* errtext);
  120. };
  121. //-------------------------------------------------------------------------------------------------------------------------
  122. //反汇编基础类声明
  123. class CMgDisasmBase
  124. {
  125. public:
  126. CMgDisasmBase();
  127. virtual ~CMgDisasmBase();
  128. public: //-------------------------------------- 结构部分 ----------------------------------
  129. // Results of disassembling
  130. typedef struct _tagDisasm_
  131. {
  132. ulong ip; // Instrucion pointer
  133. char dump[TEXTLEN]; // Hexadecimal dump of the command
  134. char result[TEXTLEN]; // Disassembled command
  135. char comment[TEXTLEN]; // Brief comment
  136. int cmdtype; // One of C_xxx
  137. int memtype; // Type of addressed variable in memory
  138. int nprefix; // Number of prefixes
  139. int indexed; // Address contains register(s)
  140. ulong jmpconst; // Constant jump address
  141. ulong jmptable; // Possible address of switch table
  142. ulong adrconst; // Constant part of address
  143. ulong immconst; // Immediate constant
  144. int zeroconst; // Whether contains zero constant
  145. int fixupoffset; // Possible offset of 32-bit fixups
  146. int fixupsize; // Possible total size of fixups or 0
  147. int error; // Error while disassembling command
  148. int warnings; // Combination of DAW_xxx
  149. } _DISASM_, t_disasm;
  150. /*
  151. //
  152. typedef struct _tagAddrDec_
  153. {
  154. int defseg;
  155. char *descr;
  156. } _ADDRDEC_,t_addrdec;
  157. */
  158. //
  159. /*
  160. typedef struct _tagCommandData_
  161. {
  162. ulong mask; // Mask for first 4 bytes of the command
  163. ulong code; // Compare masked bytes with this
  164. uchar len; // Length of the main command code
  165. uchar bits; // Special bits within the command
  166. uchar arg1,arg2,arg3; // Types of possible arguments
  167. uchar type; // C_xxx + additional information
  168. char *name; // Symbolic name for this command
  169. } _CMDDATA_,t_cmddata;
  170. */
  171. public:
  172. //
  173. t_disasm* m_pDisasm;
  174. //
  175. char* m_pCmd; // Pointer to binary data
  176. char* m_pPFixup; // Pointer to possible fixups or NULL
  177. ulong m_ulSize; // Remaining size of the command buffer
  178. int m_nMode; // Disassembly mode (DISASM_xxx)
  179. public: //-------------------------------------- 变量部分 ----------------------------------
  180. //
  181. ulong m_ulDataSize; // Size of data (1,2,4 bytes)
  182. ulong m_ulAddrSize; // Size of address (2 or 4 bytes)
  183. //
  184. int m_nSegPrefix; // Segment override prefix or SEG_UNDEF
  185. int m_nHasRM; // Command has ModR/M byte
  186. int m_nHasSIB; // Command has SIB byte
  187. int m_nDispSize; // Size of displacement (if any)
  188. int m_nImmSize; // Size of immediate data (if any)
  189. int m_nSoftError; // Noncritical disassembler error
  190. int m_nDump; // Current length of command dump
  191. int m_nResult; // Current length of disassembly
  192. int m_nAddComment; // Comment value of operand
  193. public:
  194. int m_nIDEAL; // Force IDEAL decoding mode
  195. int m_nLowercase; // Force lowercase display
  196. int m_nTabArguments; // Tab between mnemonic and arguments
  197. int m_nExtraSpace; // Extra space between arguments
  198. int m_nPutDefSeg; // Display default segments in listing
  199. int m_nShowMemSize; // Always show memory size
  200. int m_nShowNEAR; // Show NEAR modifiers
  201. int m_nShortStringCmds; // Use short form of string commands
  202. int m_nSizeSens; // How to decode size-sensitive mnemonics
  203. int m_nSymbolic; // Show symbolic addresses in disasm
  204. int m_nFarCalls; // Accept far calls, returns & addresses
  205. int m_nDecodeVxd; // Decode VxD calls (Win95/98)
  206. int m_nPrivileged; // Accept privileged commands
  207. int m_nIOCommand; // Accept I/O commands
  208. int m_nBadShift; // Accept shift out of range 1..31
  209. int m_nExtraPrefix; // Accept superfluous prefixes
  210. int m_nLockedbus; // Accept LOCK prefixes
  211. int m_nStackAlign; // Accept unaligned stack operations
  212. int m_nIsWindowsNT; // When checking for dangers, assume NT
  213. protected: //-------------------------------------- 函数部分 ----------------------------------
  214. //
  215. void DecodeRG(int index, int datasize, int type);
  216. void DecodeST(int index, int pseudoop);
  217. void DecodeMX(int index);
  218. void DecodeNR(int index);
  219. void DecodeMR(int type);
  220. void DecodeSO(void);
  221. void DecodeDE(void);
  222. void DecodeXL(void);
  223. void DecodeIM(int constsize, int sxt, int type);
  224. void DecodeVX(void);
  225. void DecodeC1(void);
  226. void DecodeIA(void);
  227. void DecodeRJ(ulong offsize, ulong nextip);
  228. void DecodeJF(void);
  229. void DecodeSG(int index);
  230. void DecodeCR(int index);
  231. void DecodeDR(int index);
  232. public:
  233. int Get3dnowsuffix(void);
  234. void Memadr(int defseg, const char* descr, long offset, int dsize);
  235. int Decodeaddress(ulong addr, char* symb, int nsymb, char* comment);
  236. public:
  237. ulong Disasm(char* src, ulong srcsize, ulong srcip, t_disasm* disasm, int disasmmode);
  238. ulong Disassembleback(char* block, ulong base, ulong size, ulong ip, int n);
  239. ulong Disassembleforward(char* block, ulong base, ulong size, ulong ip, int n);
  240. };
  241. //-------------------------------------------------------------------------------------------------------------------------
  242. //全局变量声明:
  243. extern char* g_szSegName[8];
  244. extern char* g_szRegName[3][9];
  245. extern char* g_szCRName[9];
  246. extern char* g_szDRName[9];
  247. extern char* g_szFPUName[9];
  248. extern char* g_szMMXName[9];
  249. //
  250. extern char* g_szSizeName[11];
  251. //
  252. extern t_cmddata g_CmdData[];
  253. extern t_cmddata g_VxdCmd; // Decoding of VxD calls (Win95/98)
  254. //
  255. extern t_addrdec g_addr16[8];
  256. extern t_addrdec g_addr32[8];
  257. //-------------------------------------------------------------------------------------------------------------------------
  258. //全局函数声明:
  259. //--------------------------------------------------------------------------------
  260. // Function checks whether 80x86 flags meet condition set in the command.
  261. // Returns 1 if condition is met, 0 if not and -1 in case of error (which is
  262. // not possible).
  263. //--------------------------------------------------------------------------------
  264. int Checkcondition(int code, ulong flags);
  265. //--------------------------------------------------------------------------------
  266. // Service function, checks whether command at offset addr in data is a valid
  267. // filling command (usually some kind of NOP) used to align code to a specified
  268. // (align=power of 2, 0 means no alignment) border. Returns length of filling
  269. // command in bytes or 0 if command is not a recognized filling.
  270. //--------------------------------------------------------------------------------
  271. int Isfilling(ulong addr, char* data, ulong size, ulong align);
  272. //--------------------------------------------------------------------------------
  273. // Decodes and prints 64-bit 3DNow! element f into string s (which must be at
  274. // least 30 bytes long). Returns resulting length of the string.
  275. //--------------------------------------------------------------------------------
  276. int Print3dnow(char* s, char* f);
  277. //--------------------------------------------------------------------------------
  278. // Decodes and prints 80-bit long double ext into string s (at least 32 bytes
  279. // long). Procedure correctly displays all, even invalid, numbers without
  280. // arithmetical exceptions. Returns resulting length of the string.
  281. //--------------------------------------------------------------------------------
  282. int Printfloat10(char* s, long double ext);
  283. //--------------------------------------------------------------------------------
  284. // Decodes and prints 64-bit double d into string s (at least 25 bytes long).
  285. // Returns resulting length of the string.
  286. //--------------------------------------------------------------------------------
  287. int Printfloat8(char* s, double d);
  288. //--------------------------------------------------------------------------------
  289. // Decodes and prints 32-bit float f into string s (which must be at least 16
  290. // bytes long). Returns resulting length of the string.
  291. //--------------------------------------------------------------------------------
  292. int Printfloat4(char* s, float f);
  293. //-------------------------------------------------------------------------------------------------------------------------
  294. //
  295. void message(int aa);
  296. #endif // !defined(AFX_MGDISASMBASE_H__6DCEBE26_A3B4_476C_ACA1_1B56BA3F0401__INCLUDED_)