如何在IDE中使用python代码

时间:2014-01-25 21:33:08

标签: python file assembly output mips

我已经实现了简单的Mips指令集汇编程序,它只支持15个输入指令集,例如(add,and,or,nor,ori,lw,sw,jr,jal,sub,slt,sll,andi,addi, beq),该程序是从用户&采取输入单指令。在输出.txt文件中输出机器代码,我想做一些修改,第一个是让用户输入多个指令,直到他想要停止以及如何在新行中打印.txt文件中的下一个指令,我如何使用IDE中的程序我的意思是,如果我想将汇编程序发送给某人直接使用它我应该怎么做,提前感谢。 这是代码:

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

if ((inst[0] == 'a' and inst[1] == 'd' and inst[2] == 'd') or (inst[0] == 'a' and inst[1] == 'n' and inst[2] == 'd') or (inst[0] == 'o' and inst[1] == 'r') 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] == 'j' and inst[1] == 'r') 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'):
              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'):
              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'):
              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'):
              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'):
              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'):
        Mc_file = open("output.txt", "w")
        opcode = '100011'
        Mc_file.write(str(opcode))
        Mc_file.close()
elif inst[0] == 's' and inst[1] == 'w':
        Mc_file = open("output.txt", "w")
        opcode = '101011'
        Mc_file.write('Opcode =' + str(opcode))
        Mc_file.close()
elif inst[0] == 'j' and inst[1] == 'a' and inst[2] == 'l':
        Mc_file = open("output.txt", "w")
        opcode = '000011'
        Mc_file.write('Opcode =' + str(opcode))
        Mc_file.close()
elif inst[0] == 'b' and inst[1] == 'e' and inst[2] == 'q':
        Mc_file = open("output.txt", "w")
        opcode = '000100'
        Mc_file.write('Opcode =' + str(opcode))
        Mc_file.close()
elif inst[0] == 'a' and inst[1] == 'n' and inst[2] == 'd'and inst[3] == 'i':
        Mc_file = open("output.txt", "w")
        opcode = '001100'
        Mc_file.write('Opcode =' + str(opcode))
        Mc_file.close()
elif inst[0] == 'o' and inst[1] == 'r' and inst[2] == 'i':
        Mc_file = open("output.txt", "w")
        opcode = '001101'
        Mc_file.write('Opcode =' + str(opcode))
        Mc_file.close()
elif inst[0] == 'a' and inst[1] == 'd' and inst[2] == 'd'and inst[3] == 'i':
        Mc_file = open("output.txt", "w")
        opcode = '001000'
        Mc_file.write('Opcode =' + str(opcode))
        Mc_file.close()
else:
    print "Not supported operation!"

2 个答案:

答案 0 :(得分:0)

Python是一种解释性语言。要运行程序,您需要一个解释器。如果您使用的是Linux,则可以通过在cmd中键入python来运行解释器。

如果你想让其他人在没有解释器的情况下运行它,可以使用工具:py2exe,它将从python脚本生成可执行文件。

如果对方有python解释器,你只需要向他们发送脚本。

答案 1 :(得分:0)

像@Bartlomiej Lewandowski所说,Python是一种解释型语言,你不需要编译它就像你用C或C ++那样运行。您可以将脚本作为.py文件提交,并且您发送给它的人可以通过进行以下调用从命令行运行它:

$ python script.py

假设该文件名为script.py。请注意,$表示命令提示符。

或者,您可以将hash-bang添加到文件#! /usr/bin/env python的顶部并通过调用

运行脚本
$ ./script.py

从命令行