-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.py
36 lines (27 loc) · 1.02 KB
/
assembler.py
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
import instructions
class InvalidCommandError(Exception):
Message = "Invalid command error. assembly stoped"
Code = 1
def assemble_line(mnemonic):
components = mnemonic.split(" ")
instruction = instructions.get_instruction_by_mnemonic(components[0])
try:
components[0] = (instructions.enter_key + "/").join(instruction.opcodes)
except AttributeError:
raise InvalidCommandError
opcode = instructions.enter_key.join(components)
if len(instruction.opcodes) > 1:
opcode = opcode.replace(instruction.opcodes[-1] + instructions.enter_key, instruction.opcodes[-1] + instructions.enter_key + " ")
opcode = opcode + instructions.enter_key
return opcode
def assemble_all(mnemonics):
mnemonics = mnemonics.split("\n")
opcodes = []
for mnemonic in mnemonics:
opcodes.append(assemble_line(mnemonic))
return "\n".join(opcodes)
if __name__ == "__main__":
import sys
for line in sys.stdin:
line = line.rstrip("\n")
print(assemble_line(line))