From e4c92a98df06de5e2819e5ab9456afa0a4596c42 Mon Sep 17 00:00:00 2001 From: Marty Stumpf Date: Thu, 25 Apr 2024 21:18:02 -0700 Subject: [PATCH] Convert to big endian. (#163) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: thealmarty <“thealmartyblog@gmail.com”> Co-authored-by: Morgan Thomas --- machine/src/core.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/machine/src/core.rs b/machine/src/core.rs index f854462..c0c2597 100644 --- a/machine/src/core.rs +++ b/machine/src/core.rs @@ -10,8 +10,9 @@ pub struct Word(pub [F; MEMORY_CELL_BYTES]); // Functions for byte manipulations /// Get the index of a byte in a memory cell. +/// The index is converted from little endian (as emitted by the compiler) to big endian. pub fn index_of_byte(addr: u32) -> usize { - (addr & 3) as usize + (3 - (addr & 3)) as usize } /// Get the address of the memory cells which is not empty (a multiple of 4). @@ -41,9 +42,15 @@ impl Word { } } +// The cell is stored in little endian format in the compiler. But the VM stores it in big endian. impl Word { pub fn update_byte(self, byte: u8, loc: usize) -> Self { - let mut result: [u8; MEMORY_CELL_BYTES] = self.0; + let result_little_end: [u8; MEMORY_CELL_BYTES] = self.0; + let mut result = [0; 4]; + // Convert from little to big endian. + for i in 0..MEMORY_CELL_BYTES { + result[i] = result_little_end[3 - i]; + } result[loc] = byte; Self(result) }