From 724f5e02585990b70185e3ccd43904611e21a5f6 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 15 Oct 2023 05:17:33 +0100 Subject: Force big endian when pushing words This ensures that if one wanted to pop a word byte by byte, they'd go from least to most significance. Machine independent so that's nice. --- src/main.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 19008eb..1269fe7 100644 --- a/src/main.c +++ b/src/main.c @@ -60,8 +60,14 @@ void vm_push_word(vm_t *vm, data_t w) if (vm->stack.pointer + WORD_SIZE >= VM_STACK_MAX) // TODO: Error STACK_OVERFLOW return; - memcpy(vm->stack.data + vm->stack.pointer, &w.as_word, WORD_SIZE); - vm->stack.pointer += WORD_SIZE; + // By default store in big endian + for (size_t i = 64; i > 0; i -= 8) + { + const word mask = ((word)0b11111111) << (i - 8); + byte b = (w.as_word & mask) >> (i - 8); + printf("PUSH(%lu): pushed byte %X\n", i, b); + vm_push_byte(vm, DBYTE(b)); + } } byte vm_pop_byte(vm_t *vm) @@ -78,8 +84,12 @@ word vm_pop_word(vm_t *vm) // TODO: Error STACK_UNDERFLOW return 0; word w = 0; - memcpy(&w, vm->stack.data + vm->stack.pointer - WORD_SIZE, WORD_SIZE); - vm->stack.pointer -= WORD_SIZE; + for (size_t i = 0; i < WORD_SIZE; ++i) + { + byte b = vm_pop_byte(vm); + printf("POP(%lu): popped byte %X\n", i, b); + w = w | ((word)b << (i * 8)); + } return w; } -- cgit v1.2.3-13-gbd6f