|
| 1 | +import re |
| 2 | +import sys |
| 3 | + |
| 4 | +""" Define enum """ |
| 5 | +def enum(**enums): |
| 6 | + return type('Enum', (), enums) |
| 7 | + |
| 8 | +# Defines error_codes |
| 9 | +Errors = enum(ILLOP = 0, ILLREG=1) |
| 10 | + |
| 11 | +""" Compiles op codes into single dimensional array """ |
| 12 | +def compile_opcodes(): |
| 13 | + # Define opcodes |
| 14 | + opcodes = \ |
| 15 | + """. . . . . . . . |
| 16 | + . . . . . . . . |
| 17 | + . . . . . . . . |
| 18 | + LD ST . JMP . BEQ BNE LDR |
| 19 | + ADD SUB MUL DIV CMPEQ CMPLT CMPL . |
| 20 | + AND OR XOR . SHL SHR SRA . |
| 21 | + ADDC SUBC MULC DIVC CMPEQC CMPLTC CMPLEC . |
| 22 | + ANDC ORC XORC . SHLC SHRC SRAC . |
| 23 | + """ |
| 24 | + |
| 25 | + rr = re.compile(r"[^\s]+", re.VERBOSE) |
| 26 | + |
| 27 | + # Read opcodes to single dimensional array |
| 28 | + return rr.findall(opcodes) |
| 29 | + |
| 30 | +""" |
| 31 | + Removes all comments from program |
| 32 | +""" |
| 33 | +def strip_comments(prog_content): |
| 34 | + # temp = re.sub(r'[\n]+', '', prog_content) |
| 35 | + return re.sub(r';.*', '', prog_content) |
| 36 | + |
| 37 | +""" Returns address for label """ |
| 38 | +def get_literal(label): |
| 39 | + return '000000000000000' |
| 40 | + |
| 41 | +""" |
| 42 | + Converts one instruction into corresponding |
| 43 | + machine language instruction |
| 44 | +""" |
| 45 | +def convert_inst(instr, opcodes): |
| 46 | + keyword_arr = re.split(r', |,| ', instr) |
| 47 | + result ='{0:06b}'.format(opcodes.index(keyword_arr[0].upper())) |
| 48 | + |
| 49 | + reg_used = instr.count('%') |
| 50 | + |
| 51 | + if (len(keyword_arr) == 3): # JMP / LDR |
| 52 | + if (reg_used == 2): |
| 53 | + result += '{0:05b}'.format((keyword_arr[2])[2:]) # Rc |
| 54 | + result += '{0:05b}'.format((keyword_arr[1])[2:]) # Ra |
| 55 | + result += '0000000000000000' |
| 56 | + else: |
| 57 | + result += '{0:05b}'.format((keyword_arr[2])[2:]) # Rc |
| 58 | + result += '11111' |
| 59 | + result += get_literal(keyword_arr[1]) |
| 60 | + elif (len(keyword_arr) == 4): |
| 61 | + if (reg_used == 2): # R1 literal R2 |
| 62 | + if (keyword_arr[0] == 'ST'): #ST |
| 63 | + result += '{0:05b}'.format(int((keyword_arr[1])[2:])) # Rc |
| 64 | + result += '{0:05b}'.format(int((keyword_arr[3])[2:])) # Ra |
| 65 | + result += '{0:016b}'.format(int(keyword_arr[2])) # literal |
| 66 | + elif (keyword_arr[0] == 'BEQ' | keyword_arr[0] == 'BNE'): |
| 67 | + result += '{0:05b}'.format(int((keyword_arr[1])[2:])) # Rc |
| 68 | + result += '{0:05b}'.format(int((keyword_arr[3])[2:])) # Ra |
| 69 | + result += get_literal(keyword_arr[2]) # literal |
| 70 | + else: |
| 71 | + result += '{0:05b}'.format(int((keyword_arr[3])[2:])) # Rc |
| 72 | + result += '{0:05b}'.format(int((keyword_arr[1])[2:])) # Ra |
| 73 | + result += '{0:016b}'.format(int(keyword_arr[2])) # literal |
| 74 | + else: |
| 75 | + result += '{0:05b}'.format(int((keyword_arr[3])[2:])) # Rc |
| 76 | + result += '{0:05b}'.format(int((keyword_arr[1])[2:])) # Ra |
| 77 | + result += '{0:05b}'.format(int((keyword_arr[2])[2:])) # Ra |
| 78 | + result += '00000000000' |
| 79 | + return result |
| 80 | +""" |
| 81 | + Error handling |
| 82 | +""" |
| 83 | +def handle_error(error_code, instruction): |
| 84 | + if (error_code == Errors.ILLOP): |
| 85 | + print "Illegal op code : " + str(instruction) |
| 86 | + elif (error_code == Errors.ILLREG): |
| 87 | + print "Illegal reg index : " + str(instruction) |
| 88 | + exit() |
| 89 | + |
| 90 | +""" |
| 91 | + Entry point of script |
| 92 | +""" |
| 93 | +def main(file_name): |
| 94 | + # Load content of file and strip all comments |
| 95 | + program_content = open(file_name, 'r').read() |
| 96 | + program_content = strip_comments(program_content) |
| 97 | + # Compile opcodes to single dimension array |
| 98 | + opcodes = compile_opcodes() |
| 99 | + |
| 100 | + for instr in re.split(r'\n', program_content): |
| 101 | + print convert_inst(instr, opcodes) |
| 102 | + |
| 103 | +# Start here |
| 104 | +for arg in sys.argv[1:]: |
| 105 | + main(arg) |
0 commit comments