From 66e28d6ef8a293bca0d741c218b41510f6511800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Tue, 10 May 2022 11:19:22 +0200 Subject: [PATCH] =?UTF-8?q?Properly=20handle=20non-instruction=20bytes=20i?= =?UTF-8?q?n=20the=20range=200xa0=E2=80=A60xbf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously address arguments were extracted without verifying the start byte corresponded to a valid instruction. In case of a non-instruction byte this would result in the two following bytes to be silently skipped. --- .gitignore | 1 + thingamajig_disasm.py | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8a0dce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.bin diff --git a/thingamajig_disasm.py b/thingamajig_disasm.py index 1655643..fda93b6 100644 --- a/thingamajig_disasm.py +++ b/thingamajig_disasm.py @@ -41,23 +41,22 @@ def segment(binary, origin): rx = (byte >> 2) & 3 ry = byte & 3 - addr = None - if opcodes[opcode].addr: - addr = (binary[ip + 1] << 8) + binary[ip + 2] - valid = True if not opcodes[opcode].rx and rx != 0: valid = False if not opcodes[opcode].ry and ry != 0: valid = False - if valid: + if not valid: + statements.append(Statement(ip, Data(byte))) + ip += 1 + elif opcodes[opcode].addr: + addr = (binary[ip + 1] << 8) + binary[ip + 2] instruction = Instruction(opcode, rx, ry, addr) statements.append(Statement(ip, instruction)) + ip += 3 else: - statements.append(Statement(ip, Data(byte))) - - ip += 1 - if opcodes[opcode].addr: - ip += 2 + instruction = Instruction(opcode, rx, ry, None) + statements.append(Statement(ip, instruction)) + ip += 1 return statements