Example #1
0
    def jal(operands, label_dict, line_num):
        """
		Operation : %ra <- %pc + 1; %pc <- addr;
		Format    : [ 000011 |        addr        ]
		"""
        inst_bin = "000011" + Parser.parse_operand(operands[0], Operandtype.LABEL_ABSOLUTE, label_dict)[1]
        return utils.bin2bytes(inst_bin)
Example #2
0
    def out(operands, label_dict, line_num):
        """
		Operation : RS232C receiver
		Format : [ 011011 | 00000 | %rt | -----(16bit) ]
		"""
        inst_bin = (
            "01101100000" + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0] + "0000000000000000"
        )
        return utils.bin2bytes(inst_bin)
Example #3
0
    def bclf(operands, label_dict, line_num):
        """
		Operation : if(!FPCond) %pc <- %pc + 1 + $addr;
		Format : [ 010001 | 01000 | 00000 | $addr ]
		"""
        inst_bin = (
            "0100010100000000" + Parser.parse_operand(operands[0], Operandtype.LABEL_RELATIVE, label_dict, line_num)[1]
        )
        return utils.bin2bytes(inst_bin)
Example #4
0
    def jral(operands, label_dict, line_num):
        """
		Operation : %ra <- %pc + 1; %pc <- %rs
		Format    : [ 000000 | %rs | --- | --- | --- | 001001 ]
		"""
        inst_bin = (
            "000000" + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0] + "000000000000000001001"
        )
        return utils.bin2bytes(inst_bin)
Example #5
0
    def bne(operands, label_dict, line_num):
        """
		Operation : if(%rs != %rt) %pc <- %pc + 1 + addr
		Format    : [ 000101 | %rs | %rt |    addr    ]
		"""
        inst_bin = (
            "000101"
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[2], Operandtype.LABEL_RELATIVE, label_dict, line_num)[1]
        )
        return utils.bin2bytes(inst_bin)
Example #6
0
    def andi(operands, label_dict, line_num):
        """
		Operation : %rt <- %rs & $imm
		Format    : [ 001100 | %rs | %rt |    $imm    ]
		"""
        inst_bin = (
            "001100"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[2], Operandtype.IMMEDIATE)[1]
        )
        return utils.bin2bytes(inst_bin)
Example #7
0
    def fle(operands, label_dict, line_num):
        """
		Operation : FPcond = (%fs <= %ft) ? 1 : 0
		Format : [ 010001 | 10000 | %ft | %fs | --- | 111110 ]
		"""
        inst_bin = (
            "01000110000"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + "00000111110"
        )
        return utils.bin2bytes(inst_bin)
Example #8
0
    def fsw(operands, label_dict, line_num):
        """
		Operation : Mem[%rs + $imm] <- %ft
		Format    : [ 111001 | %rs | %ft |    $imm    ]
		"""
        inst_bin = (
            "111001"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_INDIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_INDIRECT, label_dict)[1]
        )
        return utils.bin2bytes(inst_bin)
Example #9
0
    def and_(operands, label_dict, line_num):
        """
		Operation : %rd <- %rs & %rt
		Format    : [ 000000 | %rd | %rs | %rt | --- | 100001 ]
		"""
        inst_bin = (
            "000000"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[2], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + +"00000100001"
        )
        return utils.bin2bytes(inst_bin)
Example #10
0
    def fsub(operands, label_dict, line_num):
        """
		Operation : %fd <- %fs -. %ft
		Format : [ 010001 | 10000 | %ft | %fs | %fd | 000001 ]
		"""
        inst_bin = (
            "01000110000"
            + Parser.parse_operand(operands[2], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + "000001"
        )
        return utils.bin2bytes(inst_bin)
Example #11
0
    def slt(operands, label_dict, line_num):
        """
		Operation : %rd <- (%rs < %rt) ? 1 : 0
		Format    : [ 000000 | %rs | %rt | %rd | --- | 101010 ]
		"""
        inst_bin = (
            "000000"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[2], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + "00000101010"
        )
        return utils.bin2bytes(inst_bin)
Example #12
0
    def sll(operands, label_dict, line_num):
        """
		Operation : %rd <- %rs << shamt
		Format    : [ 000000 | %rs | --- | %rd | shamt | 000000 ]
		"""
        inst_bin = (
            "000000"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + "00000"
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[2], Operandtype.SHAMT)[1]
            + "000000"
        )
        return utils.bin2bytes(inst_bin)
Example #13
0
    def srl(operands, label_dict, line_num):
        """
		Operation : %rd <- %rs >> shamt
		Format    : [ 000000 | %rs | --- | %rd | shamt | 000010 ]
		"""
        opecode_bin = format(int("0", 16), "06b")
        funct_bin = format(int("02", 16), "06b")
        inst_bin = (
            "000000"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + "00000"
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[2], Operandtype.SHAMT)[1]
            + "000010"
        )
        return utils.bin2bytes(inst_bin)
Example #14
0
    def addi(operands, label_dict, line_num):
        """
		Operation : %rt <- %rs + $imm
		Format    : [ 001000 | %rs | %rt |    $imm    ]
		"""
        imm_pattern = re.compile(r"\$-?\d+")
        match = imm_pattern.match(operands[2])
        if match is not None:
            imm_bin = Parser.parse_operand(operands[2], Operandtype.IMMEDIATE)[1]
        else:
            # operand may be label
            imm_bin = utils.imm2bin(label_dict[operands[2]])

        assert len(imm_bin) <= 16, "too large integer operand"

        inst_bin = (
            "001000"
            + Parser.parse_operand(operands[1], Operandtype.REGISTER_DIRECT)[0]
            + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
            + imm_bin
        )
        return utils.bin2bytes(inst_bin)
Example #15
0
    def flw(operands, label_dict, line_num):
        """
		Operation : %ft <- Mem[%rs + $imm]
		Format    : [ 110001 | %rs | %ft |    $imm    ]
		"""
        addressing_pattern = re.compile(r"(-?\d+)?\(.*\)")
        match = addressing_pattern.match(operands[1])
        if match is not None:
            # use addressing mode
            inst_bin = (
                "110001"
                + Parser.parse_operand(operands[1], Operandtype.REGISTER_INDIRECT)[0]
                + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
                + Parser.parse_operand(operands[1], Operandtype.REGISTER_INDIRECT)[1]
            )
        else:
            # loading address value
            inst_bin = (
                "11000100000"
                + Parser.parse_operand(operands[0], Operandtype.REGISTER_DIRECT)[0]
                + Parser.parse_operand(operands[1], Operandtype.LABEL_RELATIVE, label_dict)[1]
            )
        return utils.bin2bytes(inst_bin)
Example #16
0
 def hlt(operands, label_dict, line_num):
     inst_bin = "11111111111111111111111111111111"
     return utils.bin2bytes(inst_bin)