SHARE
TWEET

Untitled

Bondok26 Nov 19th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from data import instruction_table, register_table
  2. from binHex_converter import hex2bin
  3.  
  4. file = open('testfile.txt', 'r')
  5. lines = file.readlines()
  6. print(lines)
  7. file.close()
  8. '''
  9. input_lines = ["add $s1,$s2,$s3", "beq $s1,$s2,3"]
  10. instruction_array = []
  11. lable_dict =[]
  12. input2 = ["2000 :j 2004 "]
  13. '''
  14.  
  15. def parse(lines):
  16.     for line_i, line in enumerate(lines):
  17.         if ':' in line:
  18.             lable = line.split(':')[0]
  19.             lable_dict[line_i] = lable
  20.             line = line[3:]
  21.             # print(lable_dict)
  22.         instruction = line.strip().replace(' ', ',', 1).replace(')', '').replace('(', ',').replace('\n', '')
  23.         instruction = instruction.replace(' ', '').split(',')
  24.         instruction_array.append(instruction)
  25.     return instruction_array
  26.  
  27.  
  28. parse(lines)
  29. print(instruction_array)
  30.  
  31.  
  32. # the excution  every_list means every line but cut into  list elemnts
  33.  
  34. for every_list in instruction_array:
  35.     if str(every_list[0]) == "add":
  36.         shamt = "00000"
  37.         Mc_file = open("output.txt", "a")
  38.         Mc_file.write(str(hex2bin(instruction_table.get("add").get("opcode"), 6)) + str(
  39.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[3]), 5)) + str(
  40.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  41.             hex2bin(instruction_table.get("add").get("func"), 6)) + "\n")
  42.         Mc_file.close()
  43.  
  44.     elif str(every_list[0]) == "and":
  45.         shamt = "00000"
  46.         Mc_file = open("output.txt", "a")
  47.         Mc_file.write(str(hex2bin(instruction_table.get("and").get("opcode"), 6)) + str(
  48.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[3]), 5)) + str(
  49.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  50.             hex2bin(instruction_table.get("and").get("func"), 6)) + "\n")
  51.         Mc_file.close()
  52.  
  53.     elif str(every_list[0]) == "or":
  54.         shamt = "00000"
  55.         Mc_file = open("output.txt", "a")
  56.         Mc_file.write(str(hex2bin(instruction_table.get("or").get("opcode"), 6)) + str(
  57.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[3]), 5)) + str(
  58.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  59.             hex2bin(instruction_table.get("or").get("func"), 6)) + "\n")
  60.         Mc_file.close()
  61.  
  62.     elif str(every_list[0]) == "sub":
  63.         shamt = "00000"
  64.         Mc_file = open("output.txt", "a")
  65.         Mc_file.write(str(hex2bin(instruction_table.get("sub").get("opcode"), 6)) + str(
  66.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[3]), 5)) + str(
  67.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  68.             hex2bin(instruction_table.get("sub").get("func"), 6)) + "\n")
  69.         Mc_file.close()
  70.  
  71.     elif str(every_list[0]) == "slt":
  72.         shamt = "00000"
  73.         Mc_file = open("output.txt", "a")
  74.         Mc_file.write(str(hex2bin(instruction_table.get("slt").get("opcode"), 6)) + str(
  75.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[3]), 5)) + str(
  76.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  77.             hex2bin(instruction_table.get("slt").get("func"), 6)) + "\n")
  78.         Mc_file.close()
  79.  
  80.     elif str(every_list[0]) == "nor":
  81.         shamt = "00000"
  82.         Mc_file = open("output.txt", "a")
  83.         Mc_file.write(str(hex2bin(instruction_table.get("nor").get("opcode"), 6)) + str(
  84.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[3]), 5)) + str(
  85.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  86.             hex2bin(instruction_table.get("nor").get("func"), 6)) + "\n")
  87.         Mc_file.close()
  88.  
  89.     elif str(every_list[0]) == "sll":
  90.         shamt = str(dec2bin(every_list[3], 5))
  91.         Mc_file = open("output.txt", "a")
  92.         Mc_file.write(str(hex2bin(instruction_table.get("sll").get("opcode"), 6)) + "00000" + str(
  93.             hex2bin(register_table.get(every_list[2]), 5)) + str(
  94.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  95.             hex2bin(instruction_table.get("sll").get("func"), 6)) + "\n")
  96.         Mc_file.close()
  97.  
  98.     elif str(every_list[0]) == "srl":
  99.         shamt = str(dec2bin(every_list[3], 5))
  100.         Mc_file = open("output.txt", "a")
  101.         Mc_file.write(str(hex2bin(instruction_table.get("srl").get("opcode"), 6)) + "00000" + str(
  102.             hex2bin(register_table.get(every_list[2]), 5)) + str(
  103.             hex2bin(register_table.get(every_list[1]), 5)) + shamt + str(
  104.             hex2bin(instruction_table.get("srl").get("func"), 6)) + "\n")
  105.         Mc_file.close()
  106.  
  107.     elif str(every_list[0]) == "beq":
  108.         shamt = "00000"
  109.         Mc_file = open("output.txt", "a")
  110.         Mc_file.write(str(hex2bin(instruction_table.get("beq").get("opcode"), 6)) + str(
  111.             hex2bin(register_table.get(every_list[1]), 5)) + str(hex2bin(register_table.get(every_list[2]), 5)) + str(
  112.             hex2bin(every_list[3], 16)) + "\n")
  113.         Mc_file.close()
  114.  
  115.     elif str(every_list[0]) == "bne":
  116.         shamt = "00000"
  117.         Mc_file = open("output.txt", "a")
  118.         Mc_file.write(str(hex2bin(instruction_table.get("bne").get("opcode"), 6)) + str(
  119.             hex2bin(register_table.get(every_list[1]), 5)) + str(hex2bin(register_table.get(every_list[2]), 5)) + str(
  120.             hex2bin(every_list[3], 16)) + "\n")
  121.         Mc_file.close()
  122.  
  123.     elif str(every_list[0]) == "addi":
  124.         shamt = "00000"
  125.         Mc_file = open("output.txt", "a")
  126.         Mc_file.write(str(hex2bin(instruction_table.get("addi").get("opcode"), 6)) + str(
  127.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[1]), 5)) + str(
  128.             hex2bin(every_list[3], 16)) + "\n")
  129.         Mc_file.close()
  130.  
  131.     elif str(every_list[0]) == "ori":
  132.         shamt = "00000"
  133.         Mc_file = open("output.txt", "a")
  134.         Mc_file.write(str(hex2bin(instruction_table.get("ori").get("opcode"), 6)) + str(
  135.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[1]), 5)) + str(
  136.             hex2bin(every_list[3], 16)) + "\n")
  137.         Mc_file.close()
  138.  
  139.     elif str(every_list[0]) == "andi":
  140.         shamt = "00000"
  141.         Mc_file = open("output.txt", "a")
  142.         Mc_file.write(str(hex2bin(instruction_table.get("andi").get("opcode"), 6)) + str(
  143.             hex2bin(register_table.get(every_list[2]), 5)) + str(hex2bin(register_table.get(every_list[1]), 5)) + str(
  144.             hex2bin(every_list[3], 16)) + "\n")
  145.         Mc_file.close()
  146.  
  147.     elif str(every_list[0]) == "lw":
  148.         shamt = "00000"
  149.         Mc_file = open("output.txt", "a")
  150.         Mc_file.write(str(hex2bin(instruction_table.get("lw").get("opcode"), 6)) + str(
  151.             hex2bin(register_table.get(every_list[3]), 5)) + str(hex2bin(register_table.get(every_list[1]), 5)) + str(
  152.             hex2bin(every_list[2], 16)) + "\n")
  153.         Mc_file.close()
  154.  
  155.     elif str(every_list[0]) == "sw":
  156.         shamt = "00000"
  157.         Mc_file = open("output.txt", "a")
  158.         Mc_file.write(str(hex2bin(instruction_table.get("sw").get("opcode"), 6)) + str(
  159.             hex2bin(register_table.get(every_list[3]), 5)) + str(hex2bin(register_table.get(every_list[1]), 5)) + str(
  160.             hex2bin(every_list[2], 16)) + "\n")
  161.         Mc_file.close()
  162.  
  163.     elif str(every_list[0]) == "jr":
  164.         shamt = "00000"
  165.         Mc_file = open("output.txt", "a")
  166.         Mc_file.write(str(hex2bin(instruction_table.get("jr").get("opcode"), 6)) + str(
  167.             hex2bin(register_table.get(every_list[1]), 5)) + "00000" + "00000" + shamt + str(
  168.             hex2bin(instruction_table.get("jr").get("func"), 6)) + "\n")
  169.         Mc_file.close()
  170.  
  171.     elif str(every_list[0]) == "j":
  172.         Mc_file = open("output.txt", "a")
  173.         Mc_file.write(str(hex2bin(instruction_table.get("j").get("opcode"), 6)) + str(hex2bin(every_list[1], 26)) + "\n")
  174.         Mc_file.close()
  175.  
  176.     elif str(every_list[0]) == "jal":
  177.         Mc_file = open("output.txt", "a")
  178.         Mc_file.write(str(hex2bin(instruction_table.get("jal").get("opcode"), 6)) + str(hex2bin(every_list[1], 26)) + "\n")
  179.         Mc_file.close()
  180.     else:
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top