diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-15 05:17:33 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-15 05:18:43 +0100 |
commit | 724f5e02585990b70185e3ccd43904611e21a5f6 (patch) | |
tree | 922adb3170259286ab370ca19d75866734d98cca /src/main.c | |
parent | 54b836ba644bab7e80d948459a006e3053783d5a (diff) | |
download | ovm-724f5e02585990b70185e3ccd43904611e21a5f6.tar.gz ovm-724f5e02585990b70185e3ccd43904611e21a5f6.tar.bz2 ovm-724f5e02585990b70185e3ccd43904611e21a5f6.zip |
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.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -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; } |