From 118083316114bd9b28ead41016058d37957dad70 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 15 Oct 2023 04:40:14 +0100 Subject: Implemented functions to pop a byte and a word --- src/main.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main.c b/src/main.c index 130dbfd..171a0a1 100644 --- a/src/main.c +++ b/src/main.c @@ -44,6 +44,7 @@ typedef union void vm_push_byte(vm_t *vm, data_t b) { if (vm->stack.pointer >= VM_STACK_MAX) + // TODO: Error STACK_OVERFLOW return; vm->stack.data[vm->stack.pointer++] = b.as_byte; } @@ -52,11 +53,31 @@ void vm_push_word(vm_t *vm, data_t w) { // NOTE: Relies on sizeof measuring in bytes if (vm->stack.pointer + sizeof(w) >= VM_STACK_MAX) + // TODO: Error STACK_OVERFLOW return; memcpy(vm->stack.data + vm->stack.pointer, &w.as_word, sizeof(w.as_word)); vm->stack.pointer += sizeof(w.as_word); } +byte vm_pop_byte(vm_t *vm) +{ + if (vm->stack.pointer == 0) + // TODO: Error STACK_UNDERFLOW + return 0; + return vm->stack.data[--vm->stack.pointer]; +} + +word vm_pop_word(vm_t *vm) +{ + if (vm->stack.pointer < sizeof(word)) + // TODO: Error STACK_UNDERFLOW + return 0; + word w = 0; + memcpy(&w, vm->stack.data + vm->stack.pointer - sizeof(w), sizeof(w)); + vm->stack.pointer -= sizeof(w); + return w; +} + int main(void) { puts("Hello, world!"); -- cgit v1.2.3-13-gbd6f