def convert(code): code = code.replace("(", " ") code = code.replace(")", "") code = code.replace(",", " ") code = code.replace(" ", " ") args = code.split(" ") instruction = args[0] if instruction == "exit": sys.exit(0) codes = instr_decode(instruction) func_type = codes[0] reg_values = reg_decode(func_type, instruction, args[1:]) #get the numeric values of the registers #the following if statement below prints an error if needed if reg_values == None: print("Not a valid MIPS statement") return f = open("../src/rams_init_file.data", "a+") #execution for r-type functions if func_type == "r": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) rd = '{0:05b}'.format(reg_values[2]) shamt = '{0:05b}'.format(reg_values[3]) funct = '{0:06b}'.format(codes[2]) binary = opcode + rs + rt + rd + shamt + funct #print(binary) f.write("%s\n" % binary) #execution for i-type functions elif func_type == "i": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) imm = '{0:016b}'.format(reg_values[2]) binary = opcode + rs + rt + imm #print(binary) f.write("%s\n" % binary) #execution for j-type functions elif func_type == "j": opcode = '{0:06b}'.format(codes[1]) imm = '{0:026b}'.format(reg_values[0]) binary = opcode + imm #print(binary) f.write("%s\n" % binary) else: print("Not a valid MIPS statement") return f.close() return
def convert(code): code = code.replace("(", " ") code = code.replace(")", "") code = code.replace(",", " ") code = code.replace(" ", " ") args = code.split(" ") instruction = args[0] if instruction == "exit": sys.exit() codes = instr_decode(instruction) func_type = codes[0] reg_values = reg_decode(func_type, instruction, args[1:]) #get the numeric values of the registers #the following if statement below prints an error if needed if reg_values == None: print("Not a valid MIPS statement") return #execution for r-type functions if func_type == "r": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) rd = '{0:05b}'.format(reg_values[2]) shamt = '{0:05b}'.format(reg_values[3]) funct = '{0:06b}'.format(codes[2]) print("Function type: R-Type") print("Instruction form: opcode| rs | rt | rd |shamt| funct") print("Formatted binary: "+opcode+"|"+rs+"|"+rt+"|"+rd+"|"+shamt+"|"+funct) binary = "0b"+opcode+rs+rt+rd+shamt+funct print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) print("Hex: 0x" + hex_string) #execution for i-type functions elif func_type == "i": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) imm = '{0:016b}'.format(reg_values[2]) print("Function type: I-Type") print("Instruction form: opcode| rs | rt | immediate ") print("Formatted binary: " + opcode+"|"+rs+"|"+rt+"|"+imm) binary = "0b"+opcode+rs+rt+imm print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) print("Hex: 0x" + hex_string) #execution for j-type functions elif func_type == "j": opcode = '{0:06b}'.format(codes[1]) imm = '{0:026b}'.format(reg_values[0]) print("Function type: I-Type") print("Instruction form: opcode| immediate ") print("Formatted binary: " + opcode+"|"+imm) binary = "0b"+opcode+imm print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) print("Hex: 0x" + hex_string) else: print("Not a valid MIPS statement") return return
def convert(code): code = code.replace("(", " ") code = code.replace(")", "") code = code.replace(",", " ") code = code.replace(" ", " ") code = code.replace("\n", " ") args = code.split(" ") instruction = args[0] #print("\n"+ str(args)) if instruction == "exit": sys.exit() codes = instr_decode(instruction) print(codes) func_type = codes[0] if func_type != "n": reg_values = reg_decode(func_type, instruction, args[1:4]) #get the numeric values of the registers #the following if statement below prints an error if needed if reg_values == None: print("Not a valid MIPS statement") return #execution for r-type functions if func_type == "r": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) rd = '{0:05b}'.format(reg_values[2]) shamt = '{0:05b}'.format(reg_values[3]) funct = '{0:06b}'.format(codes[2]) # print("Function type: R-Type") # print("Instruction form: opcode| rs | rt | rd |shamt| funct") # print("Formatted binary: "+opcode+"|"+rs+"|"+rt+"|"+rd+"|"+shamt+"|"+funct) binary = opcode+rs+rt+rd+shamt+funct # print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) # print("Hex: 0x" + hex_string) elif func_type == "n": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(0x00) rt = '{0:05b}'.format(0x00) rd = '{0:05b}'.format(0x00) shamt = '{0:05b}'.format(0x00) funct = '{0:06b}'.format(codes[2]) # print("Function type: R-Type") # print("Instruction form: opcode| rs | rt | rd |shamt| funct") # print("Formatted binary: "+opcode+"|"+rs+"|"+rt+"|"+rd+"|"+shamt+"|"+funct) binary = opcode+rs+rt+rd+shamt+funct # print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) # print("Hex: 0x" + hex_string) #execution for i-type functions elif func_type == "i": opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) imm = '{0:016b}'.format(reg_values[2]) # print("Function type: I-Type") # print("Instruction form: opcode| rs | rt | immediate ") # print("Formatted binary: " + opcode+"|"+rs+"|"+rt+"|"+imm) binary = opcode+rs+rt+imm # print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) # print("Hex: 0x" + hex_string) #execution for j-type functions elif func_type == "j": opcode = '{0:06b}'.format(codes[1]) imm = '{0:026b}'.format(reg_values[0]) # print("Function type: I-Type") # print("Instruction form: opcode| immediate ") # print("Formatted binary: " + opcode+"|"+imm) binary = opcode+imm # print("Binary: " + binary) hex_string = '{0:08x}'.format(int(binary, base=2)) # print("Hex: 0x" + hex_string) else: print("Not a valid MIPS statement") return return hex_string
def convert(code): code = code.replace("(", " ") code = code.replace(")", "") code = code.replace(",", " ") code = code.replace(" ", " ") args = code.split(" ") instruction = args[0] if instruction == "exit": sys.exit() codes = instr_decode(instruction) func_type = codes[0] reg_values = reg_decode(func_type, instruction, args[1:]) #get the numeric values of the registers #the following if statement below prints an error if needed if reg_values == None: print("Instruccion no valida por None") return #execution for r-type functions if func_type == "r": #("Instruction form: opcode| rs | rt | rd |shamt| funct") opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) rd = '{0:05b}'.format(reg_values[2]) shamt = '{0:05b}'.format(reg_values[3]) funct = '{0:06b}'.format(codes[2]) #print("Formatted binary: "+opcode+"|"+rs+"|"+rt+"|"+rd+"|"+shamt+"|"+funct) binary = opcode + rs + rt + rd + shamt + funct print(binary) return binary #execution for i-type functions elif func_type == "i": #("Instruction form: opcode| rs | rt | immediate ") opcode = '{0:06b}'.format(codes[1]) rs = '{0:05b}'.format(reg_values[0]) rt = '{0:05b}'.format(reg_values[1]) imm = '{0:016b}'.format(reg_values[2]) #print("Formatted binary: " + opcode+"|"+rs+"|"+rt+"|"+imm) binary = opcode + rs + rt + imm print(binary) return binary #execution for j-type functions elif func_type == "j": #("Instruction form: opcode| immediate ") opcode = '{0:06b}'.format(codes[1]) imm = '{0:026b}'.format(reg_values[0]) #print("Formatted binary: " + opcode+"|"+imm) binary = opcode + imm print(binary) return binary elif func_type == "nop": print("00000000000000000000000000000000") return "00000000000000000000000000000000" elif func_type == "halt": print("11111111111111111111111111111111") return "11111111111111111111111111111111" else: print("Instruccion no valida") return return