-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheas.py
71 lines (59 loc) · 2.03 KB
/
eas.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
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
#eater assembler
import sys
def assemble(inFilename, outFilename):
#instruction set
instructions = {
"NOP" : 0x0,
"LDA" : 0x1,
"ADD" : 0x2,
"SUB" : 0x3,
"STA" : 0x4,
"LDI" : 0x5,
"JMP" : 0x6,
"JC" : 0x7,
"JZ" : 0x8,
"OUT" : 0xE,
"HLT" : 0xF
}
#open the asm file
try:
f = open(inFilename, "r")
except:
print(f"Error: {inFilename} file not found.")
#copy the whole file into a buffer and close the file
buffer = f.read()
f.close()
#split the buffer based on new lines, we will have a list of instructions
tokens = buffer.split("\n")
#output buffer
output = []
#iterate through the tokens, convert them to hexadecimal
#values based on instruction set and append it to output
for i in range(16):
try:
ins = tokens[i].split(" ")
if(ins[0] in instructions):
if(len(ins)==1):
output.append(hex(instructions[ins[0]]<<4 | 0 ))
#print(hex(instructions[ins[0]]<<4 | 0 ))
else:
output.append(hex(instructions[ins[0]]<<4 | int(ins[1])))
#print(hex(instructions[ins[0]]<<4 | int(ins[1])))
else:
if(len(ins)==1):
output.append(hex(int(ins[0])))
#print(hex(int(ins[0])))
except Exception as e:
output.append(hex(0))
#write the output buffer to a bin file by converting it into to bytes
with open(outFilename, "wb") as f:
for i in output:
print(i)
f.write(bytes((int(i,16),)))
f.close()
if(len(sys.argv) != 4):
print("Usage: python3 eas.py <asm filename> -o <bin filename>")
exit()
inFilename = sys.argv[1]
outFilename = sys.argv[3]
assemble(inFilename, outFilename)