Examples

Explore interactive examples of GateVM Assembly operations. Each example walks through step-by-step execution and integrates with real smart contracts. You can try every example directly in our playground using the links below.

Basic Storage Operations

Reading a Single Storage Slot

This demonstrates the simplest Gateway request: reading a single uint256 value from storage slot 0.

Smart Contract:
contract C {
  uint256 slot0 = 123456; // Random value
}
GateVM Assembly:
SET_TARGET
SET_SLOT 0x0
READ_SLOT
SET_OUTPUT 0
Try in Playground →

Reading Packed Values from Single Slot

Reading two uint128 values packed into a single storage slot using slice operations.

Smart Contract:
contract C {
  uint128 slot0a = 12345;
  uint128 slot0b = 67890;
}
GateVM Assembly:
SET_TARGET
SET_SLOT 0x0
READ_SLOT
SLICE 16,16    # Extract lower 16 bytes (slot0a)
SET_OUTPUT 0
READ_SLOT
SLICE 0,16     # Extract upper 16 bytes (slot0b)
SET_OUTPUT 1
Try in Playground →

Mapping Operations

Address to String Mapping

Reading a short string value from a mapping using an address as the key. Demonstrates how Solidity stores short strings.

Smart Contract:
contract C {
  mapping(address => string) mappedData;
  
  constructor() {
    address addr = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
    mappedData[addr] = "Hello";
  }
}
GateVM Assembly:
SET_TARGET
SET_SLOT 0
PUSH_20 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
FOLLOW
READ_SLOT
SET_OUTPUT 0
Try in Playground →

Advanced Operations

Dynamic Key Usage with Chained Mappings

Reading from one slot to get a key, then using that key to access another mapping. Demonstrates the power of dynamic operations and slice for string processing.

Smart Contract:
contract C {
  uint256 latest = 262;                    // Slot 0
  mapping(uint256 => string) mappingData;  // Slot 1
  mapping(string => string) realNames;     // Slot 2

  constructor() {
    mappingData[262] = "clowes";
    realNames["clowes"] = "Thomas Clowes";
  }
}
GateVM Assembly:
SET_TARGET
SET_SLOT 0
READ
SET_OUTPUT 0      # Store the key (262)
PUSH_OUTPUT 0     # Use the key we just read
SET_SLOT 1
FOLLOW            # mappingData[262]
READ_BYTES        # Get "clowes"
SET_SLOT 2
FOLLOW            # realNames["clowes"]
READ_BYTES
SLICE 0 6         # Extract first 3 characters
SET_OUTPUT 1
Try in Playground →

Nested Struct Mappings

Navigating through nested struct mappings using offset to access different struct fields and nested mappings within structs.

Smart Contract:
contract C {
  struct Node {
    uint256 num;                    // Offset 0
    string str;                     // Offset 1
    mapping(bytes => Node) map;     // Offset 2
  }
  Node root; // Slots 0-2

  constructor() {
    root.num = 1;
    root.str = "raffy";
    root.map["a"].num = 2;
    root.map["a"].str = "chonk";
    root.map["a"].map["b"].num = 3;
    root.map["a"].map["b"].str = "eth";
  }
}
GateVM Assembly:
SET_TARGET
SET_SLOT 0
OFFSET 2          # Go to map field of root struct
PUSH_STR "a"
FOLLOW            # root.map["a"]
OFFSET 2          # Go to map field of nested struct
PUSH_STR "b"
FOLLOW            # root.map["a"].map["b"]
OFFSET 1          # Go to str field
READ_BYTES        # Get "eth"
SET_OUTPUT 0
Try in Playground →

EVAL_LOOP Example

Complete example showing program definition and EVAL_LOOP usage. This example defines constant "STACK_VAL = 7", uses EVAL_LOOP with count=3 and flags=0b1101 (STOP_ON_SUCCESS + ACQUIRE_STATE + KEEP_ARGS).

GateVM Assembly:
STACK_VAL = 7

PROGRAM FOO
  PUSH STACK_VAL
  DUP
  TARGET
  SWAP
  ASSERT 1

PUSH 0
PUSH 1
PUSH 0
PUSH_PROGRAM FOO
EVAL_LOOP 3 0b1101
Try in Playground →

FOR_INDEX Example

This example demonstrates the FOR_INDEX loop functionality, which expands to individual instructions and supports index variables.

GateVM Assembly:
FOR_INDEX 0 2 1
  PUSH INDEX
  SET_SLOT INDEX

PROGRAM test
  FOR_INDEX 10 12 1
    PUSH I

PUSH_PROGRAM test
EVAL
Try in Playground →

Supported by

Unruggable Logo