Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. # check type
  2. def checkType(instructionCode) :
  3. Rtype = ['add','sub','xor','nand','or','and','sll']
  4. Itype = ['lw','ld','lwu','addi','xori','jalr','andi']
  5. Stype = ['sw','sd']
  6. SBtype = ['beq','bne','blt','bge','bltu','bgeu']
  7.  
  8. if instructionCode in Rtype : #check R Type
  9. return "R"
  10. elif instructionCode in Itype : #check I Type
  11. return "I"
  12. elif instructionCode in Stype : #check S Type
  13. return "S"
  14. elif instructionCode in SBtype : #check SB Type
  15. return "SB"
  16. elif instructionCode==".fill" : #check Fill
  17. return "FILL"
  18. else : #else
  19. return "ERROR"
  20.  
  21. # convert decimal to binary
  22. def Dectobin(num):
  23. num = int(num)
  24. num = bin(num)[2:].zfill(3)
  25. return num
  26.  
  27. # Fill
  28. def Fill(label,inst1):
  29. # inst1 = int(inst1)
  30. label = inst1
  31. return label
  32.  
  33. # opcode R-type
  34. def Ropcode(case):
  35. op = ""
  36. if case == "add":
  37. op = "000"
  38. elif case == "nand":
  39. op = "001"
  40. return op
  41.  
  42. # opcode I-type
  43. def Iopcode(case):
  44. op = ""
  45. if case == "lw":
  46. op = "010"
  47. elif case == "sw":
  48. op = "011"
  49. elif case == "beq":
  50. op = "100"
  51. return op
  52.  
  53. #Set Path
  54. filename = "ComArch.txt"
  55.  
  56. #All instruction
  57. instruction = list()
  58.  
  59. #split instruction -> Fill
  60. with open (filename) as fin :
  61. for line in fin :
  62. instruction.append (line)
  63.  
  64.  
  65. for i in instruction :
  66. label = (i[0:6])
  67. command = (i[8:99])
  68.  
  69. label2 = ""
  70. inst = command.split(" ")
  71.  
  72. instType = checkType(inst[0])
  73.  
  74. if instType == "Fill" :
  75. label2 = Fill(label,inst[1])
  76.  
  77.  
  78. #split instruction
  79. with open (filename) as fin :
  80. for line in fin :
  81. instruction.append (line)
  82.  
  83.  
  84. for i in instruction :
  85. label = (i[0:6])
  86. command = (i[8:99])
  87.  
  88. label2 = ""
  89. inst = command.split(" ")
  90.  
  91. instType = checkType(inst[0])
  92. few = ""
  93. x = ""
  94. y = ""
  95. z = ""
  96. maichi = ""
  97. maichi2 = ""
  98. if instType == "R" :
  99. few = Ropcode(inst[0])
  100. x = Dectobin(inst[1][1:])
  101. y = Dectobin(inst[2][1:])
  102. z = Dectobin(inst[3][1:])
  103. maichi = "0000000000000"
  104. maichi2 = "0000000"
  105.  
  106.  
  107. print(i+" "+x,maichi,y,z,few,maichi2)
  108.  
  109. # if instType == "I" :
  110. # print ("few")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement