From 38813b42f2a29f75da56bd731873330def610ee6 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Wed, 10 Apr 2024 22:16:41 +0200 Subject: [PATCH] print each step format --- src/main.py | 1 - src/ram.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) delete mode 100644 src/main.py diff --git a/src/main.py b/src/main.py deleted file mode 100644 index 8b13789..0000000 --- a/src/main.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/ram.py b/src/ram.py index f28caa8..12640f5 100644 --- a/src/ram.py +++ b/src/ram.py @@ -98,15 +98,40 @@ class Ram(object): else: self.current += 1 + def __register_to_str(register): + if register[0] == '@': + return f'{register[3]}@{register[2]}{register[1]}' + elif register[0] == 'value': + return f'{register[1]}' + else: + return f'{register[0]}{register[1]}' + + def __instr_to_str(self, instr_number): + instr = self.instr[instr_number] + op = instr["args"][0] + number = f'{instr_number} :' + instr_str = f' {number: <6}{op}(' + if op == 'JUMP': + instr_str += f'{instr["args"][1]})' + elif op == 'JL' or op == 'JE': + instr_str += f'{instr["args"][1]}, ' + instr_str += f'{Ram.__register_to_str(instr["args"][2])}, ' + instr_str += f'{Ram.__register_to_str(instr["args"][3])})' + else: + instr_str += f'{Ram.__register_to_str(instr["args"][1])}, ' + instr_str += f'{Ram.__register_to_str(instr["args"][2])}, ' + instr_str += f'{Ram.__register_to_str(instr["args"][3])})' + return instr_str + + def execute(self): ''' Run machine to completion ''' while self.current < len(self.instr): - print(self.current+2) - #print(*self.instr[self.current]['args']) + instr_str = self.__instr_to_str(self.current) self.instr[self.current]['op'](self, *self.instr[self.current]['args']) - print(self.work_registers) + print(f'{instr_str: <28}{self.work_registers}') def generate_graph(self): '''