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.
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:
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:
Assuming memory addresses 1 and 2 initially contain the values 5 and 7 respectively, the memory state would evolve as follows:
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.