【发布时间】:2014-01-25 21:33:07
【问题描述】:
我已经实现了简单的 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!"
【问题讨论】:
-
当您说“在 IDE 之外使用程序”时,您的意思是您在 IDE 中开发它并在那里测试它,并且您试图弄清楚如何在 IDE 之外运行它——例如从命令行?
-
不,如果我不应该发送代码,这只是一个任务,我只想将文件发送给某人以使用汇编程序,或者我必须发送代码并且好友编译它&测试一下?
标签: python file assembly output mips