Appearance
Solana Opcode Guide
Courtesy of Distributed Atomic State Machine Algorithms Corporation (DASMAC)
📚 Background
Solana programs are typically written in Rust, then compiled via LLVM into an ELF file with bytecode that runs on a virtual machine. Native Rust techniques, generally considered low-level due to the manual memory management operations they perform entail, are often further supplemented by higher-level frameworks like Anchor that provide further abstractions at the cost of execution overhead.
Yet at its core, Solana simply runs SBPF opcodes (based on eBPF) to manipulate bytes. Mastery of these opcodes and their syscall support (for things like logging) enables high-performance program development in excess of what traditional techniques can deliver.
For example, compute unit analysis from this guide shows that Rust can easily introduce 50-100% overhead compared with hand-written assembly, even when using high-performance frameworks like pinocchio.
Contrary to typical approaches of higher-level frameworks and abstractions, this guide surveys optimization techniques that are only possible when writing in assembly, helping you gain full control over program execution to squeeze as much performance as possible out of the Solana Virtual Machine.
💡 Example
Here is a simple "Hello, World!" program implemented in both SBPF assembly and Rust:
asm
.equ MESSAGE_LENGTH, 14
.globl entrypoint
entrypoint:
lddw r1, message
lddw r2, MESSAGE_LENGTH
call sol_log_
exit
.rodata
message: .ascii "Hello, DASMAC!"rs
use pinocchio::{entrypoint, AccountView, Address, ProgramResult};
#[cfg(target_os = "solana")]
use pinocchio::syscalls::sol_log_;
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Address,
_accounts: &[AccountView],
_instruction_data: &[u8],
) -> ProgramResult {
#[cfg(target_os = "solana")]
{
const MESSAGE: &[u8] = b"Hello, DASMAC!";
unsafe { sol_log_(MESSAGE.as_ptr(), MESSAGE.len() as u64) };
}
Ok(())
}🚀 Continue your journey
Start with the quickstart to set up your environment and run the above program, then follow the examples in order to incrementally learn more advanced SBPF concepts. See also the indices for additional reference material, including a full list of SBPF opcodes and Solana syscalls used in the examples.