Skip to content

Commit 0aedec4

Browse files
committed
Add basic assembler w/o support for labels
1 parent 528a4a5 commit 0aedec4

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

tools/assembler.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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)

tools/disassembler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def get_opcode_from_bin(opcode_bin):
5454
else:
5555
if (isWithLiteral):
5656
literal = line[-16:]
57-
print opcode + "(R" + str(int(ra, 2)) + ", " + str(int(literal, 2)) + ", R" + str(int(rc, 2)) + ")"
57+
print opcode + " %R" + str(int(ra, 2)) + ", " + str(int(literal, 2)) + ", %R" + str(int(rc, 2)) + ""
5858
else:
5959
rb = line[17:22]
60-
print opcode + "(R" + str(int(ra, 2)) + ", R" + str(int(rb, 2)) + ", R" + str(int(rc, 2)) + ")"
61-
60+
print opcode + " %R" + str(int(ra, 2)) + ", %R" + str(int(rb, 2)) + ", %R" + str(int(rc, 2)) + ""

0 commit comments

Comments
 (0)