2014-01-26 1 views
0

[나는 15 밉스 명령어 세트를 어셈블 링하는 간단한 어셈블러를 가지고있다. 프로그램은 사용자로부터 명령어를 가져 와서 해당 머신 코드를 출력하여 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

테이블을 사용하는 법을 배워야합니다. 지시 사항을 표에 넣고 반복하여 일치하는 것을 찾으십시오. 코드에서 반복을 줄입니다. – stark

+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 

할 수 있습니다 중 하나 open이 루프를 한 번에 write 라인, 또는 ("추가") - 다시 열 'a'에서 루프를 통해 파일을 매번 모드 전에 파일.

+0

제가 말씀 드린 것을했으나 32 비트 라인에서만 마지막 입력 명령을 어셈블합니다. –

+0

네가 'w'(" (over) write ") 모드로 전환됩니다. 내 대답을 모두 읽으십시오. – jonrsharpe

+0

3 개의 명령을 입력하면 3 줄의 기계어 코드가 같은 줄에 정렬됩니다. 각 명령을 한 줄에 인쇄해야합니다. –

관련 문제