-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.py
51 lines (47 loc) · 1.81 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Pledge: I pledge my honor that I have abided by the Stevens Honor System.
#Name: Haig Emirzian
def assembler():
#File being converted to an image file
file = 'simpleInstructions.s'
#Reads from file
with open(file, 'r') as file:
read = file.readline()
result = []
convert = 0
#Ends reading at .end
while(read != '' and read != 'FIN'):
inst = read[0:3]
if(inst == 'LDR' or inst == 'ldr' or inst == 'Ldr'):
convert = (int(read[4:6])) << 2
convert += (int(read[8]))
#Adds a 0 to the hex conversion if it's just one nibble and makes it two nibbles
if len(hex(convert)[2:4]) == 1:
result.append('0' + hex(convert)[2:4])
else:
result.append(hex(convert)[2:4])
#Add operation
if (inst == 'ADD' or inst == 'add' or inst == 'Add'):
convert = 64
convert += (int(read[5]) << 4)
convert += (int(read[8]) << 2)
convert += (int(read[11]))
result.append(hex(convert)[2:4])
#Sub operation
if (inst == 'SUB' or inst == 'sub' or inst == 'Sub'):
convert = 128
convert += (int(read[5]) << 4)
convert += (int(read[8]) << 2)
convert += (int(read[11]))
result.append(hex(convert)[2:4])
read = file.readline()
file.close()
name = 'result.txt'
#Writes to file
with open(name, 'w') as file:
hexs = 0
#Adds space between hex values
while(hexs != len(result)):
file.write(result[hexs] + ' ')
hexs += 1
file.close()
assembler()