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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
def hlt(operands, label_dict, line_num): inst_bin = "11111111111111111111111111111111" return utils.bin2bytes(inst_bin)