Esempio n. 1
0
def readFile(file, instructionList):
    instructionNumber = 0  # Instruction number depending on the line that it is.
    lineInFile = 0  # Line that the instruction is in file
    groupCounter = 0  # Count how many groups of instructions the program have
    jumpList = []  # Aux list that will contain all jump instructions.

    for line in file:  # Run line by line in file
        line = line.rstrip()  # Copies line in file for the variable "line"
        if not ':' in line:  # Select just the instructions, because instructions don't have ":" in their names.

            instructionNumber = instructionNumber + 1  # Increasing number of instruction aux variable
            instruction = Instruction(
                False, False, False, False, False,
                False)  # Initialize a instruction object.

            name = line.split().__getitem__(
                0
            )  # Separate every word from a unique line, and get '0' position, which is the name of instruction

            instruction.setNumber(
                instructionNumber)  # Setting number of the instruction
            instruction.setName(name)  # Setting name of instruction
            instruction.setLine(
                lineInFile)  # Setting line number of this instruction in file

            if name == 'jmp' or name == 'je' or name == 'jne' or name == 'jg' or name == 'jge' or name == 'jl' or name == 'jle':  # If name is a deviation Instruction so...
                jumpName = line.split().__getitem__(
                    1
                )  # Get the second word from this line, and here is the name of a group of instructions
                instruction.setOP1(
                    jumpName
                )  # Setting deviation group name in a JUMP instruction

                if name == "jmp":  # Instruction "jmp" will always deviate.
                    instruction.setIsDeviate(True)
                else:
                    instruction.setIsDeviate(False)

            else:  # Is not a deviation instruction.
                operation_1 = line.split().__getitem__(
                    1)  # Getting first operator
                operation_1 = operation_1.replace(",", "")
                operation_2 = line.split().__getitem__(
                    2)  # Getting second operator
                if not ";" in operation_1:  # ";" means that is a "comment"
                    instruction.setOP1(
                        operation_1)  # Setting operator 1. (Second column)
                if not ";" in operation_2:
                    instruction.setOP2(
                        operation_2
                    )  # Setting operator 2. Maybe it doesn't exist (Third column)
                if instruction.getName() == "ret" or instruction.getName(
                ) == "leave":  # Instructions that have no operators.
                    instruction.setOP1(
                        False
                    )  # Setting false in OP1 means that have no operator
                    instruction.setOP2(
                        False
                    )  # Setting false in OP2 means that have no operator
                instruction.setIsDeviate(False)  # Means no deviation

            instructionList.append(
                instruction
            )  # Appending one element on a list of instructions.

            lineInFile = lineInFile + 1  # Increment line in file.

        else:  # Here will get just de grouo of instructions names.
            if not '_' in line:  # Here is the lines that countains the name of a group of instruction from a deviation
                groupCounter = groupCounter + 1
                jump = Instruction(False, False, False, False, False,
                                   False)  # Iniatialize the object jump

                name = line.replace(':', "")  # Erasing ":" from this line

                jump.setName(name)  # Setting name in this object
                jump.setLine(
                    lineInFile - groupCounter +
                    1)  # Setting the instruction number that deviate will jump
                jumpList.append(
                    jump
                )  # Appending one element on a list of group instruction names

            lineInFile = lineInFile + 1

    os.system('cls' if os.name == 'nt' else 'clear')  # Clearing console

    # Lines from 48 to 57, is to set "JumpLine" in "Jump" instructions,
    # JumpLine: Line which the program is going to jump.
    for i in range(len(instructionList)):
        name = instructionList[i].getName()
        jumpName = ""

        if name == 'jmp' or name == 'je' or name == 'jne' or name == 'jg' or name == 'jge' or name == 'jl' or name == 'jle':
            jumpName = instructionList[i].getOP1()

            for j in range(len(jumpList)):  # Runs the jumpList list.
                if jumpName == jumpList[j].getName(
                ):  # Find the instruction that have to jump.
                    instructionList[i].setOP2(
                        jumpList[j].getLine()
                    )  # Setting the number of instruction that the jump instruction have to jump.
    file.close()  # Close file
    return instructionList