-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
217 lines (206 loc) · 6.12 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import sys
# erase all lines in program and write your own assembly code.
program = '''
MOV a, 8 ; value
MOV b, 0 ; next
MOV c, 0 ; counter
MOV d, 0 ; first
MOV e, 1 ; second
CALL proc_fib
CALL print
END
proc_fib: ; generic time label comment
CMP c, 2
JL func_0
MOV b, d
ADD b, e
MOV d, e
MOV e, b
INC c
CMP c, a
JLE proc_fib
RET
func_0:
MOV b, c
INC c
JMP proc_fib
print:
MSG 'Term ', a, ' of Fibonacci series is: ', b ; output text
RET
'''
def main():
timer = float(input("\n\nChoose the speed of the execution from 0 to 10. (5 as default): "))/10
assembler_interpreter(program, timer)
def set_up_commands():
global MOV, INC, DEC, ADD, SUB, MUL, DIV, JMP, CMP, JNE, JE, JGE, JG, JLE, JL, CALL, RET, MSG, END, COMMENT, commands
def MOV(args):
x, y = args[0].strip(', '), args[1]
registers[x] = get_value(y, registers)
def INC(args):
x = args[0]
registers[x] += 1
def DEC(args):
x = args[0]
registers[x] -= 1
def ADD(args):
x, y = args[0].strip(', '), args[1]
registers[x] += get_value(y, registers)
def SUB(args):
x, y = args[0].strip(', '), args[1]
registers[x] -= get_value(y, registers)
def MUL(args):
x, y = args[0].strip(', '), args[1]
registers[x] *= get_value(y, registers)
def DIV(args):
x, y = args[0].strip(', '), args[1]
registers[x] //= get_value(y, registers)
def JMP(args):
global line_number
lbl = args[0]
line_number = labels[lbl]
def CMP(args):
'''
CMP = 0 if equal
CMP = 1 if x > y
CMP = -1 if x < y
'''
global compare
x, y = args[0].strip(', '), args[1]
x, y = get_value(x, registers), get_value(y, registers)
compare = (x > y) - (x < y)
def JNE(args):
global line_number
if compare != 0:
lbl = args[0]
line_number = labels[lbl]
def JE(args):
global line_number
if compare == 0:
lbl = args[0]
line_number = labels[lbl]
def JGE(args):
global line_number
if compare >= 0:
lbl = args[0]
line_number = labels[lbl]
def JG(args):
global line_number
if compare == 1:
lbl = args[0]
line_number = labels[lbl]
def JLE(args):
global line_number
if compare <= 0:
lbl = args[0]
line_number = labels[lbl]
def JL(args):
global line_number
if compare == -1:
lbl = args[0]
line_number = labels[lbl]
def CALL(args):
global line_number, line_number_reference, in_a_function
lbl = args[0]
if not in_a_function:
line_number_reference, in_a_function = line_number, True
line_number = labels[lbl]
def RET(args):
global line_number, in_a_function
line_number = line_number_reference
in_a_function = False
def MSG(args):
global output
output = make_msg(args)
def END(args):
global line_number, program_ended_successfully
program_ended_successfully = True
line_number = total_lines
def COMMENT(args): pass
commands = {
'MOV' : MOV,
'INC' : INC,
'DEC' : DEC,
'ADD' : ADD,
'SUB' : SUB,
'MUL' : MUL,
'DIV' : DIV,
'JMP' : JMP,
'CMP' : CMP,
'JNE' : JNE,
'JE' : JE,
'JGE' : JGE,
'JG' : JG,
'JLE' : JLE,
'JL' : JL,
'CALL' : CALL,
'RET' : RET,
'MSG' : MSG,
'END' : END,
';' : COMMENT,
'' : COMMENT
}
def set_labels(program):
global labels
labels = {}
for index, line in enumerate(program.split('\n')):
if line != '' and ':' in line and "'" not in line[:line.index(':')+1]:
lbl = line[:line.index(':')]
labels[lbl] = index
return labels
def get_value(x, registers):
if x in registers.keys():
return registers[x]
return int(x)
def make_msg(string):
import re
pattern = r"(', '|.|'.*?')"
remove_comment = string[:string.index(';')].strip(' ') if ';' in string else string.strip(' ')
message = re.findall(f"{pattern},", remove_comment) + re.findall(f",? {pattern}$", remove_comment)
output = ''
for word in message:
if word[0] == "'": # if the word is a literal message, we join it with the ouput
output += word.strip("'")
else:
output += str(get_value(word, registers))
return output
def assembler_interpreter(program, timer):
set_up_commands()
set_labels(program)
visuals = timer > 0
global registers, total_lines, in_a_function, program_ended_successfully, line_number
registers = {}
total_lines = len(program.split('\n'))
in_a_function = False
program_ended_successfully = False
if visuals:
gui = program.split('\n')
gui = [line + ' '*10 for line in gui]
gui.append(f"registers: {registers}")
line_number = 1
while line_number < total_lines:
line = program.split('\n')[line_number]
if visuals:
gui[line_number] += '*'
gui[-1] = f"registers: {registers}"
print('\n'*5) # we center the program output (1)
print('\n'.join(gui))
gui[line_number] = gui[line_number].strip('*')
if line != '':
command, *args = line.strip(' ').strip('\t').split(' ')
do_command = commands[command]
if command != 'MSG':
args = list(filter(lambda x : x!='', args))
else:
args = line
do_command(args)
line_number += 1
import time
time.sleep(timer)
sys.stdout.flush()
if program_ended_successfully:
if visuals: print('\n',output)
else: print(output)
return output
return "Error. \nProgram ended with exit code -1"
if __name__ == '__main__':
main()