Advertisement
Guest User

Assembler

a guest
Nov 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. print ("\n\n\n==================================================ENCODING==================================================")
  2.  
  3. # Temp Create for Complier
  4. temp = open("temp.txt", "w")
  5.  
  6. # check type
  7. def checkType(instructionCode) :
  8. Rtype = ["add","nand"]
  9. Itype = ["lw","sw","beq"]
  10. Jtype = ["jalr"]
  11. Otype = ["halt","noop"]
  12.  
  13. if instructionCode in Rtype : #check R Type
  14. return "R"
  15. elif instructionCode in Itype : #check I Type
  16. return "I"
  17. elif instructionCode in Jtype : #check J Type
  18. return "J"
  19. elif instructionCode in Otype : #check O Type
  20. return "O"
  21. elif instructionCode==".fill" : #check Fill
  22. return "FILL"
  23. else : #else
  24. return "ERROR"
  25.  
  26. # convert decimal to binary
  27. def Dectobin(n, bits):
  28. n = int(n)
  29. s = bin(n & int("1"*bits, 2))[2:]
  30. return ("{0:0>%s}" % (bits)).format(s)
  31.  
  32. # Fill
  33. def Fill(label,inst1):
  34. # inst1 = int(inst1)
  35.  
  36. label = inst1
  37. return label
  38.  
  39. # opcode R-type
  40. def Ropcode(type):
  41. op = ""
  42. if type == "add":
  43. op = "000"
  44. elif type == "nand":
  45. op = "001"
  46. return op
  47.  
  48. # opcode J-type
  49. def Jopcode(type):
  50. op = ""
  51. if type == "jalr":
  52. op = "101"
  53. return op
  54.  
  55. # opcode O-type
  56. def Oopcode(type):
  57. op = ""
  58. if type == "halt":
  59. op = "110"
  60. elif type == "noop":
  61. op = "111"
  62. return op
  63.  
  64. # opcode I-type
  65. def Iopcode(type):
  66. op = ""
  67. if type == "lw":
  68. op = "010"
  69. elif type == "sw":
  70. op = "011"
  71. elif type == "beq":
  72. op = "100"
  73. return op
  74.  
  75. #Set Path
  76. filename = "ComArch.txt"
  77.  
  78. #All instruction
  79. instruction = list()
  80. variableOfFill = list()
  81. adrlabel = list()
  82. countadr = 0
  83. myadr = 0
  84. k = 0
  85.  
  86. #split instruction
  87. with open (filename) as fin :
  88. for line in fin :
  89. instruction.append (line)
  90.  
  91.  
  92. for i in instruction : # วนลูปรันคำสั่งทุกคำสั่ง โดยจะรับไปทีละบรรทัด
  93. inst = i.split("\t") # นำ ตัวแปลcommad มาแบ่งโดยใช้ช่องว่าง ถ้าเจอช่องว่างก็แบ่ง ไปเก็บในตัวแปล inst ที่เป็น array
  94. if len(inst[0]) > 6 :
  95. print ("ERROR : label > 6")
  96. break
  97.  
  98. instType = checkType(inst[1]) # เอาไปเข้า ฟังชั่น chrcktype อยู่ข้างบน ฟังชั่นนี้จะ return ออกมาเพื่อนแบ่ง type R I S บลาๆ
  99.  
  100. adrlabel.append(inst[0])
  101. adrlabel.append(countadr)
  102. countadr = countadr + 1
  103. # if inst[0] != None:
  104. # print ("few")
  105. if instType == "FILL" :
  106. k = 0
  107. z = inst[2][:-1]
  108.  
  109. while k < len(adrlabel) :
  110. if z == adrlabel[k] :
  111. z = adrlabel[k+1]
  112. z = str(z)
  113. k = k + 2
  114. else :
  115. k = k + 2
  116. variableOfFill.append(inst[0])
  117. variableOfFill.append(z)
  118.  
  119. # print(adrlabel)
  120. # print(variableOfFill)
  121. for i in instruction :
  122. inst = i.split("\t")
  123. instType = checkType(inst[1])
  124.  
  125. if len(inst[0]) > 6 :
  126. print ("ERROR : label > 6")
  127. break
  128. if instType == "R" : # ถ้าเจอคำสั่ง r type ฟังชั่น instType จะ return ตัว "R" ออกมา นำมาเช็คว่าเป็น R type รึป่าว
  129. few = Ropcode(inst[1]) # สร้างตัวแปล เรียนใช้ฟังชัน Ropcode อยู่ข้างบน โดยจะส่ง inst[0](ตัวนี้จะเป็นตัวบอกคำสั่งเช่น add,nand,addi,lw) เอาไปเปลี่ยนเป็น opcode เช่น add => 000
  130. x = Dectobin(inst[2],3) # สร้างตัวแปลเรียกใช้ฟังชัน Dectobin อยู่ข้างบน โดนส่ง inst[1](x1 xต่างๆไป) เอาไปเปลี่ยนจาก x1 เป็น 1ในเลขฐาน2 001
  131. y = Dectobin(inst[3],3)
  132. z = Dectobin(inst[4],3)
  133. maichi = "0000000000000"
  134. maichi2 = "0000000"
  135. temp.write(maichi2+few+x+y+maichi+z+"\n") # เอาตัวแปลมาต่อกันออกเป็นเลขฐาน2 32 bit
  136. print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
  137.  
  138.  
  139. if instType == "I" : # เรียกใช้ I type ยังไม่เสร็จจ้าา
  140. few = Iopcode(inst[1])
  141. x = Dectobin(inst[2],3)
  142. y = Dectobin(inst[3],3)
  143. z = (inst[4][:-1])
  144. w = (inst[4][1:-1])
  145. check = 0
  146.  
  147. maichi2 = "0000000"
  148. k = 0
  149. while k < len(adrlabel) :
  150. if z == adrlabel[k] :
  151. if few == "100" :
  152. z = int(adrlabel[k+1]) - myadr
  153. z = z -1
  154. z = str(z)
  155. else :
  156. z = int(adrlabel[k+1]) - myadr
  157. z = str(z)
  158. k = k + 2
  159. elif z != adrlabel[k] :
  160.  
  161. k = k + 2
  162. c = Dectobin(z,16)
  163. temp.write(maichi2+few+x+y+c+"\n")
  164. print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
  165.  
  166. if instType == "J" :
  167. few = Jopcode(inst[1])
  168. x = Dectobin(inst[2],3)
  169. y = Dectobin(inst[3],3)
  170. maichi = "0000000000000000"
  171. maichi2 = "0000000"
  172. temp.write(maichi2+few+x+y+maichi+"\n")
  173. print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
  174.  
  175. if instType == "O" :
  176. few = Oopcode(inst[1])
  177. maichi = "0000000000000000000000"
  178. maichi2 = "0000000"
  179. temp.write(maichi2+few+maichi+"\n") # เอาตัวแปลมาต่อกันออกเป็นเลขฐาน2 32 bit
  180. print ("Finsish ", myadr,"instruction(s) : ",instType,"Type")
  181.  
  182. if instType == "FILL" :
  183. x = (inst[2])[:-1]
  184. k = 0
  185.  
  186. while k < len(adrlabel) :
  187. if x == adrlabel[k] :
  188. x = adrlabel[k+1]
  189. k = k+2
  190. else :
  191. k = k+2
  192. if int(x) > 0 :
  193. x = Dectobin(x,3)
  194. temp.write(str(int(x,2))+"\n")
  195. print ("Finsish ", myadr,"Instruction(s) : FILL")
  196. else :
  197. temp.write(x +"\n")
  198. print ("Finsish ", myadr,"Instruction(s) : FILL")
  199.  
  200. myadr = myadr +1
  201. temp.close
  202. print ("===============================================FINISH ENCODING===============================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement