Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Set Path
- filename = "ComArch.txt"
- # BackgroundOperation===============================================
- # DECIMAL TO BINARY
- def Dectobin(num):
- num = int(num)
- num = bin(num)[2:].zfill(3)
- return num
- # OPCODE
- def Opcode(case):
- op = ""
- if case == "add":
- op = "000"
- elif case == "nand":
- op = "001"
- return op
- # TYPE DEFINATION
- 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 :
- return "R"
- elif instructionCode in Itype :
- return "I"
- elif instructionCode in Stype :
- return "S"
- elif instructionCode in SBtype :
- return "SB"
- elif instructionCode==".fill" :
- return "FILL"
- else :
- return "ERROR"
- # BackgroundOperation===============================================
- # Testing Zone======================================================
- class sample(object):
- def __init__(self):
- vars(self)['a'] = 10
- def check_int(s):
- if s[0] in ('-', '+'):
- return s[1:].isdigit()
- return s.isdigit()
- # Testing Zone======================================================
- # Main Program======================================================
- # All instruction
- instruction = list()
- variableOfFill = {}
- # Open Temp Text
- temp = open("temp.txt", "w")
- # 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:])
- inst = command.split(" ")
- # Check Type of Inst
- instType = checkType(inst[0])
- if instType == "FILL" :
- # label = variable , command = Value
- variableOfFill[label] = (i[14:])
- print (variableOfFill[label])
- #Create new temp file
- instruction2 = list()
- with open (filename) as fin :
- for line in fin :
- instruction2.append (line)
- for i in instruction :
- label = (i[0:6])
- command = (i[8:])
- inst = command.split(" ")
- instType = checkType(inst[0])
- if instType == "R" :
- temp.write(label + command)
- else :
- if inst[3].isdigit() :
- temp.write(label + command)
- else :
- temp.write(label + inst[0] + inst[1] + inst[2] + variableOfFill[inst[3]])
- #end Fill Variable
- # Main Program======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement