forked from saradaria/Calculator_de_buzunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler1.py
154 lines (137 loc) · 3.79 KB
/
assembler1.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#run the program with command python assembler1.py file.asm in the terminal and get the results from the text file rom.txt
import sys
import re
labels = {}
def parselabels(fn):
linenum = 0
with open(sys.argv[1]) as f:
for line in f:
line = line.replace('\n', '').replace('\r', '')
if line[0]=='#':
# Note that linenum won't be increased, so the address
# remains correct
continue
if line[0]=='.':
labels[line[1:]] = linenum*2
print ("Label: " + line[1:] + " @ " + format(linenum*2, '#02x'))
else:
linenum = linenum + 1
def zerobin(fn):
with open("rom.txt", "w") as file:
file.close()
def writetofile(fn, b1, b2):
with open("rom.txt", "a") as file:
file.write(b1)
file.write(b2)
def Convert(string):
list1 = []
list1[:0] = string
return list1
if len(sys.argv) != 2:
print ("Usage: vASM file.asm")
sys.exit()
zerobin("rom.txt")
parselabels(sys.argv[1])
with open(sys.argv[1]) as f:
for line in f:
# Ignore labels
if line[0]=='.':
continue
# Ignore comments
if line[0]=='#':
continue
line = line.replace('\n', '').replace('\r', '')
tok = re.split(r'[, ]',line)
if '' in tok:
tok.remove('')
print(str(tok))
if tok[0].upper() == "LDR" or tok[0].upper() == "STR" or tok[0].upper() == "ADD" or tok[0].upper() == "SUB" or tok[0].upper() == "MOV" or tok[0].upper() == "MUL" or tok[0].upper() == "DIV" or tok[0].upper() == "MOD" or tok[0].upper() == "CMP" or tok[0].upper() == "PUSH" or tok[0].upper() == "POP" :
r = tok[1]
if tok[0].upper() == "PUSH" : #27
bopr = "011011"
if r == 'X':
writetofile("rom.txt", bopr, " 0\n")
elif r == 'Y' :
writetofile("rom.txt", bopr, " 1\n")
else :
print ("Invalid register name")
print (tok[1].upper())
sys.exit()
elif tok[0].upper() == "POP" : #28
bopr = "011100"
if r == 'X':
writetofile("rom.txt", bopr, " 0\n")
elif r == 'Y' :
writetofile("rom.txt", bopr, " 1\n")
else :
print ("Invalid register name")
print (tok[1].upper())
sys.exit()
else :
op = tok[0].upper()
imm = tok[2]
v = int(imm, 0) #conversie nr string in nr int
bin(v)[2:] #conversie nr int in nr binar + scoatem 0b de la inceput
vbin = f'{v:09b}' #il facem pe 9 biti si string
vbin = vbin + "\n"
print(vbin)
if op == "LDR": #0
bopr = "000000"
elif op == "STR": #1
bopr = "000001"
elif op == "ADD": #9
bopr = "001001"
elif op == "SUB": #10
bopr = "001010"
elif op == "MOV": #15
bopr = "001111"
elif op == "MUL": #16
bopr = "010000"
elif op == "DIV": #17
bopr = "010001"
elif op == "MOD": #18
bopr = "010010"
elif op == "CMP": #23
bopr = "010111"
if r == "X": #0
bopr = bopr + " 0 "
writetofile("rom.txt", bopr, vbin)
elif r == "Y": #1
bopr = bopr + " 1 "
writetofile("rom.txt", bopr, vbin)
else:
print ("Invalid register name")
print (tok[1].upper())
sys.exit()
elif tok[0].upper() == "BRZ" or tok[0].upper() == "BRN" or tok[0].upper() == "BRC" or tok[0].upper() == "BRO" or tok[0].upper() == "BRA" or tok[0].upper() == "JMP" or tok[0].upper() == "RET":
op = tok[0].upper()
if tok[1] in labels:
addr = labels[tok[1]]
v = addr
bin(v)[2:]
vbin = f'{v:010b}'
vbin = " " + vbin + "\n"
#print(vbin)
if op == "BRZ": #2
bop = "000010" #opcode
elif op == "BRN": #3
bop = "000011"
elif op == "BRC": #4
bop = "000100"
elif op == "BRO": #5
bop = "000101"
elif op == "BRA": #6
bop = "000110"
elif op == "JMP": #7
bop = "000111"
elif op == "RET": #8
bop = "001000"
writetofile("rom.txt", bop, vbin)
else:
print ("Unknown label")
print (tok[1])
sys.exit()
else:
print ("Unknown operand")
print (tok[0])
sys.exit()