Created
April 30, 2018 13:16
-
-
Save sencagri/1be89e1c6c9b47cf59b6f6b72f881849 to your computer and use it in GitHub Desktop.
Revisions
-
sencagri created this gist
Apr 30, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ import re # program counter pc = 100 # jump list colonList = list() # param list param = list() # result list output = list() # instruction set for ARM V8 processor instructionSet = [["ADDI", "00001110001mmmmm100001nnnnnddddd", 3], ["SUB","00101110001mmmmm100001nnnnnddddd", 3]] # delimiter characters used for splitting line delimiter = ", | " def incrementPC(): global pc pc = pc + 4 def getInputFile(isAsm2Bin): if isAsm2Bin: program = "sample_program.txt" else: program = "sample_program2.txt" file = open(program, "r") lines = file.readlines() file.close() if len(lines) == 0: print("No input data!") exit() return lines def decodeParameters(stringPart): for par in stringPart[1:]: # register value if str.startswith(par, "R") or str.startswith(par, "#"): paramDigit = par[1:]; if len(paramDigit) > 0: param.append("{0:05b}".format(int(paramDigit))) def processLine_asm2bin(line): endPos = len(line) if endPos > 0: # check if comment added commentIndex = str.find(line, ";") if commentIndex > -1: endPos = commentIndex # copy only to the semicolon pos stringPart = line[:endPos] if len(stringPart) > 0: # check if line is jump point, if so add colonName to list then exit colonIndex = str.find(stringPart, ":") if colonIndex > 0: colonName = stringPart[:colonIndex] colonList.append([colonName, pc]) return # split by delimiter character stringPart = re.split(delimiter, stringPart) stringPart = list(filter(None, stringPart)) # search first string in instruction list candidateInst = stringPart[0] candidateRes = [item for item in instructionSet if item[0] == candidateInst] # we found the instruction if len(candidateRes) > 0: insBinary = candidateRes[0][1]; paramSize = candidateRes[0][2]; if paramSize == 3: decodeParameters(stringPart) insBinary = str.replace(insBinary, "mmmmm", param[0]) insBinary = str.replace(insBinary, "nnnnn", param[1]) insBinary = str.replace(insBinary, "ddddd", param[2]) param.clear() output.append(insBinary) def dec_asm2bin(inData): for line in inData: processLine_asm2bin(line) def main(): isAsm2Bin = True inData = getInputFile(isAsm2Bin) # clean empty lines inData = list(filter(None, inData)) if isAsm2Bin: dec_asm2bin(inData) else: dec_bin2asm(inData) if __name__ == "__main__": main()