如何修改简单的汇编代码更加真实?

时间:2014-01-26 13:50:28

标签: python assembly mips

[我已经实现了组装15个mips指令的简单汇编程序,程序从如图所示的用户获取指令并打印相应的机器代码输出txt文件,我的问题是如何修改代码使汇编程序需要无限的不。指令,直到用户中止他自己和他自己的程序在输出文件的新行中打印每个32位指令]

这是代码:

inst = raw_input("enter your instruction operation:")

if ((inst[0] == 'a' and inst[1] == 'd' and inst[2] == 'd' and inst[3] != 'i') or (inst[0] == 'a' and inst[1] == 'n' and inst[2] == 'd' and inst[3] != 'i') or (inst[0] == 'o' and inst[1] == 'r'and inst[2] != 'i') or (inst[0] == 'n' and inst[1] == 'o' and inst[2] == 'r') or (inst[0] == 's' and inst[1] == 'u' and inst[2] == 'b') or (inst[0] == 's' and inst[1] == 'l' and inst[2] == 'l') or(inst[0] == 's' and inst[1] == 'l' and inst[2] == 't')):

        Rs = raw_input("enter the first operand:")
        for i in range (0,32):
         if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
            opcode = '000000'
            Mc_file = open("output.txt", "w")
            Mc_file.write(str(opcode) + str(rs))
            Mc_file.close()
        Rt = raw_input("enter the 2nd operand:")
        for i in range (0,32):
         if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
            opcode = '000000'
            Mc_file = open("output.txt", "w")
            Mc_file.write(str(opcode) + str(rs) + str(rt))
            Mc_file.close()
        Rd = raw_input("enter the destination operand:")
        for i in range (0,32):
         if(Rd == '$'+ str(i)):
            rd = "{0:05b}".format(i)
            opcode = '000000'
            shamt ='00000'
            Mc_file = open("output.txt", "w")
            Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt)
            Mc_file.close()

        if(inst[0] == 'a' and inst[1] == 'd' and inst[2] == 'd'):
              ass_inst = str(inst) + ' ' + str(Rd) + ',' + str(Rs) + ',' + str(Rt)
              print "your assembly instruction is: %s" % (ass_inst)
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('100000'))
              Mc_file.close()
        if(inst[0] == 'a' and inst[1] == 'n' and inst[2] == 'd'):
              ass_inst = str(inst) + ' ' + str(Rd) + ',' + str(Rs) + ',' + str(Rt)
              print "your assembly instruction is: %s" % (ass_inst)
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('100100'))
              Mc_file.close()
        if(inst[0] == 'o' and inst[1] == 'r'):
              ass_inst = str(inst) + ' ' + str(Rd) + ',' + str(Rs) + ',' + str(Rt)
              print "your assembly instruction is: %s" % (ass_inst)
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('100101'))
              Mc_file.close()
        if(inst[0] == 'n' and inst[1] == 'o' and inst[2] == 'r'):
              ass_inst = str(inst) + ' ' + str(Rd) + ',' + str(Rs) + ',' + str(Rt)
              print "your assembly instruction is: %s" % (ass_inst)
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('100111'))
              Mc_file.close()
        if(inst[0] == 's' and inst[1] == 'u' and inst[2] == 'b'):
              ass_inst = str(inst) + ' ' + str(Rd) + ',' + str(Rs) + ',' + str(Rt)
              print "your assembly instruction is: %s" % (ass_inst)
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('100010'))
              Mc_file.close()
              """ if(inst[0] == 'j' and inst[1] == 'r'):
              ass_inst = str(inst) + ' ' + str(Rd) + ',' + str(Rs) + ',' + str(Rt)
              print "your assembly instruction is: %s" % (ass_inst)
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str('00000') + str('00000') +str(rd) + shamt + str('001000'))
              Mc_file.close()"""
        if(inst[0] == 's' and inst[1] == 'l' and inst[2] == 'l'):
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('000000'))
              Mc_file.close()
        if(inst[0] == 's' and inst[1] == 'l' and inst[2] == 't'):
              Mc_file = open("output.txt", "w")
              Mc_file.write(str(opcode) + str(rs) + str(rt) +str(rd) + shamt + str('101010'))
              Mc_file.close()
elif (inst[0] == 'l' and inst[1] == 'w'):
    Rs = raw_input("enter the first operand:",)
    for i in range (0,32):
     if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
    Rt = raw_input("enter the destination operand:")
    for i in range (0,32):
     if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
    imediate = input("Enter the value of the offset:")
    ass_inst = str(inst) + ' ' + str(Rs) + ',' + str(str(imediate) + '('+ str(Rt) + ')')

    print " Entered assembly instruction is: %s " % ass_inst
    opcode ='100011'
    imd = "{0:016b}".format(imediate)
    Mc_file = open("output.txt", "w")
    Mc_file.write(str(opcode) + str(rs) + str(rt) + str(imd))
    Mc_file.close()
elif (inst[0] == 's' and inst[1] == 'w'):
    Rs = raw_input("enter the destination operand:")
    for i in range (0,32):
     if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
    Rt = raw_input("enter the source operand:")
    for i in range (0,32):
     if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
    imediate = input("Enter the value of the offset:")
    ass_inst = str(inst) + ' ' + str(Rs) + ',' + str(str(imediate) + '('+ str(Rt) + ')')
    print "your assembly instruction is: %s" % (ass_inst)
    imd = "{0:016b}".format(imediate)
    opcode = '101011'
    Mc_file = open("output.txt", "w")
    Mc_file.write(str(opcode) + str(rs) + str(rt) + str(imd))
    Mc_file.close()

elif inst[0] == 'j' and inst[1] == 'r':
    dr = raw_input("enter the destination register:")
    inst_ = str(inst) + ' ' + str(dr)
    print "The entered assembly instruction is %s" % inst_
    for i in range(0,32):
     if(dr == '$'+ str(i)):
        addrss = "{0:026b}".format(i)
        Mc_file = open("output.txt", "w")
        opcode = '000011'
        Mc_file.write(str(opcode) + str(addrss))
        Mc_file.close()
elif inst[0] == 'b' and inst[1] == 'e' and inst[2] == 'q':
       Rs = raw_input("enter the first operand to be compared:",)
       for i in range (0,32):
        if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
       Rt = raw_input("enter the second operand to be compared:")
       for i in range (0,32):
        if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
       imediate = input("Enter the branch target address:")
       ass_inst = (str(inst) + ' ' + str(Rs) + ',' + str(Rt) + ' ' + str(imediate))
       print " Entered assembly instruction is: %s " % ass_inst
       imd = "{0:016b}".format(imediate)
       opcode = '000100'
       Mc_file = open("output.txt", "w")
       Mc_file.write(str(opcode) + str(rs) + str(rt) + str(imd))
       Mc_file.close() 
elif (inst[0] == 'a' and inst[1] == 'n' and inst[2] == 'd'and inst[3] == 'i'):
     Rs = raw_input("enter the first operand:")
     for i in range (0,32):
        if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
     Rt = raw_input("enter the second operand:")
     for i in range (0,32):
        if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
     imediate = input("Enter the Imediate number:")
     ass_inst = (str(inst) + ' ' + str(Rs) + ',' + str(Rt) + ' ' + str(imediate))
     print " Entered assembly instruction is: %s " % ass_inst
     imd = "{0:016b}".format(imediate)
     opcode = '000100'
     Mc_file = open("output.txt", "w")
     Mc_file.write(str(opcode) + str(rs) + str(rt) + str(imd))
     Mc_file.close() 

elif (inst[0] == 'o' and inst[1] == 'r' and inst[2] == 'i'):
     Rs = raw_input("enter the first operand:")
     for i in range (0,32):
        if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
     Rt = raw_input("enter the second operand:")
     for i in range (0,32):
        if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
     imediate = input("Enter the Imediate number:")
     ass_inst = (str(inst) + ' ' + str(Rs) + ',' + str(Rt) + ' ' + str(imediate))
     print " Entered assembly instruction is: %s " % ass_inst
     imd = "{0:016b}".format(imediate)
     opcode = '001101'
     Mc_file = open("output.txt", "w")
     Mc_file.write(str(opcode) + str(rs) + str(rt) + str(imd))
     Mc_file.close() 
elif (inst[0] == 'a' and inst[1] == 'd' and inst[2] == 'd'and inst[3] == 'i'):
     Rs = raw_input("enter the first operand:")
     for i in range (0,32):
        if(Rs == '$'+ str(i)):
            rs = "{0:05b}".format(i)
     Rt = raw_input("enter the second operand:")
     for i in range (0,32):
        if(Rt == '$'+ str(i)):
            rt = "{0:05b}".format(i)
     imediate = input("Enter the Imediate number:")
     ass_inst = (str(inst) + ' ' + str(Rs) + ',' + str(Rt) + ' ' + str(imediate))
     print " Entered assembly instruction is: %s " % ass_inst
     imd = "{0:016b}".format(imediate)
     opcode = '001000'
     Mc_file = open("output.txt", "w")
     Mc_file.write(str(opcode) + str(rs) + str(rt) + str(imd))
     Mc_file.close() 
else:
    print "Not supported operation!"

1 个答案:

答案 0 :(得分:1)

您可以使用while True无限循环:

while True:
    inst = raw_input("enter your instruction operation or 'q' to quit:")
    if inst.lower() in ("q", "quit"):
        break
    # rest of code goes here

您可以{@ 1}}此循环之前的文件和open一行一行,或者每次通过write(“追加”)中的循环重新打开文件模式。