Learn how to write smart contracts for Minichain using assembly language
Smart contracts in Minichain are programs that run on the register-based VM. This guide covers best practices, patterns, and techniques for writing efficient and secure contracts.
; Contract description and purpose.entry main; Constants.const STORAGE_SLOT_COUNTER 0.const STORAGE_SLOT_OWNER 1main: ; Your contract logic here HALT; Helper functions (if using CALL/RET)helper_function: ; Function implementation RET
Contracts have access to linear memory (up to 1MB):
; Allocate memory by storing to itLOADI R0, 0 ; Address 0LOADI R1, 42 ; Value to storeSTORE64 R0, R1 ; Store 64-bit value at address 0LOAD64 R2, R0 ; Load back from address 0; Byte-level accessLOADI R3, 8 ; Address 8LOADI R4, 0xFF ; Byte valueSTORE8 R3, R4 ; Store single byteLOAD8 R5, R3 ; Load single byte
Memory addresses are byte-addressable. Use LOAD64/STORE64 for 64-bit values and LOAD8/STORE8 for bytes.
main: ; Who called this contract? CALLER R0 LOG R0 ; Debug: print caller address ; How much value was sent? CALLVALUE R1 LOG R1 ; What's my address? ADDRESS R2 LOG R2 ; Block context BLOCKNUMBER R3 TIMESTAMP R4 ; Remaining gas GAS R5 HALT
.entry mainmain: LOADI R0, 5 LOADI R1, 10 LOADI R2, add_numbers CALL R2 ; Call function ; Result in R3 LOG R3 HALTadd_numbers: ADD R3, R0, R1 ; R3 = R0 + R1 RET ; Return to caller
The VM does not provide a call stack for automatic return address management. If you use CALL, ensure you have a strategy for handling return addresses (e.g., storing in a register).