Skip to content

Memory Model

Adam Mathlay edited this page Sep 9, 2024 · 4 revisions

Memory Model in MVM

This document describes the memory model used in the Micro Virtual Machine (MVM).

Overview

MVM uses a flat, linear memory model with a fixed size.

  • Flat Memory: All memory locations are directly addressable, and there is no segmentation or paging.
  • Fixed Size: The memory has a predetermined size (1024 words), which is set at the time the VM starts.

Memory Addressing

  • Addresses: Memory locations are addressed using integers.
  • Zero-Based Indexing: Memory addresses start from 0. The first memory location is at address 0, the second at address 1, and so on.

Data Storage

  • Words: The basic unit of memory is a word, which is currently a 32-bit integer in MVM.
  • Data Types: The memory can store various data types, including:
    • Integers (represented as 32-bit words)
    • Characters (represented as ASCII codes, using a single word per character)
    • Strings (represented as sequences of ASCII codes, stored in consecutive memory locations)

Memory Access

  • LOAD Instruction: Loads a value from a specified memory address into a register.
  • STORE Instruction: Stores a value from a register into a specified memory address.

Example:

// Load the value at memory address 100 into register G1
LOAD 100 G1 

// Store the value in register G2 into memory address 200
STORE G2 200

String Storage

  • TODO. Add null-terminated strings to the VM and make the len: Int argument to writeIO to optional instead of mandatory.

Strings are stored as null-terminated sequences of characters in consecutive memory locations. Each character is represented by its ASCII code, stored as a 32-bit integer. The null terminator (a byte with the value 0) indicates the end of the string.

Example:

// Store the string "hello" in memory starting at address 500

// 'h' (ASCII code 104)
LIT G1 104
STORE G1 500

// 'e' (ASCII code 101)
LIT G1 101
STORE G1 501

// 'l' (ASCII code 108)
LIT G1 108
STORE G1 502

// 'l'
STORE G1 503

// 'o' (ASCII code 111)
LIT G1 111
STORE G1 504

// Null terminator (0)
LIT G1 0 
STORE G1 505

Memory Management

  • Static Allocation: MVM does not use dynamic memory allocation (no ALLOC instruction). All memory is pre-allocated when the VM starts.
  • Fragmentation: Since memory is not dynamically allocated, there is no issue of memory fragmentation.

Error Handling

  • Invalid Memory Access: The VM checks for out-of-bounds memory accesses and throws an InvalidMemoryAddressException if access occurs outside the allocated memory range.

Key Points:

  • Simplicity: The flat, fixed-size memory model makes memory management simple.
  • Direct Addressing: All memory locations are directly addressable.
  • Limited Size: The fixed memory size might be a limitation for larger programs.
Clone this wiki locally