aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-15 04:40:14 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-15 04:57:30 +0100
commit118083316114bd9b28ead41016058d37957dad70 (patch)
tree0ccb5974fe7b2b01a74f69fffc675f1cdf3e802e /src
parentd52d7ad413ffaf5d48fe57c082d823f6052cbc4a (diff)
downloadovm-118083316114bd9b28ead41016058d37957dad70.tar.gz
ovm-118083316114bd9b28ead41016058d37957dad70.tar.bz2
ovm-118083316114bd9b28ead41016058d37957dad70.zip
Implemented functions to pop a byte and a word
Diffstat (limited to 'src')
-rw-r--r--src/main.c21
1 files changed, 21 insertions, 0 deletions
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!");