Пример #1
0
def load_instructions():
    file_data = get_file_data()
    int_codes = []
    instruction = Instruction()

    for i in range(len(file_data)):
        if (i % 4 == 0):
            instruction = Instruction()
            instruction.opcode = file_data[i]
        elif (i % 3 == 0 and i != 0):
            instruction.parameters.append(file_data[i])
            int_codes.append(instruction)
        else:
            instruction.parameters.append(file_data[i])

    return int_codes
Пример #2
0
    def parse(self, asm_file):
        """
        :param asm_file: .asm source file
        :return: parsed instructions
        """
        with open(asm_file, 'r') as f:
            asm_source = f.read()

        # First pass through the instructions.
        # Stores all instructions in a list, and builds the symbol table.
        instructions_list = []
        for raw_instruction in asm_source.split('\n'):
            raw_instruction = raw_instruction.strip()

            if not raw_instruction:  # Pass if empty line
                continue

            line_type = self.line_type(raw_instruction)

            if line_type == 'comment':
                instructions_list.append(self.parse_comment(raw_instruction))

            elif line_type == 'instruction':
                parsed_instruction = Instruction(raw_instruction)
                parsed_instruction.addr = self._pc
                self._pc += 2  # Since memory is byte-addressable and words are 16-bits

                # If we have li, split into lui and ori.
                if parsed_instruction.opname == 'li':
                    parsed_instruction.opname = 'li_lui'
                    parsed_instruction.opcode = self._codes.get_opcode('lui')
                    instructions_list.append(parsed_instruction)

                    # Ori part of instruction
                    ori_li_instruction = deepcopy(parsed_instruction)
                    ori_li_instruction.addr = self._pc
                    self._pc += 2
                    ori_li_instruction.opname = 'li_ori'
                    ori_li_instruction.opcode = self._codes.get_opcode('ori')
                    instructions_list.append(ori_li_instruction)
                else:
                    instructions_list.append(parsed_instruction)

            elif line_type == 'directive':
                # Needs to be a list since asciiz will return multiple lines of instructions.
                directive_list = self.parse_directive_to_list(raw_instruction)
                # Stores the first item's address to the symbol table
                first_in_mem = directive_list[0]
                self._symbol_table[first_in_mem.label] = self._pc
                for directive in directive_list:
                    directive.addr = self._pc
                    self._pc += 2
                    instructions_list.append(directive)

            else:
                assert line_type == 'label'
                self.store_label(raw_asm_line=raw_instruction, addr=self._pc)

        if len(instructions_list) > self._MAX:
            raise MemoryError("Instructions exceeded {}".format(self._MAX))

        encoded_instructions = []
        for instruction in instructions_list:
            encoded_instructions.append(self.encode(instruction))

        return encoded_instructions