Advertisement
Guest User

Assumbler

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