Architecture
The VM is built around three key components:Registers
16 general-purpose 64-bit registers (R0-R15)
Memory
Linear byte-addressable RAM (max 1MB)
Storage
Persistent key-value storage (32-byte keys/values)
Register-Based vs Stack-Based
Why Register-Based?
Why Register-Based?
Register-based VMs offer several advantages over stack-based architectures:Register-Based (Minichain):
- Fewer instructions: Operations work directly on registers instead of push/pop sequences
- Compact bytecode: No need for stack manipulation instructions
- Better performance: Reduced instruction count and memory access
- Easier optimization: Register allocation enables better compiler optimizations
Example Comparison
Computingc = a + b:Stack-Based (EVM-style):Key Features
40+ Opcodes
The VM supports over 40 opcodes organized into categories:- Control Flow: HALT, NOP, JUMP, JUMPI, CALL, RET, REVERT
- Arithmetic: ADD, SUB, MUL, DIV, MOD, ADDI
- Bitwise: AND, OR, XOR, NOT, SHL, SHR
- Comparison: EQ, NE, LT, GT, LE, GE, ISZERO
- Memory: LOAD8, LOAD64, STORE8, STORE64, MSIZE, MCOPY
- Storage: SLOAD, SSTORE
- Immediate: LOADI, MOV
- Context: CALLER, CALLVALUE, ADDRESS, BLOCKNUMBER, TIMESTAMP, GAS
- Debug: LOG
Gas Metering
All operations consume gas to prevent infinite loops and resource exhaustion:Execution Context
Each VM execution has access to:- Caller address: Who initiated the transaction
- Contract address: The current contract’s address
- Call value: Amount sent with the transaction
- Block number: Current blockchain height
- Timestamp: Block timestamp
- Gas remaining: Available gas for execution
VM Structure
From the source code atcrates/vm/src/executor.rs:44:
Example Program
Here’s a simple program that adds two numbers:Next Steps
Registers & Memory
Learn about the register file and memory model
Opcodes
Complete opcode reference with encoding details
Gas Metering
Understand gas costs and metering
Execution Model
Deep dive into the execution loop