Created
April 24, 2016 20:29
-
-
Save Konctantin/e1fb8ab0fcecd3decb6eae14d8c788ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import x86instructions | |
| import re | |
| def tryint(str, h=10): | |
| if str == "": | |
| return 0; | |
| return int(str, h); | |
| pass | |
| class x86Operand: | |
| def __init__(self, str): | |
| self.str = str; | |
| self.sizes = []; | |
| self.rtypes= []; | |
| self.type = ""; | |
| self.freg = ""; | |
| self.IsValid = False; | |
| if str == "": return; | |
| self.GetSizes(); | |
| pass | |
| def GetSizes(self): | |
| for sz in re.findall("(\d+)", self.str): | |
| self.sizes.append(sz); | |
| pass | |
| def GetType(self): | |
| if self.str.startswith("r/m"): | |
| self.type = "EffectiveAddress"; | |
| elif self.str.startswith("moff"): | |
| self.type = "MemoryOffset"; | |
| elif self.str.startswith("imm"): | |
| self.type = "Immedicate"; | |
| elif self.str.startswith("r"): | |
| self.type = "Register"; | |
| elif self.str.startswith("m"): | |
| self.type = "Memory"; | |
| pass | |
| pass | |
| class x86Instr: | |
| def __init__(self, mnem, operands, pref, pref0f, opcode, enc, mode, lock, desc): | |
| self.mnem = mnem; | |
| self.operands = self.parseOperands(operands); | |
| self.prefix = pref; | |
| self.prefix0F = pref0f; | |
| self.opcode = self.parseOpcode(opcode); | |
| self.encoding = self.parseEncoding(enc); | |
| self.opcField = self.parseOpcField(enc); | |
| self.isX64 = self.parseIs64(mode); | |
| self.lock = lock; | |
| self.desc = desc; | |
| pass | |
| def parseOperands(self, op): | |
| l = [] | |
| for o in op: | |
| oo = x86Operand(o); | |
| if oo.IsValid: | |
| l.append(oo) | |
| return l | |
| pass | |
| def parseOpcode(self, opcodes): | |
| p1 = tryint(opcodes[0], 16) | |
| p2 = tryint(opcodes[1], 16) | |
| if p1 == 0: return []; | |
| if p2 == 0: return [p1]; | |
| return [p1, p2] | |
| pass | |
| def parseEncoding(self, enc): | |
| if enc == "a": | |
| return "AddToOpcode"; | |
| if enc == "r": | |
| return "Reg"; | |
| return None; | |
| pass | |
| def parseOpcField(self, enc): | |
| if enc in ['0','1','2','3','4','5','6','7']: | |
| return int(enc); | |
| return None; | |
| pass | |
| def parseIs64(self, str): | |
| return str.strip() != ""; | |
| pass | |
| pass | |
| class InstrDb: | |
| def __init__(self, db): | |
| self.map = { } | |
| for instr in db: | |
| mnem = instr["name"] | |
| if not self.map.has_key(mnem): | |
| self.map[mnem] = []; | |
| self.map[mnem].append(x86Instr( | |
| mnem, | |
| instr["operands"], | |
| instr["pref"], | |
| instr["pref0F"], | |
| instr["opcode"], | |
| instr["enc"], | |
| instr["mode"], | |
| instr["lock"], | |
| instr["desc"] | |
| )) | |
| pass | |
| #def generate(self, path): | |
| # for name in sorted(self.map): | |
| # | |
| #pass | |
| pass | |
| db = InstrDb(x86instructions.instruction_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment