def _determine_size(self): if self.operand_1.size == self.operand_2.size == -1: raise InvalidInstructionException("Operation size not specified") elif self.operand_1.size == -1: self.size = self.operand_2.size elif self.operand_2.size == -1: self.size = self.operand_1.size elif self.operand_1.size != self.operand_2.size: raise InvalidInstructionException("Operand sizes do not match") else: self.size = self.operand_1.size
def _extract_operands(self): tokens = self.instruction_str.split(',') if len(tokens) != 2: raise InvalidInstructionException("Could not determine operands") self.operand_1 = Operand(tokens[0].split(' ')[1].strip(), 'first') self.operand_2 = Operand(tokens[1].strip(), 'second')
def _determine_type(self): tokens = self.str.split(' ') if tokens[0] in operand_size_dict: self.str = tokens[1] self.size_specifier_str = tokens[0] if '[' in self.str: if ']' in self.str: self.type = OperandType.MEM else: raise InvalidInstructionException("Expected ']'") elif is_numeric(self.str): self.type = OperandType.CON elif self.str in register_dict: self.type = OperandType.REG else: raise InvalidInstructionException("Unknown operand: {}".format( self.str))
def _determine_size(self): if self.type == OperandType.REG: self.size = reg_size_dict[self.str] elif self.size_specifier_str and self.size_specifier_str in operand_size_dict: self.size = operand_size_dict[self.size_specifier_str] elif self.type == OperandType.MEM or self.type == OperandType.CON: self.size = -1 else: raise InvalidInstructionException( "No one should encounter this, really")
def _build_mod(self): if self.op_1.type == self.op_2.type == OperandType.REG: self.mod = '11' elif self.displacement == '': self.mod = '00' elif len(self.displacement) == 8: self.mod = '01' elif len(self.displacement) == self.instruction.size: self.mod = '10' else: raise InvalidInstructionException("Bad displacement")
def _build_sib(self, mem_op): op_str = mem_op.str.replace('[', '').replace(']', '') for p in op_str.split('+'): p = p.strip() if '*' not in p: if p in register_dict: self.base = register_dict[p] else: self.displacement = convert_to_binary(p) continue for p2 in p.split('*'): p2 = p2.strip() try: if p2 in register_dict: self.index = register_dict[p2] else: self.scale = scale_dict[p2] except KeyError as e: raise InvalidInstructionException(e)
def _extract_operation(self): try: self.operation = Operation( self.instruction_str.split(' ')[0].strip()) except ValueError: raise InvalidInstructionException("Unsupported operation")
def _validate(self): if is_numeric(self.str) and self.position == OperandPosition.FIRST: raise InvalidInstructionException( "First operand cannot be a number")
def _validate(self): if self.instruction.operand_1.type == OperandType.MEM and self.instruction.operand_2.type == OperandType.MEM: raise InvalidInstructionException("Both operands are memory")