From 1e11162f5f516c5eb5d56bb1b82dd1810400ee58 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 26 Nov 2023 16:14:38 +0100 Subject: [PATCH] Added asm instruction tests --- include/ulas.h | 5 +++++ src/test.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/ulas.h b/include/ulas.h index 8472fdd..2a7d5a2 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -469,6 +469,11 @@ void ulas_exprbuffree(struct ulas_exprbuf *eb); * Assembly step */ +// assembles an instruction, writes bytes into dst +// returns bytes written or -1 on error +int ulas_asminstr(char *dst, unsigned long max, const char **line, + unsigned long n); + // returns 0 if no more data can be read // > 0 if data was read // -1 on error diff --git a/src/test.c b/src/test.c index 6264afa..1fbdfc6 100644 --- a/src/test.c +++ b/src/test.c @@ -277,9 +277,27 @@ void test_intexpr(void) { TESTEND("intexpr"); } +#define ASSERT_ASMINSTR(expect_len, line, ...) \ + { \ + const char *l = line; \ + int n = strlen(l); \ + char dst[64]; \ + unsigned char expect_dst[] = {__VA_ARGS__}; \ + int res = ulas_asminstr(dst, 64, &l, n); \ + assert(res == expect_len); \ + for (int i = 0; i < res; i++) { \ + assert(expect_dst[i] == (unsigned char)dst[i]); \ + } \ + } + void test_asminstr(void) { TESTBEGIN("asminstr"); + ASSERT_ASMINSTR(1, "nop", 0x00); + ASSERT_ASMINSTR(1, "halt", 0x76); + ASSERT_ASMINSTR(1, "ld b, c", 0x41); + ASSERT_ASMINSTR(2, "ldh a, [1 + 2]", 0xF0, 0x03); + TESTEND("asminstr"); } -- 2.30.2