ram
Vm

Memory Model

Understanding the RAM memory model

Memory Structure

The memory in the RAM VM is conceptually organized as an infinite array of cells, each capable of storing an integer value. Each cell is identified by an address, which is also an integer.

Address:    0    1    2    3    4    5    ...
Value:     [0]  [5]  [7]  [0]  [0]  [0]  ...

In practice, the memory is implemented as a sparse array, meaning that only cells that have been written to are actually stored in memory. This allows for efficient use of memory resources.

Memory Access

Memory can be accessed using different addressing modes:

  • Direct addressing: Access a memory cell directly by its address
  • Indirect addressing: Use the value in a memory cell as the address to access another cell
  • Immediate addressing: Use a literal value directly, without accessing memory
  • Register addressing: Access a register directly

See the Operands page for more details on addressing modes.

Registers

In addition to the main memory, the RAM VM includes a set of registers. Registers are special memory locations that can be accessed more efficiently than regular memory.

The most important register is the accumulator (R0), which is used for most arithmetic operations. Other registers (R1, R2, etc.) can be used for temporary storage.

Memory Initialization

When a RAM program starts execution, all memory cells are initialized to 0. However, you can provide initial values for specific memory cells when running a program.

For example, when using the CLI, you can specify initial memory values using the --memory flag:

ram run program.ram --memory "1=5,2=7"

This sets memory address 1 to 5 and memory address 2 to 7 before the program starts execution.

Memory Limitations

While the RAM memory model is conceptually infinite, practical implementations may have limitations:

  • The maximum value that can be stored in a memory cell may be limited by the implementation (e.g., 32-bit or 64-bit integers)
  • The maximum address that can be used may also be limited
  • The total amount of memory available may be constrained by the host system

These limitations are implementation-specific and may vary depending on the platform and configuration.

Example

Here's an example of how memory is used in a RAM program:

# Program to swap two values
        LOAD 1      # Load the value from address 1 into the accumulator
        STORE 3     # Store it temporarily in address 3
        LOAD 2      # Load the value from address 2
        STORE 1     # Store it in address 1
        LOAD 3      # Load the temporary value
        STORE 2     # Store it in address 2
        HALT        # Stop execution

Assuming memory addresses 1 and 2 initially contain the values 5 and 7 respectively, the memory state would evolve as follows:

Initial:
Address:    0    1    2    3    ...
Value:     [0]  [5]  [7]  [0]  ...

After STORE 3:
Address:    0    1    2    3    ...
Value:     [5]  [5]  [7]  [5]  ...

After STORE 1:
Address:    0    1    2    3    ...
Value:     [7]  [7]  [7]  [5]  ...

After STORE 2:
Address:    0    1    2    3    ...
Value:     [5]  [7]  [5]  [5]  ...

After execution, the values in addresses 1 and 2 have been swapped.

The RAM memory model is designed to be simple and intuitive, making it easy to understand how programs manipulate data. This simplicity is one of the reasons why the Random Access Machine is a popular model for teaching computational concepts.

On this page