1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| import random
list_R = ["addu", "subu", "and", "or", "nor", "xor", "sltu", "slt", "sllv", "srlv", "srav"] list_I = ["andi", "addiu", "ori", "xori", "lui", "slti", "sltiu"] list_LS = ["lw", "sw", "lh", "lhu", "sh", "lb", "lbu", "sb"] list_shift = ["sll", "srl", "sra"] list_B = ["bne", "beq","bgtz", "blez", "bltz", "bgez"] list_MD = ["mult", "multu", "div", "divu"] list_MTMF = ["mfhi", "mflo", "mthi", "mtlo"]
length = 8
def R_test(file, n): for i in range(n): k = random.randint(0, 10000000) % len(list_R) rs = random.randint(0, 10000000) % length; rt = random.randint(0, 10000000) % length; rd = random.randint(0, 10000000) % length; s = "{} ${}, ${}, ${}\n".format(list_R[k], rd, rs, rt) file.write(s)
def I_test(file, n): for i in range(n): k = random.randint(0, 10000000) % len(list_I) rs = random.randint(0, 10000000) % length rt = random.randint(0, 10000000) % length imm = random.randint(-32768, 32768) abs_imm = random.randint(0, 65536) if list_I[k] == "lui": s = "{} ${},{}\n".format(list_I[k], rt, abs_imm) else: s = "{} ${}, ${}, {}\n".format(list_I[k], rt, rs, imm) file.write(s)
def LS_test(file, n): for i in range(n): k = random.randint(0,10000000) % len(list_LS) ins = list_LS[k] num = 0 if(ins[1] == "w"): num = (random.randint(0,10000000) << 2) % 4096 elif(ins[1] == "h"): num = (random.randint(0,10000000) << 1) % 4096 else: num = (random.randint(0,10000000)) % 4096 rt = random.randint(0, 10000000) % length s = "{} ${}, {}($0)\n".format(ins, rt, num) file.write(s)
def shift_test(file, n): for i in range(n): k = random.randint(0,10000000) % len(list_shift) shamt = random.randint(0, 10000000) % length rd = random.randint(0, 10000000) % length rt = random.randint(0, 10000000) % length s = "{} ${}, ${}, {}\n".format(list_shift[k], rd, rt, shamt) file.write(s)
def MD_test(file, n): for i in range(n): k = random.randint(0,10000000) % len(list_MD) rs = random.randint(0, 10000000) % length rt = random.randint(0, 10000000) % length if(list_MD[k] == "mult" or list_MD[k] == "mul"): s = "{} ${}, ${}\n".format(list_MD[k], rs, rt) else: s = "{} ${}, ${}\n".format(list_MD[k], rs, 8) file.write(s)
def MTMF_test(file, n): for i in range(n): k = random.randint(0,10000000) % len(list_MTMF) rs = random.randint(0, 10000000) % length s = "{} ${}\n".format(list_MTMF[k], rs) file.write(s)
def B_test(file, lable): k = random.randint(0,10000000) % len(list_B) if(k == 0 or k == 1): rs = random.randint(0, 10000000) % length rt = random.randint(0, 10000000) % length s = "{} ${}, ${}, {}\n".format(list_B[k], rs, rt, lable) file.write(s) else: rs = random.randint(0, 10000000) % length s = "{} ${}, {}\n".format(list_B[k], rs, str(lable)) file.write(s)
def b_begin(file, n): file.write("\nb_test_{}_one:\n".format(n)) B_test(file, "b_test_{}_one_then".format(n)) R_test(file,1)
file.write("b_test_{}_two:\n".format(n)) B_test(file, "b_test_{}_two_then".format(n)) I_test(file,1)
file.write("jal_test_{}:\n".format(n)) file.write("jal jal_test_{}_then\n".format(n)) I_test(file,1)
file.write("end_{}:\n\n".format(n))
R_test(file, 1) I_test(file, 1) LS_test(file, 1) shift_test(file, 1)
def b_end(file, n): file.write("\nb_test_{}_one_then:\n".format(n)) R_test(file, 1) I_test(file, 1) LS_test(file, 1) shift_test(file, 1) MD_test(file, 1) MTMF_test(file, 1) file.write("j b_test_{}_two\n".format(n)) R_test(file, 1) file.write("\nb_test_{}_two_then:\n".format(n)) R_test(file, 1) I_test(file, 1) LS_test(file, 1) shift_test(file, 1) MD_test(file, 1) MTMF_test(file, 1) file.write("jal jal_test_{}\n".format(n)) file.write("addu $1, $ra, $0\n")
file.write("\njal_test_{}_then:\n".format(n)) R_test(file, 1) I_test(file, 1) LS_test(file, 1) shift_test(file, 1) MD_test(file, 1) MTMF_test(file, 1) file.write("addiu $ra,$ra, 8\n".format(n)) B_test(file, "end_{}".format(n)) R_test(file, 1) I_test(file, 1) LS_test(file, 1) shift_test(file, 1) MD_test(file, 1) MTMF_test(file, 1) file.write("jr $ra\n".format(n))
with open("mips_code.asm", "w") as file: for i in range(length): temp = random.randint(-2147483648, 2147483648) s = "li ${} {}\n".format(i, temp) file.write(s) file.write("li $8, {}\n".format(random.randint(-2147483648, 2147483648) % 10000 + 1))
for i in range(10): R_test(file, 2) MD_test(file,1) MTMF_test(file, 1) I_test(file, 2) LS_test(file, 2) MTMF_test(file, 1) I_test(file, 2) shift_test(file, 2) file.write("\n")
|