Skip to content

Commit

Permalink
Convert to big endian. (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: thealmarty <“[email protected]”>
Co-authored-by: Morgan Thomas <[email protected]>
  • Loading branch information
3 people authored Apr 26, 2024
1 parent fdf0ddf commit e4c92a9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions machine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub struct Word<F>(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).
Expand Down Expand Up @@ -41,9 +42,15 @@ impl Word<u8> {
}
}

// The cell is stored in little endian format in the compiler. But the VM stores it in big endian.
impl Word<u8> {
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)
}
Expand Down

0 comments on commit e4c92a9

Please sign in to comment.