From 6e0d124efd41b9c6df384a4c41343a65ade7fa87 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Tue, 21 Feb 2023 17:42:51 +0000 Subject: [PATCH] add data parsing --- asm/creole.py | 19 ++++++++++++++++--- asm/test.py | 6 ++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/asm/creole.py b/asm/creole.py index a614701..e323432 100644 --- a/asm/creole.py +++ b/asm/creole.py @@ -72,10 +72,10 @@ class Argument: class StringArgument(Argument): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - def __bytes__(self): + def __call__(self): b = bytes() for v in self.val: - b = b + Argument(ArgType.IMM, v)() + b = b + Argument(ArgType.IMM, int(v, base=16))() return b class LabelArgument(Argument): @@ -350,6 +350,19 @@ class Line: class InstructionNotFoundException(Exception): pass + +def _term_sep(s): + """ Split up the arguments of an instruction. + OP arg1 arg2 [data,data,data,...] + """ + + s = s.strip() + s_data = s.split('[') + if len(s_data) == 2: + return s_data[0].split() + [s_data[1].rstrip('] \t\n\r\v').split(',')] + else: + return s.split() + class Program: def _asm_push_line(self, ins, args): l = Line(ins, args) @@ -361,7 +374,7 @@ class Program: :param line: String containing the line. :raises InstructionNotFoundException: """ - line = line.strip().split() + line = _term_sep(line) line[0] = line[0].casefold() if line[0][0] == '.': self.asm.append(line[0]) diff --git a/asm/test.py b/asm/test.py index 4ab05e6..e49d46d 100644 --- a/asm/test.py +++ b/asm/test.py @@ -433,6 +433,12 @@ class ProgramTest(unittest.TestCase): self.range_test(0x7FFFF00, 0x8000000, sgn=True) self.range_test(-100, 0, sgn=True) +class DataTest(unittest.TestCase): + def test_parse_db(self): + p = Program() + p.parse_asm_line("db d0 [4d2,1234,0,5]") + self.assertEqual(p(), b'\x0b\xc0\x80\xe0\x93\x92\xf0\x81\x88\xb4\xc0\x80\xc0\x85\x00') + class SCEnv(ffi.Environment): def syscall(self, s): self.s = s.value