forked from shane-e945/Python-Assembly-Interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembly_interpreter.py
More file actions
482 lines (406 loc) · 19.6 KB
/
assembly_interpreter.py
File metadata and controls
482 lines (406 loc) · 19.6 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
from assembly_helpers import *
import colorama
from colorama import Fore, Back, Style
import time
import os
from collections import deque
from sys import argv
# Initialize colorama for cross-platform color support
colorama.init(autoreset=True)
def assembler_interpreter(program, DEBUG=False, STEP_MODE=False, DELAY=0.3):
"""Interprets lines of assembly program and returns a set return code"""
# Tokenize the lines and filter out whitespace
program = filter(None, map(process_line, program.split('\n')))
# Convert to a tuple for indexing w/ the line counter
program = tuple(program)
# Error if no end statement in the program
if all(line[0] != 'end' for line in program):
print(f"{Fore.RED}Error: No 'end' statement found in program{Style.RESET_ALL}")
return -1
# Find all functions/routines for line jumping
label_lines = {line[0].rstrip(':'): i for i, line in enumerate(program) if line[0][-1] == ':'}
# Setting auxiliary variables for the language
memory = dict()
registers, line_counter = dict(), 0
prev_lines, return_code = deque(), str()
compare = [0, 0] # Initialize compare to avoid UnboundLocalError
# For execution history
execution_history = []
step_counter = 0
while line_counter < len(program) and program[line_counter][0] != 'end':
step_counter += 1
current_line = program[line_counter]
command = current_line[0]
# Skip label lines - this is the key fix
if command.endswith(':'):
line_counter += 1
continue
other = current_line[1:] if len(current_line) > 1 else []
if STEP_MODE or DEBUG:
clear_screen()
print(f"{Fore.CYAN}=== Assembly Interpreter - Step {step_counter} ==={Style.RESET_ALL}")
print(f"{Fore.YELLOW}Program Counter: {line_counter}{Style.RESET_ALL}")
# Show execution context (5 lines before and after current line)
print(f"\n{Fore.CYAN}Program Context:{Style.RESET_ALL}")
for i in range(max(0, line_counter - 5), min(len(program), line_counter + 6)):
if i == line_counter:
print(f"{Fore.GREEN}→ {i:04d}: {' '.join(str(x) for x in program[i])}{Style.RESET_ALL}")
else:
print(f" {i:04d}: {' '.join(str(x) for x in program[i])}")
# Show registers
print(f"\n{Fore.CYAN}Registers:{Style.RESET_ALL}")
if not registers:
print(" No registers used yet")
else:
for reg_name, reg_value in sorted(registers.items()):
print(f" {reg_name}: {reg_value}")
# Show memory
print(f"\n{Fore.CYAN}Memory (showing up to 10 items):{Style.RESET_ALL}")
if not memory:
print(" No memory used yet")
else:
for i, (addr, value) in enumerate(sorted(memory.items())):
print(f" {addr}: {value}")
if i >= 9: # Show only 10 memory locations max
remaining = len(memory) - 10
if remaining > 0:
print(f" ... and {remaining} more locations")
break
# Show call stack
print(f"\n{Fore.CYAN}Call Stack:{Style.RESET_ALL}")
if not prev_lines:
print(" Empty stack")
else:
for i, addr in enumerate(prev_lines):
print(f" {i}: line {addr}")
# Show compare values
print(f"\n{Fore.CYAN}Compare Values: {compare}{Style.RESET_ALL}")
# Show current command to be executed
print(f"\n{Fore.GREEN}Executing: {' '.join(str(x) for x in current_line)}{Style.RESET_ALL}")
# Show last 5 operations
if execution_history:
print(f"\n{Fore.CYAN}Last Operations:{Style.RESET_ALL}")
for hist in execution_history[-5:]:
print(f" {hist}")
if STEP_MODE:
input(f"{Fore.YELLOW}Press Enter to continue...{Style.RESET_ALL}")
else:
time.sleep(DELAY)
# Record command in history
execution_history.append(f"Step {step_counter:04d}: " + ' '.join(str(x) for x in current_line))
# mov command moves either a constant or register value to a register
if command == 'mov':
register, value = other
registers[register] = get_value(value, registers)
# inc increments the value of a register by 1
elif command == 'inc':
register = other[0]
registers[register] += 1
# dec decrements the value of a register by 1
elif command == 'dec':
register = other[0]
registers[register] -= 1
# add adds the contents of a register and a register or constant
elif command == 'add':
register, value = other
registers[register] += get_value(value, registers)
# sub subtracts the contents of a register and a register or constant
elif command == 'sub':
register, value = other
registers[register] -= get_value(value, registers)
# mul multiplies the contents of a register and a register or constant
elif command == 'mul':
register, value = other
registers[register] *= get_value(value, registers)
# div integer divides the contents of a register and a register or constant
elif command == 'div':
register, value = other
divisor = get_value(value, registers)
if divisor == 0:
print(f"{Fore.RED}Error: Division by zero at line {line_counter}{Style.RESET_ALL}")
return -1
registers[register] //= divisor
# jmp jumps the line counter to the function/routine provided without storing the old location
elif command == 'jmp':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# call jumps the line counter to the function/routine provided while storing the previous one
elif command == 'call':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# ret returns the line counter to the previous line before a routine was called
elif command == 'ret':
if not prev_lines:
print(f"{Fore.RED}Error: 'ret' with empty call stack at line {line_counter}{Style.RESET_ALL}")
return -1
line_counter = prev_lines.pop()
# cmp stores both values for the next jump comparison
elif command == 'cmp':
one, two = other
compare = [get_value(one, registers),
get_value(two, registers)]
# je jumps to function/routine if the comparison is ==
elif command == 'je':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] == compare[1]:
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# ce call to function/routine if the comparison is ==
elif command == 'ce':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] == compare[1]:
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# jne jumps to function/routine if the comparison is !=
elif command == 'jne':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] != compare[1]:
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# cne call to function/routine if the comparison is !=
elif command == 'cne':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] != compare[1]:
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# jge jumps to function/routine if the the comparison is >=
elif command == 'jge':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] >= compare[1]:
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# cne call to function/routine if the comparison is >=
elif command == 'cge':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] >= compare[1]:
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# jg jumps to function/routine if the the comparison is >
elif command == 'jg':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] > compare[1]:
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# cne call to function/routine if the comparison is >
elif command == 'cg':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] > compare[1]:
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# jle jumps to function/routine if the comparison is <=
elif command == 'jle':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] <= compare[1]:
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# cne call to function/routine if the comparison is <=
elif command == 'cle':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] <= compare[1]:
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# jl jumps to function/routine if the comparison is <
elif command == 'jl':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] < compare[1]:
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# cne call to function/routine if the comparison is <
elif command == 'cl':
label = other[0]
if label not in label_lines:
print(f"{Fore.RED}Error: Unknown label '{label}' at line {line_counter}{Style.RESET_ALL}")
return -1
if compare[0] < compare[1]:
prev_lines.append(line_counter)
line_counter = label_lines[label]
continue # Skip incrementing line_counter
# stw stores a value at a memory address
elif command == 'stw':
value, address = other
address = get_address(address, registers)
memory[address] = get_value(value, registers)
# mvw takes a value from a memory address and stores it in a register
elif command == 'mvw':
register, address = other
address = get_address(address, registers)
registers[register] = memory.get(address, 0) # Default to 0 if address not in memory
# msg stores the return output of the program
elif command == 'msg':
parts = []
i = 0
while i < len(other):
part = other[i]
if part.startswith("'") and not part.endswith("'"):
# Handle multi-part strings
full_str = [part[1:]] # Remove opening quote
i += 1
while i < len(other) and not other[i].endswith("'"):
full_str.append(other[i])
i += 1
if i < len(other):
full_str.append(other[i][:-1]) # Remove closing quote
parts.append(' '.join(full_str))
elif part.startswith("'") and part.endswith("'"):
# Handle complete quoted strings
parts.append(part[1:-1])
elif part == '\\n':
# Handle newlines
parts.append('\n')
elif part in registers:
# Handle register values
parts.append(str(registers[part]))
else:
# Handle other text
parts.append(part)
i += 1
message = ''.join(parts)
return_code += message
if not message.endswith('\n'):
return_code += '\n' # Ensure each msg ends with a newline
# Display message immediately in step mode
if STEP_MODE or DEBUG:
print(f"\n{Fore.MAGENTA}Output: {message}{Style.RESET_ALL}")
else:
print(f"{Fore.RED}Error: Unknown command '{command}' at line {line_counter}{Style.RESET_ALL}")
return -1
line_counter += 1
# Catches programs that terminate incorrectly
if line_counter >= len(program):
print(f"{Fore.RED}Error: Program reached end without 'end' statement{Style.RESET_ALL}")
return -1
# Final program state
if STEP_MODE or DEBUG:
clear_screen()
print(f"{Fore.GREEN}=== Program Execution Complete ==={Style.RESET_ALL}")
print(f"\n{Fore.CYAN}Final Register Values:{Style.RESET_ALL}")
for reg_name, reg_value in sorted(registers.items()):
print(f" {reg_name}: {reg_value}")
print(f"\n{Fore.CYAN}Final Output:{Style.RESET_ALL}")
print(f"{return_code}")
print(f"\n{Fore.YELLOW}Program executed in {step_counter} steps{Style.RESET_ALL}")
if STEP_MODE:
input(f"{Fore.YELLOW}Press Enter to exit...{Style.RESET_ALL}")
return return_code
def clear_screen():
"""Clears the terminal screen"""
os.system('cls' if os.name == 'nt' else 'clear')
def display_help():
"""Displays help information for the program"""
print(f"{Fore.CYAN}=== Assembly Interpreter Help ==={Style.RESET_ALL}")
print(f"\n{Fore.YELLOW}Usage:{Style.RESET_ALL}")
print(" assembly_interpreter.py [file] [options]")
print(f"\n{Fore.YELLOW}Options:{Style.RESET_ALL}")
print(" -h, --help Show this help message")
print(" -d, --debug Run in debug mode (shows program state)")
print(" -s, --step Run in step mode (pause after each instruction)")
print(" --delay=N Set delay between instructions in debug mode (seconds)")
print(f"\n{Fore.YELLOW}Commands:{Style.RESET_ALL}")
print(" mov reg, val Move value to register")
print(" inc reg Increment register")
print(" dec reg Decrement register")
print(" add reg, val Add value to register")
print(" sub reg, val Subtract value from register")
print(" mul reg, val Multiply register by value")
print(" div reg, val Divide register by value")
print(" cmp v1, v2 Compare values")
print(" jmp label Jump to label")
print(" je label Jump if equal")
print(" jne label Jump if not equal")
print(" jg label Jump if greater")
print(" jl label Jump if less")
print(" jge label Jump if greater or equal")
print(" jle label Jump if less or equal")
print(" call label Call subroutine")
print(" ret Return from subroutine")
print(" stw val, addr Store value at memory address")
print(" mvw reg, addr Move value from memory to register")
print(" msg ... Output message")
print(" end End program")
print(f"\n{Fore.YELLOW}Examples:{Style.RESET_ALL}")
print(" assembly_interpreter.py test.asm")
print(" assembly_interpreter.py test.asm -s")
print(" assembly_interpreter.py test.asm -d --delay=0.5")
if __name__ == '__main__':
# Initialize colorama for cross-platform color support
colorama.init(autoreset=True)
# Parse command line arguments
if len(argv) < 2 or argv[1] in ['-h', '--help']:
display_help()
exit(0)
assembly_file = argv[1]
# Process options
DEBUG = False
STEP_MODE = False
DELAY = 0.3
for arg in argv[2:]:
if arg in ['-d', '--debug']:
DEBUG = True
elif arg in ['-s', '--step']:
STEP_MODE = True
elif arg.startswith('--delay='):
try:
DELAY = float(arg.split('=')[1])
except ValueError:
print(f"{Fore.RED}Error: Invalid delay value{Style.RESET_ALL}")
exit(1)
try:
with open(assembly_file) as program:
print(f"{Fore.GREEN}Loading program: {assembly_file}{Style.RESET_ALL}")
output = assembler_interpreter(program.read(), DEBUG=DEBUG, STEP_MODE=STEP_MODE, DELAY=DELAY)
if output == -1:
print(f"{Fore.RED}Program execution failed{Style.RESET_ALL}")
exit(1)
if not (DEBUG or STEP_MODE):
print(f"{Fore.GREEN}Program Output:{Style.RESET_ALL}")
print(output)
except FileNotFoundError:
print(f"{Fore.RED}Error: File '{assembly_file}' not found.{Style.RESET_ALL}")
exit(1) # Exit with error code