aboutsummaryrefslogtreecommitdiff
path: root/src/c8asm
diff options
context:
space:
mode:
Diffstat (limited to 'src/c8asm')
-rw-r--r--src/c8asm/assembler.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/c8asm/assembler.c b/src/c8asm/assembler.c
index ea292b7..2e8693b 100644
--- a/src/c8asm/assembler.c
+++ b/src/c8asm/assembler.c
@@ -77,6 +77,8 @@ assemble(FILE *stream, struct ast ast)
fwrite(&__x, 1, sizeof(__x), stream); \
} while (false)
+ bool pad = false;
+
da_foreach (&ast, node) {
if (node->kind == D_LABEL) {
struct label lbl = {
@@ -96,6 +98,11 @@ assemble(FILE *stream, struct ast ast)
if (node->kind != D_INSTR)
unreachable();
+ /* Instructions need to be 0-padded so they appear on an even byte
+ boundary. */
+ if (node->instr.kind != I_DB && pad)
+ putchar(0);
+
switch (node->instr.kind) {
case I_ADD_I_VX:
PUT(0xF01E | (node->instr.args[0].val << 8));
@@ -124,6 +131,8 @@ assemble(FILE *stream, struct ast ast)
case I_DB:
da_foreach (&node->instr, byte)
fputc(*byte, stream);
+ if (node->instr.len & 1)
+ pad = !pad;
break;
case I_DRW:
PUT(0xD000 | (node->instr.args[0].val << 8)