Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # check type
- def checkType(instructionCode) :
- Rtype = ["add","nand"]
- Itype = ["lw","sw","beq",]
- Jtype = ["jalr"]
- Otype = ["halt","noop"]
- if instructionCode in Rtype : #check R Type
- return "R"
- elif instructionCode in Itype : #check I Type
- return "I"
- elif instructionCode in Jtype : #check J Type
- return "J"
- elif instructionCode in Otype : #check O Type
- return "O"
- elif instructionCode==".fill" : #check Fill
- return "FILL"
- else : #else
- return "ERROR"
- # convert decimal to binary
- def Dectobin(num):
- num = int(num)
- num = bin(num)[2:].zfill(3)
- return num
- # Fill
- def Fill(label,inst1):
- # inst1 = int(inst1)
- label = inst1
- return label
- # opcode R-type
- def Ropcode(type):
- op = ""
- if type == "add":
- op = "000"
- elif type == "nand":
- op = "001"
- return op
- # opcode I-type
- def Iopcode(type):
- op = ""
- if type == "lw":
- op = "010"
- elif type == "sw":
- op = "011"
- elif type == "beq":
- op = "100"
- return op
- #Set Path
- filename = "ComArch.txt"
- #All instruction
- instruction = list()
- variableOfFill = list()
- k = 0
- #split instruction
- with open (filename) as fin :
- for line in fin :
- instruction.append (line)
- for i in instruction : # วนลูปรันคำสั่งทุกคำสั่ง โดยจะรับไปทีละบรรทัด
- # label = (i[0:6]) # สร้างตัวแปลชื่อ label เก็บ 6 ตัวแรกในบรรทัดนั้นๆ
- # command = (i[8:99]) # สร้างตัวแปล command เก็บที่เหลือทัดจาก 6 ตัวแรก
- # label2 = ""
- inst = i.split("\t") # นำ ตัวแปลcommad มาแบ่งโดยใช้ช่องว่าง ถ้าเจอช่องว่างก็แบ่ง ไปเก็บในตัวแปล inst ที่เป็น array
- instType = checkType(inst[1]) # เอาไปเข้า ฟังชั่น chrcktype อยู่ข้างบน ฟังชั่นนี้จะ return ออกมาเพื่อนแบ่ง type R I S บลาๆ
- if instType == "FILL" : #ยังไม่เสร็จ R type ไม่ได้ใช้
- variableOfFill.append(inst[0])
- variableOfFill.append(inst[2])
- for i in instruction :
- # label = (i[0:6]) # เหมือนข้างบน
- # command = (i[8:99])
- # label2 = ""
- inst = i.split("\t")
- instType = checkType(inst[1])
- # print(inst[1])
- if instType == "R" : # ถ้าเจอคำสั่ง r type ฟังชั่น instType จะ return ตัว "R" ออกมา นำมาเช็คว่าเป็น R type รึป่าว
- few = Ropcode(inst[1]) # สร้างตัวแปล เรียนใช้ฟังชัน Ropcode อยู่ข้างบน โดยจะส่ง inst[0](ตัวนี้จะเป็นตัวบอกคำสั่งเช่น add,nand,addi,lw) เอาไปเปลี่ยนเป็น opcode เช่น add => 000
- x = Dectobin(inst[2][1:]) # สร้างตัวแปลเรียกใช้ฟังชัน Dectobin อยู่ข้างบน โดนส่ง inst[1](x1 xต่างๆไป) เอาไปเปลี่ยนจาก x1 เป็น 1ในเลขฐาน2 001
- y = Dectobin(inst[3][1:])
- z = Dectobin(inst[4][1:])
- maichi = "0000000000000"
- maichi2 = "0000000"
- print(i+"\t"+x+maichi,y,z,few,maichi2) # เอาตัวแปลมาต่อกันออกเป็นเลขฐาน2 32 bit
- if instType == "I" : # เรียกใช้ I type ยังไม่เสร็จจ้าา
- few = Iopcode(inst[1])
- x = Dectobin(inst[2][1:])
- y = Dectobin(inst[3][1:])
- z = (inst[4][:-1])
- # # print(variableOfFill[2])
- # # print (variableOfFill)
- k = 0
- # print (variableOfFill[2])
- while k < len(variableOfFill) :
- if z == variableOfFill[k] :
- z = variableOfFill[k+1][:-1]
- k = k + 2
- else :
- k = k + 2
- c = Dectobin(z)
- print (i+"\t"+few,x,y,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement