Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. # check type
  2. def checkType(instructionCode) :
  3. Rtype = ["add","nand"]
  4. Itype = ["lw","sw","beq",]
  5. Jtype = ["jalr"]
  6. Otype = ["halt","noop"]
  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 Jtype : #check J Type
  13. return "J"
  14. elif instructionCode in Otype : #check O Type
  15. return "O"
  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.  
  31. label = inst1
  32. return label
  33.  
  34. # opcode R-type
  35. def Ropcode(type):
  36. op = ""
  37. if type == "add":
  38. op = "000"
  39. elif type == "nand":
  40. op = "001"
  41. return op
  42.  
  43. # opcode I-type
  44. def Iopcode(type):
  45. op = ""
  46. if type == "lw":
  47. op = "010"
  48. elif type == "sw":
  49. op = "011"
  50. elif type == "beq":
  51. op = "100"
  52. return op
  53.  
  54. #Set Path
  55. filename = "ComArch.txt"
  56.  
  57. #All instruction
  58. instruction = list()
  59. variableOfFill = list()
  60. k = 0
  61.  
  62.  
  63. #split instruction
  64. with open (filename) as fin :
  65. for line in fin :
  66. instruction.append (line)
  67.  
  68.  
  69. for i in instruction : # วนลูปรันคำสั่งทุกคำสั่ง โดยจะรับไปทีละบรรทัด
  70. # label = (i[0:6]) # สร้างตัวแปลชื่อ label เก็บ 6 ตัวแรกในบรรทัดนั้นๆ
  71. # command = (i[8:99]) # สร้างตัวแปล command เก็บที่เหลือทัดจาก 6 ตัวแรก
  72.  
  73. # label2 = ""
  74. inst = i.split("\t") # นำ ตัวแปลcommad มาแบ่งโดยใช้ช่องว่าง ถ้าเจอช่องว่างก็แบ่ง ไปเก็บในตัวแปล inst ที่เป็น array
  75.  
  76. instType = checkType(inst[1]) # เอาไปเข้า ฟังชั่น chrcktype อยู่ข้างบน ฟังชั่นนี้จะ return ออกมาเพื่อนแบ่ง type R I S บลาๆ
  77.  
  78. if instType == "FILL" : #ยังไม่เสร็จ R type ไม่ได้ใช้
  79. variableOfFill.append(inst[0])
  80. variableOfFill.append(inst[2])
  81.  
  82.  
  83. for i in instruction :
  84. # label = (i[0:6]) # เหมือนข้างบน
  85. # command = (i[8:99])
  86.  
  87. # label2 = ""
  88. inst = i.split("\t")
  89. instType = checkType(inst[1])
  90. # print(inst[1])
  91. if instType == "R" : # ถ้าเจอคำสั่ง r type ฟังชั่น instType จะ return ตัว "R" ออกมา นำมาเช็คว่าเป็น R type รึป่าว
  92. few = Ropcode(inst[1]) # สร้างตัวแปล เรียนใช้ฟังชัน Ropcode อยู่ข้างบน โดยจะส่ง inst[0](ตัวนี้จะเป็นตัวบอกคำสั่งเช่น add,nand,addi,lw) เอาไปเปลี่ยนเป็น opcode เช่น add => 000
  93. x = Dectobin(inst[2][1:]) # สร้างตัวแปลเรียกใช้ฟังชัน Dectobin อยู่ข้างบน โดนส่ง inst[1](x1 xต่างๆไป) เอาไปเปลี่ยนจาก x1 เป็น 1ในเลขฐาน2 001
  94. y = Dectobin(inst[3][1:])
  95. z = Dectobin(inst[4][1:])
  96. maichi = "0000000000000"
  97. maichi2 = "0000000"
  98.  
  99.  
  100. print(i+"\t"+x+maichi,y,z,few,maichi2) # เอาตัวแปลมาต่อกันออกเป็นเลขฐาน2 32 bit
  101.  
  102. if instType == "I" : # เรียกใช้ I type ยังไม่เสร็จจ้าา
  103. few = Iopcode(inst[1])
  104. x = Dectobin(inst[2][1:])
  105. y = Dectobin(inst[3][1:])
  106. z = (inst[4][:-1])
  107.  
  108. # # print(variableOfFill[2])
  109. # # print (variableOfFill)
  110. k = 0
  111. # print (variableOfFill[2])
  112. while k < len(variableOfFill) :
  113. if z == variableOfFill[k] :
  114. z = variableOfFill[k+1][:-1]
  115. k = k + 2
  116. else :
  117. k = k + 2
  118.  
  119. c = Dectobin(z)
  120. print (i+"\t"+few,x,y,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement