-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmc-assembler.py
More file actions
executable file
·77 lines (65 loc) · 2.14 KB
/
lmc-assembler.py
File metadata and controls
executable file
·77 lines (65 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
import os
import sys
instructions = {'ADD': 100,
'SUB': 200,
'STA': 300,
'LDA': 500,
'BRA': 600,
'BRZ': 700,
'BRP': 800,
'INP': 901,
'OUT': 902,
'HALT': 000,
'DAT': 000 }
def main(argv):
if len(argv) is not 2:
print 'Specify a file to assemble.\nusage: LMC-assembler input.asm'
sys.exit()
tokens = pythonize(argv[1])
symbols = get_symbols(tokens)
print assemble(tokens, symbols)
def pythonize(infile):
"""Read the input file and produce easy to parse data structures.
Returns a list of lists. The sublists contain each token in a given line
from the input. The outer list is a list of the lines. Empty lines are
included as empty lists.
"""
lines = []
try:
with open(infile, 'r') as asm:
for line in asm:
lines.append(line.split())
except EnvironmentError:
print 'Specify a valid file as input\nusage: LMC-assembler input.asm'
sys.exit()
return lines
def get_symbols(token_list):
"""Find all of the symbols used in the assembly code.
Returns a dictionary. Keys are symbol names, values are mailbox numbers.
"""
symbols = []
for i, line in zip(range(len(token_list)), token_list):
# ignore blank lines
if not line:
break
if line[0] not in instructions.keys():
symbols.append((line[0], i))
return dict(symbols)
def assemble(token_list, symbols):
program = ''
for line in token_list:
instruction = 0
# ignore blank lines
if not line:
break
for i, token in zip(range(len(line)), line):
if token in instructions.keys():
instruction += instructions[token]
elif token in symbols.keys() and i != 0:
instruction += symbols[token]
elif token.isdigit():
instruction += int(token)
program += str('%03d'%instruction)+'\n'
return program
main(sys.argv)