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.67 KB | None | 0 0
  1. # Set Path
  2. filename = "ComArch.txt"
  3.  
  4. # BackgroundOperation===============================================
  5.  
  6. # DECIMAL TO BINARY
  7. def Dectobin(num):
  8. num = int(num)
  9. num = bin(num)[2:].zfill(3)
  10. return num
  11.  
  12. # OPCODE
  13. def Opcode(case):
  14. op = ""
  15. if case == "add":
  16. op = "000"
  17. elif case == "nand":
  18. op = "001"
  19. return op
  20.  
  21. # TYPE DEFINATION
  22. def checkType(instructionCode) :
  23. Rtype = ['add','sub','xor','nand','or','and','sll']
  24. Itype = ['lw','ld','lwu','addi','xori','jalr','andi']
  25. Stype = ['sw','sd']
  26. SBtype = ['beq','bne','blt','bge','bltu','bgeu']
  27.  
  28. if instructionCode in Rtype :
  29. return "R"
  30. elif instructionCode in Itype :
  31. return "I"
  32. elif instructionCode in Stype :
  33. return "S"
  34. elif instructionCode in SBtype :
  35. return "SB"
  36. elif instructionCode==".fill" :
  37. return "FILL"
  38. else :
  39. return "ERROR"
  40.  
  41. # BackgroundOperation===============================================
  42.  
  43. # Testing Zone======================================================
  44.  
  45. class sample(object):
  46. def __init__(self):
  47. vars(self)['a'] = 10
  48.  
  49. def check_int(s):
  50. if s[0] in ('-', '+'):
  51. return s[1:].isdigit()
  52. return s.isdigit()
  53.  
  54.  
  55. # Testing Zone======================================================
  56.  
  57. # Main Program======================================================
  58.  
  59. # All instruction
  60. instruction = list()
  61. variableOfFill = {}
  62.  
  63. # Open Temp Text
  64. temp = open("temp.txt", "w")
  65.  
  66. # Split instruction
  67. with open (filename) as fin :
  68. for line in fin :
  69. instruction.append (line)
  70.  
  71. for i in instruction :
  72. label = (i[0:6])
  73. command = (i[8:])
  74. inst = command.split(" ")
  75.  
  76. # Check Type of Inst
  77. instType = checkType(inst[0])
  78.  
  79. if instType == "FILL" :
  80. # label = variable , command = Value
  81. variableOfFill[label] = (i[14:])
  82. print (variableOfFill[label])
  83.  
  84. #Create new temp file
  85. instruction2 = list()
  86. with open (filename) as fin :
  87. for line in fin :
  88. instruction2.append (line)
  89.  
  90. for i in instruction :
  91. label = (i[0:6])
  92. command = (i[8:])
  93. inst = command.split(" ")
  94.  
  95. instType = checkType(inst[0])
  96.  
  97. if instType == "R" :
  98. temp.write(label + command)
  99. else :
  100. if inst[3].isdigit() :
  101. temp.write(label + command)
  102. else :
  103. temp.write(label + inst[0] + inst[1] + inst[2] + variableOfFill[inst[3]])
  104.  
  105.  
  106.  
  107.  
  108. #end Fill Variable
  109.  
  110. # Main Program======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement