Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # check type
- def checkType(instructionCode) :
- Rtype = ['add','sub','xor','nand','or','and','sll']
- Itype = ['lw','ld','lwu','addi','xori','jalr','andi']
- Stype = ['sw','sd']
- SBtype = ['beq','bne','blt','bge','bltu','bgeu']
- if instructionCode in Rtype : #check R Type
- return "R"
- elif instructionCode in Itype : #check I Type
- return "I"
- elif instructionCode in Stype : #check S Type
- return "S"
- elif instructionCode in SBtype : #check SB Type
- return "SB"
- 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(case):
- op = ""
- if case == "add":
- op = "000"
- elif case == "nand":
- op = "001"
- return op
- # opcode I-type
- def Iopcode(case):
- op = ""
- if case == "lw":
- op = "010"
- elif case == "sw":
- op = "011"
- elif case == "beq":
- op = "100"
- return op
- #Set Path
- filename = "ComArch.txt"
- #All instruction
- instruction = list()
- #split instruction -> Fill
- with open (filename) as fin :
- for line in fin :
- instruction.append (line)
- for i in instruction :
- label = (i[0:6])
- command = (i[8:99])
- label2 = ""
- inst = command.split(" ")
- instType = checkType(inst[0])
- if instType == "Fill" :
- label2 = Fill(label,inst[1])
- #split instruction
- with open (filename) as fin :
- for line in fin :
- instruction.append (line)
- for i in instruction :
- label = (i[0:6])
- command = (i[8:99])
- label2 = ""
- inst = command.split(" ")
- instType = checkType(inst[0])
- few = ""
- x = ""
- y = ""
- z = ""
- maichi = ""
- maichi2 = ""
- if instType == "R" :
- few = Ropcode(inst[0])
- x = Dectobin(inst[1][1:])
- y = Dectobin(inst[2][1:])
- z = Dectobin(inst[3][1:])
- maichi = "0000000000000"
- maichi2 = "0000000"
- print(i+" "+x,maichi,y,z,few,maichi2)
- # if instType == "I" :
- # print ("few")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement