def test_2_makes_mult_instruction():
    computer = OpcodeComputer([2, 2, 3, 0, 99])
    factory = InstructionFactory(computer)

    instr = factory.load_instruction(0)

    assert isinstance(instr, MultInstruction)
    assert instr.params == [Param(2), Param(3), Param(0)]
Exemplo n.º 2
0
def test_jump_if_true_instruction_sets_instruction_pointer():
    opcode_computer = Mock()

    jump_true_instruction = JumpIfTrueInstruction(
        opcode_computer, [Param(5, 1), Param(10, 1)])

    jump_true_instruction.execute()

    opcode_computer.set_instruction_pointer.assert_called_with(10)
Exemplo n.º 3
0
def test_jump_if_true_doesnt_jump_if_false():
    opcode_computer = Mock()
    opcode_computer.instruction_pointer = 5

    jump_true_instruction = JumpIfTrueInstruction(
        opcode_computer, [Param(0, 1), Param(10, 1)])

    jump_true_instruction.execute()

    assert opcode_computer.set_instruction_pointer.called is False
Exemplo n.º 4
0
def test_mult_instruciton_puts_difference_to_address(stub_computer):
    opcode_computer = stub_computer
    opcode_computer.put = Mock()
    addr_orig1 = Param(3)
    addr_orig2 = Param(5)
    addr_target = Param(10)

    subtraction_instruction = MultInstruction(
        opcode_computer, [addr_orig1, addr_orig2, addr_target])
    subtraction_instruction.execute()

    opcode_computer.put.assert_called_with(10, 42 * 58)
Exemplo n.º 5
0
def test_addition_instruction_puts_sum_to_address(stub_computer):
    opcode_computer = stub_computer
    opcode_computer.put = Mock()

    addr_orig1 = Param(3)
    addr_orig2 = Param(5)
    addr_target = Param(10)

    addition_instruction = AddInstruction(
        opcode_computer, [addr_orig1, addr_orig2, addr_target])
    addition_instruction.execute()

    opcode_computer.put.assert_called_with(10, 100)
Exemplo n.º 6
0
def test_equals(stub_computer):
    opcode_computer = stub_computer
    opcode_computer.put = Mock()

    addr_orig1 = Param(5)
    addr_orig2 = Param(7)
    addr_target = Param(10)

    addition_instruction = EqualsInstruction(
        opcode_computer, [addr_orig1, addr_orig2, addr_target])
    addition_instruction.execute()

    opcode_computer.put.assert_called_with(10, 1)
Exemplo n.º 7
0
def test_less_than(stub_computer):
    opcode_computer = stub_computer
    opcode_computer.put = Mock()

    addr_orig1 = Param(3)
    addr_orig2 = Param(5)
    addr_target = Param(10)

    addition_instruction = LessThanInstruction(
        opcode_computer, [addr_orig1, addr_orig2, addr_target])
    addition_instruction.execute()

    opcode_computer.put.assert_called_with(10, 1)
Exemplo n.º 8
0
def test_input_instruction_asks_computer_for_input():
    opcode_computer = Mock()
    opcode_computer.read_input = Mock(return_value=55)

    addr_target = Param(10)
    input_instruction = InputInstruction(opcode_computer, [addr_target])

    input_instruction.execute()

    assert opcode_computer.read_input.called is True
    opcode_computer.put.assert_called_with(10, 55)
Exemplo n.º 9
0
def test_relative_base_offset(stub_computer):
    opcode_computer = stub_computer
    opcode_computer.adjust_relative_base = Mock()

    relative_base_instruction = AdjustRelativeBaseInstruction(
        opcode_computer, [Param(55, mode=1)]
    )

    relative_base_instruction.execute()

    opcode_computer.adjust_relative_base.assert_called_with(55)
    def load_instruction(self, pos):
        instruction = self.computer.get(pos)
        opcode = instruction % 100
        InstructionClass = OPCODE_TYPES[opcode]

        num_args = InstructionClass.num_args()

        arg_values = self.computer[pos + 1:pos + num_args + 1]

        modes = [0] * num_args
        self.set_modes(modes, instruction)

        params = [Param(value, mode) for value, mode in zip(arg_values, modes)]

        return InstructionClass(self.computer, params)
Exemplo n.º 11
0
    def __init__(self, methanalysis):
        self.method = methanalysis.get_method()
        self.metha = methanalysis
        self.name = self.method.get_name()
        self.lparams = []
        self.basic_blocks = [bb for bb in methanalysis.basic_blocks.get()]
        self.var_to_name = {}
        self.writer = None

        access = self.method.get_access_flags()
        self.access = [
            flag for flag in util.ACCESS_FLAGS_METHODS if flag & access
        ]
        desc = self.method.get_descriptor()
        self.type = util.get_type(desc.split(')')[-1])
        self.params_type = util.get_params_type(desc)

        self.exceptions = methanalysis.exceptions.exceptions

        code = self.method.get_code()
        if code is None:
            util.log(
                'No code : %s %s' % (self.name, self.method.get_class_name()),
                'debug')
        else:
            start = code.registers_size - code.ins_size
            if 0x8 not in self.access:
                self.var_to_name[start] = ThisParam(start, self.name)
                self.lparams.append(start)
                start += 1
            num_param = 0
            for ptype in self.params_type:
                param = start + num_param
                self.lparams.append(param)
                self.var_to_name.setdefault(param, Param(param, ptype))
                num_param += util.get_type_size(ptype)