[我implmented簡單的彙編程序,組裝一套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!"
你應該學會使用表格。把說明放在一張表中並循環查找匹配。它減少了代碼中的重複。 – stark
是的,謝謝你的這個問題我會盡量記住下一次 –