From a721fdf3dfefa817701371a62c834619055d7ce7 Mon Sep 17 00:00:00 2001 From: kdx Date: Thu, 13 Apr 2023 23:48:14 +0200 Subject: ROT DBG --- samples/helloworld.orgaasm | 1 + spec.md | 1 + src/ops.h | 2 ++ src/orgaasm.c | 12 +++++++++--- src/orgaemu.c | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/samples/helloworld.orgaasm b/samples/helloworld.orgaasm index 2c3a0fa..b78de23 100644 --- a/samples/helloworld.orgaasm +++ b/samples/helloworld.orgaasm @@ -7,6 +7,7 @@ RET INC ( increment pointer ) DUP LDA ( loop until end of string ) JNZ ,putstr + POP RET ( "hello world" ) diff --git a/spec.md b/spec.md index bac7d39..640622a 100644 --- a/spec.md +++ b/spec.md @@ -37,3 +37,4 @@ RSF ( a shift -- c ) right shift SLP ( -- ) give control back to the interpreter INC ( a -- a+1 ) DEC ( a -- a-1 ) +DBG ( -- ) output debug informations, can be omitted in release builds diff --git a/src/ops.h b/src/ops.h index de30ea2..0d06f1b 100644 --- a/src/ops.h +++ b/src/ops.h @@ -34,6 +34,7 @@ enum { OP_SLP, OP_INC, OP_DEC, + OP_DBG, }; static const char ops[][4] = { @@ -70,4 +71,5 @@ static const char ops[][4] = { [OP_SLP] = "SLP", [OP_INC] = "INC", [OP_DEC] = "DEC", + [OP_DBG] = "DBG", }; diff --git a/src/orgaasm.c b/src/orgaasm.c index 793a2dd..3e8477d 100644 --- a/src/orgaasm.c +++ b/src/orgaasm.c @@ -146,11 +146,17 @@ first_pass(char *s) continue; } - /* Label marker. */ - if (tok[0] == '@') + switch (tok[0]) { + case '@': /* label marker */ register_label(tok + 1, pc); - else + break; + case '#': /* literal marker */ + pc += 2; + break; + default: pc += 1; + break; + } } } diff --git a/src/orgaemu.c b/src/orgaemu.c index 74fd41f..38bd412 100644 --- a/src/orgaemu.c +++ b/src/orgaemu.c @@ -95,6 +95,14 @@ exec_op(uint16_t *mem, long pc) push(b); return pc + 1; } + case OP_ROT: { + const uint16_t a = pop(); + const uint16_t b = pop(); + const uint16_t c = pop(); + push(b); + push(a); + push(c); + } return pc + 1; case OP_DUP: { const uint16_t a = pop(); push(a); @@ -145,6 +153,12 @@ exec_op(uint16_t *mem, long pc) case OP_DEC: push(pop() - 1); return pc + 1; + case OP_DBG: + fprintf(stderr, "<%ld> ", stack_ptr); + for (size_t i = 0; i < stack_ptr; i++) + fprintf(stderr, "%d ", stack[i]); + fputc('\n', stderr); + return pc + 1; default: fprintf(stderr, "unhandled opcode %04x\n", mem[pc]); if (mem[pc] <= OP_SLP) -- cgit v1.2.3