﻿{"id":835236,"date":"2026-02-18T10:24:26","date_gmt":"2026-02-18T10:24:26","guid":{"rendered":"https:\/\/gridnet.org\/wpp\/?p=835236"},"modified":"2026-04-05T16:41:18","modified_gmt":"2026-04-05T16:41:18","slug":"gridscript-the-language-of-the-decentralized-state-machine-a-comprehensive-guide","status":"publish","type":"post","link":"https:\/\/mag.gridnet.org\/index.php\/2026\/02\/18\/gridscript-the-language-of-the-decentralized-state-machine-a-comprehensive-guide\/","title":{"rendered":"GridScript: The Language of the Decentralized State Machine \u2014 A Comprehensive Guide"},"content":{"rendered":"<h2>Preface \u2014 The Language That Became an Operating System<\/h2>\n<p>In 1970, Charles H. Moore created Forth \u2014 a programming language so minimal, so close to the metal, that it could run on hardware with 8 kilobytes of memory. Forth was not elegant in the way that Lisp was elegant, nor structured in the way Pascal was structured. It was something else entirely: a language where the programmer thought in terms of a stack, where every operation consumed values and produced values, where the boundary between the language and the machine dissolved into nothing. NASA used Forth to control spacecraft. Telescope operators used it to point instruments at distant galaxies. It was the language of people who needed absolute control over every byte.<\/p>\n<p>Half a century later, a different kind of machine needed a language with those same properties \u2014 not a spacecraft, but a decentralized state machine. A machine that does not exist in any single location, that is maintained by independent operators across the globe, that must execute every instruction identically on every node, and that must do so with mathematical certainty. This machine is <strong>GRIDNET OS<\/strong> \u2014 the world&#8217;s first decentralized operating system. And its language is <strong>GridScript<\/strong>.<\/p>\n<p>GridScript is derived from Forth, but it is not Forth. Where Forth was designed to control hardware, GridScript was designed to control consensus. Where Forth communicated with serial ports and memory-mapped I\/O, GridScript communicates with a Merkle Patricia Trie holding the global state of a decentralized network. Where Forth programs ran on a single processor, GridScript programs are replicated and executed across every node in the network \u2014 deterministically, verifiably, and irreversibly. And where Forth execution is essentially free \u2014 instructions run as fast as the processor allows with no accounting \u2014 every single GridScript instruction consumes <strong>ERG<\/strong> (Execution Resource Gas), making it a metered computation model where unbounded execution is impossible by design. This single difference transforms GridScript from a language into an economic mechanism: computation has a price, and that price prevents denial-of-service attacks against the network.<\/p>\n<p>GridScript serves simultaneously as:<\/p>\n<ul>\n<li><strong>The transaction language<\/strong> \u2014 every blockchain transaction is a GridScript program compiled to bytecode<\/li>\n<li><strong>The smart contract language<\/strong> \u2014 decentralized applications are written, deployed, and invoked in GridScript<\/li>\n<li><strong>The system shell<\/strong> \u2014 operators manage GRIDNET Core nodes through an interactive GridScript terminal<\/li>\n<li><strong>The consensus language<\/strong> \u2014 all nodes execute the same GridScript deterministically to reach agreement on state<\/li>\n<li><strong>The query language<\/strong> \u2014 all data retrieval from the decentralized state machine is performed through GridScript execution<\/li>\n<\/ul>\n<p>This article is a comprehensive guide to GridScript \u2014 its architecture, its execution model, its role as the backbone of a decentralized service-oriented architecture, and its practical use in building applications that run without servers, without central authorities, and without trust in any single entity.<\/p>\n<h2>I. Decentralized Processing Threads \u2014 The Cornerstone of Everything<\/h2>\n<p>If you understand only one concept in GridScript, let it be this: <strong>Decentralized Processing Threads (DPTs)<\/strong>. They are the mechanism through which every interaction with GRIDNET OS occurs \u2014 every value transfer, every smart contract call, every data query, every file operation. DPTs are to GRIDNET OS what HTTP requests are to the web, except they execute Turing-complete code inside a metered virtual machine on a decentralized network.<\/p>\n<h3>BT and CT \u2014 Begin Thread, Commit Thread<\/h3>\n<p>The two most important commands in GridScript are <code>BT<\/code> (Begin Thread) and <code>CT<\/code> (Commit Thread). Together, they define the lifecycle of a transaction:<\/p>\n<pre>BT\ncd \/YourDomainAddress\nsend RecipientAddress 1000000000000000000\nCT<\/pre>\n<p>When <code>BT<\/code> executes, it creates a new sub-thread \u2014 a sandbox where subsequent GridScript commands are <strong>accumulated but not executed<\/strong>. The commands are recorded into a code buffer. Inline commands like <code>send<\/code> are automatically converted to their explicit stack-based forms (<code>sendEx<\/code>) for serialization. The thread tracks its own ERG (Execution Resource Gas) consumption.<\/p>\n<p>When <code>CT<\/code> executes, it collects code from all ready threads, compiles the accumulated GridScript into bytecode, signs the transaction with the user&#8217;s private key, and submits it to the network. The transaction then propagates to all nodes, where it is re-executed in kernel mode \u2014 deterministically, identically, on every machine \u2014 and the resulting state changes are committed to the global Merkle Patricia Trie.<\/p>\n<h3>Ephemeral vs. Committed \u2014 The Two Modes of DPTs<\/h3>\n<p>Not all DPTs result in blockchain transactions. This distinction is fundamental:<\/p>\n<p><strong>Committed DPTs<\/strong> follow the full <code>BT \u2192 accumulate \u2192 CT<\/code> cycle. They produce signed transactions that modify the global state. A value transfer, a smart contract deployment, an identity registration \u2014 these are committed DPTs.<\/p>\n<p><strong>Ephemeral DPTs<\/strong> execute GridScript on a remote GRIDNET Core node <em>without committing anything to the blockchain<\/em>. They are read-only queries against the current state of the decentralized state machine. When the Blockchain Explorer displays a list of recent transactions, it is using ephemeral DPTs. When the Wallet dApp retrieves an account balance, it is using an ephemeral DPT. The GridScript executes, reads from the state trie, produces a BER-encoded result, and the thread is discarded. No transaction is signed. No bytecode is stored. No ERG is charged to the user&#8217;s account.<\/p>\n<p>This dual nature \u2014 the same mechanism serving both state-modifying transactions and read-only queries \u2014 is what makes DPTs the cornerstone of the entire GRIDNET OS architecture. Every UI dApp, every data retrieval operation, every interaction that a user has with the decentralized state machine flows through a DPT.<\/p>\n<h3>Multi-Thread Transactions<\/h3>\n<p>GridScript supports formulating up to five threads simultaneously (<code>MAX_THREADS_COUNT = 5<\/code>), enabling atomic multi-operation transactions:<\/p>\n<pre>BT\ncd \/DomainA\nsend Recipient1 1000000000000000000\nBT\ncd \/DomainB\nsend Recipient2 2000000000000000000\nCT      \\ Commits all ready threads as one atomic transaction<\/pre>\n<p>Thread management commands provide fine-grained control: <code>RT<\/code> (Resume Thread), <code>ST<\/code> (Suspend Thread), <code>PT<\/code> (Print Thread \u2014 show accumulated code), <code>FT<\/code> (Focus Thread \u2014 switch between threads), and <code>AT<\/code> (Abort Thread \u2014 discard accumulated code).<\/p>\n<h2>II. GridScript as a Decentralized Service-Oriented Architecture<\/h2>\n<p>Stop and consider what is actually happening when a user opens the Wallet UI dApp or the Blockchain Explorer in their browser. Every balance displayed. Every transaction listed. Every block detail rendered. Every identity resolved. None of it comes from a traditional server. There is no PHP backend. There is no .NET API. There is no REST endpoint hosted in a data center. Every single piece of data the user sees on screen is retrieved through <strong>GridScript code executing on remote GRIDNET Core nodes<\/strong> \u2014 decentralized machines operated independently across the network.<\/p>\n<p>This is the <strong>GridScript Service-Oriented Architecture (SOA)<\/strong>, and it is what makes the entire GRIDNET OS dApp ecosystem work.<\/p>\n<h3>The Data Flow Pipeline<\/h3>\n<p>The SOA pipeline operates in six stages:<\/p>\n<ol>\n<li><strong>JavaScript formulates the request<\/strong> \u2014 Browser-side code in <code>VMContext.js<\/code> (which exports the <code>CVMContext<\/code> singleton) constructs a GridScript data request. This might be a balance query, a transaction search, a block listing, or a domain lookup.<\/li>\n<li><strong>BER encoding<\/strong> \u2014 The request is serialized using BER (Basic Encoding Rules) \u2014 an efficient binary encoding format derived from ASN.1. BER is not JSON. It is not Protocol Buffers. It is a compact, self-describing binary format that can represent complex nested data structures with minimal overhead. Web Workers perform the encoding off the main thread to keep the UI responsive.<\/li>\n<li><strong>Double-encrypted transport<\/strong> \u2014 The BER-encoded request is wrapped in ECC (Elliptic Curve Cryptography) encryption at the application level, then transmitted through a TLS-secured WebSocket. Two independent encryption layers. An attacker who compromised TLS would still face a second cryptographic barrier.<\/li>\n<li><strong>GridScript VM execution<\/strong> \u2014 The remote GRIDNET Core node receives the request, decodes it, and executes the corresponding GridScript handler. The VM traverses the Merkle Patricia Trie \u2014 the global state database \u2014 reading account data, transaction histories, block headers, identity tokens, or whatever the request demands.<\/li>\n<li><strong>BER-encoded response<\/strong> \u2014 The results are packaged into structured BER metadata \u2014 organized into typed sections (<code>directoryListing<\/code>, <code>searchResults<\/code>, <code>notifications<\/code>, <code>transactionInfo<\/code>, <code>blockInfo<\/code>, <code>domainInfo<\/code>) containing typed entries. This structured format enables rich data exchange between the VM and the browser.<\/li>\n<li><strong>CVMContext receives and dispatches<\/strong> \u2014 The BER-encoded response arrives back through the same double-encrypted, onion-routed channel. The in-browser GRIDNET OS subsystem \u2014 the <strong>CVMContext singleton<\/strong> \u2014 receives the raw data, decodes it using <code>CVMMetaParser<\/code>, and dispatches it through two distinct mechanisms:\n<ul>\n<li><strong>Active waiting (request-response)<\/strong> \u2014 A UI dApp that initiated a specific data request is <em>actively awaiting<\/em> the response. It holds a pending Promise or async callback keyed to the request ID it generated. When <code>CVMContext<\/code> encounters that request ID in the incoming BER data, it resolves the waiting call directly \u2014 the dApp receives exactly the data it asked for. This is the primary pattern for queries: the Wallet asks for a balance, the Explorer asks for a block list, and each <code>await<\/code>s the specific response.<\/li>\n<li><strong>Passive notification (event-driven)<\/strong> \u2014 UI dApps may also register general-purpose event listeners for broad categories of data \u2014 new block announcements, VM state changes, commit status transitions, DFS updates, incoming network messages. These listeners fire whenever <code>CVMContext<\/code> receives matching data, regardless of whether the dApp initiated the request. A new block notification may reach both the Explorer and the Wallet simultaneously; a commit confirmation may notify multiple dApps that share a transaction context. The dApp does not ask for this data \u2014 it arrives because the dApp has expressed interest in a category of events.<\/li>\n<\/ul>\n<p>This dual dispatch model \u2014 active request-response correlation <em>and<\/em> passive event subscription \u2014 operating simultaneously through the same <code>CVMContext<\/code> singleton is what gives GRIDNET OS UI dApps both the responsiveness of traditional client-server applications and the real-time reactivity of event-driven systems.<\/li>\n<li><strong>UI dApp renders<\/strong> \u2014 Whether actively awaiting a response or passively notified of an event, the UI dApp instance receives the decoded data, processes it according to its own logic, and updates its Shadow DOM. The user sees data on screen and has no idea that what just happened bears no resemblance to a conventional HTTP request-response cycle.<\/li>\n<\/ol>\n<figure><img decoding=\"async\" src=\"https:\/\/talk.gridnet.org\/uploads\/default\/original\/1X\/6b57acbe8452a614c4fa11086e1020d5505dc0c9.png\" alt=\"GridScript VM Decentralized SOA Architecture\" style=\"width:100%; max-width:860px;\"><figcaption>The GridScript SOA pipeline \u2014 from browser-side JavaScript through double-encrypted transport to remote GridScript VM execution, with BER-encoded responses flowing back through CVMContext to individual UI dApp instances.<\/figcaption><\/figure>\n<h3>Performance That Defies Expectations<\/h3>\n<p>The measured balance update latency \u2014 the round-trip time from browser request to rendered result through this entire double-encrypted, BER-encoded pipeline \u2014 is <strong>20\u201345 milliseconds on a local network<\/strong> (WAN latency varies with node proximity and network hops). This is on par with, and in many cases faster than, conventional centralized web frameworks querying a local database.<\/p>\n<p>This performance is possible because BER encoding is remarkably efficient for the structured data GRIDNET OS works with. Unlike JSON, which carries field names as human-readable strings in every message, BER uses numeric tags and length prefixes, producing payloads that are significantly smaller. Unlike Protocol Buffers, BER requires no schema compilation step \u2014 the encoding is self-describing. The VM Meta-Data Protocol builds on BER with a Sections \u2192 Entries structure, where each section has a typed purpose and each entry carries a request ID for correlation \u2014 enabling multiple concurrent asynchronous requests to be in flight simultaneously.<\/p>\n<h3>How dApps Use This Architecture<\/h3>\n<p>The Blockchain Explorer UI dApp provides a vivid example. When a user navigates to the Blocks view, the Explorer&#8217;s JavaScript constructs a GridScript request to fetch recent blocks with sorting and filtering parameters. The request is BER-encoded, dispatched through the encrypted WebSocket, and routed to a GRIDNET Core node. On that node, the GridScript VM executes a stateful iterator \u2014 a cursor that traverses the block chain with pagination support, filter matching, and sort optimization. The VM enhanced this capability with stateful iterators that remember position between calls, eliminating the need to re-traverse from the beginning for each page of results. The results \u2014 block headers with height, timestamp, transaction count, miner ID, difficulty, reward \u2014 are BER-encoded into a <code>searchResults<\/code> section and returned. The browser decodes and renders a Tabulator data grid with cyberpunk aesthetics.<\/p>\n<h3>The BER Meta-Data Protocol<\/h3>\n<p>Communication between browser and GRIDNET Core follows a structured protocol built on BER encoding:<\/p>\n<pre>\/\/ JavaScript side \u2014 constructing a request\nconst generator = new CVMMetaGenerator();\ngenerator.addRAWGridScriptCmd(gridScriptCode, requestID, processID, vmID);\nconst berData = generator.finalize();\n\/\/ Send through encrypted WebSocket...<\/pre>\n<p><strong>Architecture reference (GRIDNET Core internal):<\/strong><\/p>\n<pre>\/\/ On the GRIDNET Core side \u2014 C++ processing\nCVMMetaGenerator gen;\ngen.beginSection(eVMMetaSectionType::searchResults);\ngen.addTransactionInfo(txDesc, reqID, appID, vmID);\ngen.endSection();\ngen.finalize();\nvector&lt;uint8_t&gt; data = gen.getData();<\/pre>\n<p>The protocol supports dozens of entry types: <code>GridScriptCode<\/code>, <code>terminalData<\/code>, <code>transactionInfo<\/code>, <code>blockInfo<\/code>, <code>domainInfo<\/code>, <code>searchResults<\/code>, <code>fileContent<\/code>, <code>stateLessChannelElement<\/code>, and many more. Each entry carries metadata for request-response correlation, enabling the browser to match responses to the specific UI component that initiated the request.<\/p>\n<p>The three-tier data hierarchy \u2014 Level 1 (raw transaction\/receipt on-chain data), Level 2 (core info containers), Level 3 (BER-compatible description objects for transactions, blocks, and domains) \u2014 ensures that the same data structures are serialized identically in C++ and JavaScript, achieving full parity between the native Core implementation and the browser-side ECMA6 objects.<\/p>\n<h2>III. Where GridScript Runs \u2014 The Six Execution Contexts<\/h2>\n<p>GridScript is not confined to a single execution environment. It runs in six distinct contexts, each with different capabilities and security restrictions:<\/p>\n<h3>1. SSH \u2014 Direct Connection to GRIDNET Core<\/h3>\n<p>The most direct way to interact with GridScript. Connect via SSH to a running GRIDNET Core node and you have a full interactive terminal \u2014 the Forth heritage made tangible. Type commands, watch the stack, formulate transactions, manage the node. Every command in the GridScript vocabulary is available, subject only to authentication level.<\/p>\n<pre>\\ SSH session example\n10 20 + .          \\ prints 30\n.\" Hello, GRIDNET!\" cr\nbalance 'MyDomain'\n.                  \\ prints balance in attoGNC<\/pre>\n<h3>2. Local Instance \u2014 Ctrl+E in GRIDNET Core<\/h3>\n<p>When running GRIDNET Core directly, pressing Ctrl+E switches from the Events View to the Terminal View \u2014 an embedded GridScript console. This provides the same capabilities as SSH but with physical access to the machine. Certain administrative commands (like <code>shutdown<\/code> or <code>firewall<\/code>) may require this local context.<\/p>\n<h3>3. Terminal UI dApp \u2014 Browser-Based Terminal<\/h3>\n<p>The Terminal dApp brings GridScript to the browser. Built on xterm.js, it provides a full terminal experience within the GRIDNET OS desktop environment. The Terminal registers for VM metadata callbacks, DFS message events, and GridScript result notifications through <code>CVMContext<\/code> \u2014 the same SOA pipeline used by all dApps. Commands entered in the browser terminal are transmitted to a GRIDNET Core node for execution, with results streamed back in real-time.<\/p>\n<h3>4. Smart Contract Execution \u2014 On-Chain Bytecode<\/h3>\n<p>When a transaction is committed, its GridScript is compiled to bytecode and executed in <strong>kernel mode<\/strong> across all network nodes. Kernel mode is the consensus context \u2014 fully deterministic, no terminal I\/O, no file system access, no user interaction. The VM checks <code>REG_KERNEL_THREAD<\/code> to enforce these restrictions. Smart contracts deployed via <code>BC<\/code>\/<code>EC<\/code> (Begin Code \/ End Code) are stored as bytecode in the state trie:<\/p>\n<pre>BT\ncd \/YourDomain\/contracts\nBC\ncd \/YourDomain\ngetVar 'counter'\n1 +\n0 \"counter\" setVarEx\nEC\nCT<\/pre>\n<p>The bytecode format uses sequential opcode assignment starting from <code>BASE_OPCODE_ID = 9<\/code>. Single-byte encoding for opcodes \u2264 127, two-byte encoding for higher values. The order of codewords is <strong>immutable<\/strong> \u2014 new commands are always appended at the end. Reordering would break every compiled contract on the blockchain.<\/p>\n<h3>5. Implicit via JavaScript APIs \u2014 VMContext.js<\/h3>\n<p>This is the SOA context described in the previous section. JavaScript code in the browser generates GridScript automatically \u2014 the developer may not even realize GridScript is being executed. When a dApp calls a balance query or transaction search through <code>CVMContext<\/code>, the JavaScript constructs GridScript data requests, BER-encodes them, and dispatches them to remote nodes.<\/p>\n<h3>6. User Interactions with UI dApps<\/h3>\n<p>Drag a file in the File Manager. Send GNC in the Wallet. Deploy an identity token. Every user action in a UI dApp is ultimately formulated as GridScript \u2014 making it reproducible across the entire decentralized system once committed. The Wallet dApp pioneered this architecture: JavaScript ECMA6 formulates GridScript instruction sequences, Web Workers perform BER encoding and GridScript compilation and ECC signing off the main thread, and compiled bytecode is dispatched through onion-routed encrypted WebSocket channels.<\/p>\n<h3>The Security Model \u2014 Permission Layers in Depth<\/h3>\n<p>Every one of GridScript&#8217;s 312+ codewords carries <strong>18 security properties<\/strong> that are checked at runtime before the instruction executes. This is not a coarse-grained permission system \u2014 it is per-instruction, per-context security enforcement.<\/p>\n<h4>Permission Levels<\/h4>\n<p>From least restrictive to most restrictive:<\/p>\n<p><strong>Level 1: Public Commands<\/strong> \u2014 Arithmetic (<code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code>), stack operations (<code>dup<\/code>, <code>drop<\/code>, <code>swap<\/code>), comparison, and logic. These execute in any context \u2014 kernel mode, terminal, GUI, smart contract. No restrictions.<\/p>\n<p><strong>Level 2: Terminal-Only Commands<\/strong> (<code>onlyFromTerminal = true<\/code>) \u2014 <code>BT<\/code>, <code>CT<\/code>, <code>keygen<\/code>, <code>BC<\/code>\/<code>EC<\/code>, <code>setKey<\/code>, node configuration. These <strong>cannot<\/strong> be called from kernel mode during consensus execution. If a smart contract attempted to call <code>BT<\/code>, the VM would reject it \u2014 you cannot formulate a transaction from within a transaction.<\/p>\n<p><strong>Level 3: Local Admin Commands<\/strong> (<code>requiresLocalAdminCredentials = true<\/code>) \u2014 <code>shutdown<\/code>, <code>firewall<\/code>. These require physical or local access to the machine and administrator authentication via <code>sudo<\/code>.<\/p>\n<p><strong>Level 4: Overwatch Commands<\/strong> (<code>allowedOnlyByAnOverwatch = true<\/code>) \u2014 Critical system modifications that only the network&#8217;s highest-privilege entities (overwatches) can perform. The VM checks <code>REG_EXECUTING_BY_OVERWATCH<\/code> (register 16).<\/p>\n<p><strong>Level 5: Kernel-Restricted Commands<\/strong> (<code>allowedInKernelMode = false<\/code>) \u2014 File I\/O, <code>erg<\/code> management, network operations, blockchain queries like <code>getChain<\/code>, <code>stepBack<\/code>. These are <strong>forbidden during consensus execution<\/strong> because they would break determinism.<\/p>\n<h4>Security Registers \u2014 Identity and Privilege<\/h4>\n<p>GridScript maintains 54 special-purpose registers (indices 0\u201453). Several are critical for security:<\/p>\n<p><strong><code>REG_CALLERS_ID<\/code> (register 11) vs <code>REG_AUTHENTICATED_SD<\/code> (register 12)<\/strong> \u2014 <code>REG_CALLERS_ID<\/code> identifies <strong>who signed and paid<\/strong> for the transaction. <code>REG_AUTHENTICATED_SD<\/code> identifies <strong>who has permission<\/strong> to execute the current operation. These can differ during delegation, proxy execution, or sponsored transactions.<\/p>\n<pre>\\ Inside a smart contract \u2014 check both:\n11 getReg     \\ Read REG_CALLERS_ID \u2014 who SIGNED the transaction\n12 getReg     \\ Read REG_AUTHENTICATED_SD \u2014 who HAS PERMISSION\nfcomp         \\ Compare them\n\\ If different: delegation or proxy execution is occurring<\/pre>\n<p><strong>\u26a0 <code>REG_AUTHENTICATED_SD<\/code> must be cleared after a dApp exits<\/strong> \u2014 failure to do so causes privilege leakage.<\/p>\n<p><strong><code>REG_KERNEL_THREAD<\/code> (register 34)<\/strong> \u2014 The kernel mode flag. When set, execution is occurring as part of consensus. Only kernel threads can modify decentralized state. This register <strong>is read-only<\/strong> and set internally by the VM.<\/p>\n<h4>Complete Register Reference \u2014 All 54 Registers (0\u201453)<\/h4>\n<p>GridScript maintains 54 special-purpose registers accessible via the <code>getReg<\/code> codeword. Each register stores state critical to the operation of the Decentralized State Machine.<\/p>\n<table style=\"width:100%; border-collapse:collapse; margin:1em 0;\">\n<tr>\n<th style=\"padding:6px; border:1px solid #555; background:#1a1a2e; width:40px;\">#<\/th>\n<th style=\"padding:6px; border:1px solid #555; background:#1a1a2e; width:220px;\">Name<\/th>\n<th style=\"padding:6px; border:1px solid #555; background:#1a1a2e;\">Description<\/th>\n<th style=\"padding:6px; border:1px solid #555; background:#1a1a2e; width:80px;\">Category<\/th>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">0<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXEC_SD<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Executing State Domain \u2013 current domain context affecting data access scope<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">1<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_DATA_TYPE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Data Type Indicator for the most recent operation result<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Context<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">2<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_SIGN<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Sign flag from the most recent arithmetic operation<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Context<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">3<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CURRENT_SD<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Current state domain \u2013 active domain for state operations (set by <code>cd<\/code>)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Context<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">4<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CURRENT_DIR<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Current directory path within the state domain (DFS navigation)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Context<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">5<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_THROW_ON_FAILURE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">When set, operations throw runtime errors on failure instead of returning error codes<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Error<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">6<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_PRIV_KEY<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Active private key for signing operations (terminal session)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Crypto<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">7<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_RECEIPT_ID<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Receipt ID of the current transaction (set after commit)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Crypto<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">8<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_BASE58_DEBUG_VIEW<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">When set, <code>.s<\/code> and <code>.m<\/code> display pointer values in base58 encoding<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Debug<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">9<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_MAKING_SACRIFICE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: a sacrifice (staking) operation is in progress<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">10<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXECUTING_CODE_ID<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Identifier of the currently executing code bundle (smart contract path)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">System<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">11<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CALLERS_ID<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><strong>Critical:<\/strong> Identity of the entity that <em>signed<\/em> and paid for the transaction.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">12<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_AUTHENTICATED_SD<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><strong>Critical:<\/strong> Identity of the entity with <em>permission<\/em> to execute the current operation.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">13<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_DAPP_INC_BALANCE_CHANGE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Incremental balance change caused by the current dApp execution<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">14<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_DOING_COLON<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: currently inside a colon definition (<code>: name ... ;<\/code>)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Exec State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">15<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_SACRIFICED_VALUE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">The GNC amount sacrificed (staked) in the current transaction<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">16<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXECUTING_BY_OVERWATCH<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><strong>Security:<\/strong> When set, grants highest privilege level (Overwatch restricted).<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">17<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_TALKING_TO<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Target entity for messaging operations<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">System<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">18<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_APP_RUNNING<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: a dApp (smart contract) is currently executing<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">System<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">19<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXTERNAL_DATA<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">External data attached to the current execution context<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">System<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">20<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_REQ_APP_ABORT<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Request abort of the currently running dApp from external source<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">System<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">21<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_SUPPRESS_THROW<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Suppresses throw on the very next instruction. Auto-clears after one instruction.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Error<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">22<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_SUPPRESS_DFS_THROW<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Suppresses throw on the next DFS operation. Auto-clears after next DFS call.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Error<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">23<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_COMMIT_PENDING<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: a transaction commit is pending network confirmation<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Control<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">24<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_THREAD_READY<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: thread\/transaction formulation is finished and ready for commitment<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Control<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">25<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_THREAD_PAUSED<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: the current thread is paused (via <code>ST<\/code>)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Control<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">26<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXECUTING_INSTRUCTION<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Currently executing instruction identifier<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Exec State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">27<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXECUTING_PROGRAM<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Currently executing program\/word identifier<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Exec State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">28<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_HIGH_PRECISION<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Enables high-precision arithmetic mode and display formatting.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Output<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">29<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_LOGGED_IN_AS<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Identity of the currently logged-in user (set by <code>logmein<\/code>)<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">30<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_WAS_SANDBOX_PRE_AUTHED<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Per-command sandbox pre-authorization flag<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Security<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">31<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_IS_THREAD_UI_AWARE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><strong>Security:<\/strong> Indicates if thread can perform UI operations (Read-Only).<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">32<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_IS_TEXT_OUTPUT_ENABLED<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Controls whether text output operations produce visible output<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Threading<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">33<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_HAS_DETACHED_THREAD<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Indicates whether a detached native thread is attached to VM thread<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Threading<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">34<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_KERNEL_THREAD<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><strong>Critical:<\/strong> Kernel mode flag (Read-Only). Execution is part of consensus.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Critical<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">35<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_DO_NOT_COMPILE_READS<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">When set, data-read operations are excluded from bytecode compilation<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Compilation<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">36<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_NEW_LINE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Internal newline tracking for output formatting<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Threading<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">37<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_PP_MODE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Flag: executing within a GridScript++ (<code>evalGPP<\/code>) context<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Exec State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">38<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_ABORT<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Abort signal from GridScript++ context<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Control<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">39<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_LAST_ERROR<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Contains the error message from the most recent failure<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Error<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">40<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CURRENT_CODE_PATH<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">File path of the currently executing code<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Context<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">41<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_ASSETS_RECEIVED_FROM<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Identity of the entity that sent assets (GNC) to the current context<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">42<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_PRESERVE_RECENT_CALL<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Instructions leading to current dApp invocation remain in compiled source<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Compilation<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">43<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_IMPLICIT_CALL_THREAD_BEGAN<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Internal: implicit contract call has been initiated<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Control<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">44<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CALLERS_ID_BEFORE_DAPP_CALLED<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Saved identity before dApp invocation. Restored on exit.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Security<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">45<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_EXCUSE_ERG_USAGE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Bypasses ERG checks during block replay (Trusted Internal Only).<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">ERG<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">46<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_KERNEL_ERG_BID<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">ERG bid (price per ERG unit). Set via <code>erg -setbid<\/code>.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">ERG<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">47<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_KERNEL_ERG_LIMIT<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Max ERG allowed for transaction. Set via <code>erg -setlimit<\/code>.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">ERG<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">48<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_KERNEL_NONCE<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Manual nonce override. Allows enqueueing transactions.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Threading<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">49<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><em>(Reserved)<\/em><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Internal structural reserve<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">50<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CUMULATIVE_TRANSFER_GNC<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Running total of GNC transferred in current transaction bundle.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">51<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_CUMULATIVE_TT_CASHOUT_GNC<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Running total of TT cashout value in this transaction.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">52<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_ALL_GNC_RECIPIENTS<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Array of recipient-value pairs for current transaction.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:6px; border:1px solid #555;\">53<\/td>\n<td style=\"padding:6px; border:1px solid #555;\"><code>REG_ALL_TT_POOL_IDS<\/code><\/td>\n<td style=\"padding:6px; border:1px solid #555;\">Set of unique Token Pool IDs involved in current transaction.<\/td>\n<td style=\"padding:6px; border:1px solid #555;\">State<\/td>\n<\/tr>\n<\/table>\n<h3>Contract Debugging with REG_PRESERVE_RECENT_CALL<\/h3>\n<p>Debugging decentralized code is notoriously difficult. GridScript addresses this via <strong>Register 42 (REG_PRESERVE_RECENT_CALL)<\/strong>. When this register is enabled (set to 1), the VM includes the instructions that <em>preceded<\/em> the <code>call<\/code> or <code>callEx<\/code> codeword in the final compiled bytecode. This allows for full stack-trace reconstruction in the event of a contract failure, as the parameters passed to the contract remain part of the signed transaction record rather than being discarded after execution. This is a critical tool for developers during the &#8220;Magic Button&#8221; (\u27ea\u27ea\u27ea) debugging phase, ensuring that opaque contract errors can be traced back to their specific input state.<\/p>\n<h2>IV. Stack-Based Fundamentals \u2014 The Forth Heritage<\/h2>\n<p>If you have never used a stack-based language, the mental model is simple:<\/p>\n<ol>\n<li><strong>Everything is a word<\/strong> \u2014 commands, numbers, operators are all &#8220;words&#8221;<\/li>\n<li><strong>Numbers push themselves<\/strong> onto the stack<\/li>\n<li><strong>Words consume and produce<\/strong> stack values<\/li>\n<li><strong>No parentheses needed<\/strong> \u2014 execution order is left-to-right, stack-driven<\/li>\n<\/ol>\n<pre>10 20 +     \\ Push 10, push 20, add \u2192 stack contains 30\n5 *         \\ Push 5, multiply \u2192 stack contains 150\n.           \\ Print top of stack \u2192 outputs \"150\"<\/pre>\n<h3>The Three Stacks<\/h3>\n<p>GridScript maintains three stacks:<\/p>\n<p><strong>Data Stack (dStack)<\/strong> \u2014 The primary operand stack. Maximum depth: 256. This is where you push numbers, pointers, addresses, and where operations consume and produce results.<\/p>\n<p><strong>Return Stack (rStack)<\/strong> \u2014 Used for temporary storage and loop control. Loop constructs (<code>DO...LOOP<\/code>) use the return stack to track loop indices.<\/p>\n<p><strong>Meta Stack<\/strong> \u2014 Parallel to the data stack, tracking type information (integer, pointer, string, BigInt) for runtime type checking and debugging. Each value on the data stack has a corresponding type descriptor on the meta stack.<\/p>\n<h3>Stack Manipulation<\/h3>\n<pre>dup         \\ ( a -- a a )        Duplicate top\nswap        \\ ( a b -- b a )      Swap top two\nover        \\ ( a b -- a b a )    Copy second to top\nrot         \\ ( a b c -- b c a )  Rotate top three\ndrop        \\ ( a -- )            Remove top\npick        \\ ( ... u -- ... xu ) Copy item at depth u\nroll        \\ ( ... u -- ... )    Move item at depth u to top\ndepth       \\ ( -- n )            Current stack depth<\/pre>\n<h3>Control Flow \u2014 Conditionals, Loops, and Word Definitions<\/h3>\n<p>GridScript inherits Forth&#8217;s complete control flow vocabulary. All branching constructs compile to <code>(zbranch)<\/code> and <code>(branch)<\/code> opcodes at compile time.<\/p>\n<h4>Conditionals \u2014 IF&#8230;THEN&#8230;ELSE<\/h4>\n<pre>\\ IF...THEN (basic conditional)\n: is-positive ( n -- )\n    0 > IF\n        .\" Positive!\" cr\n    THEN\n;\n5 is-positive        \\ prints: Positive!\n\n\\ IF...ELSE...THEN\n: abs ( n -- |n| )\n    dup 0 < IF\n        -1 *\n    ELSE\n        \\ already positive, do nothing\n    THEN\n;\n-7 abs .             \\ prints: 7<\/pre>\n<h4>Counted Loops \u2014 DO...LOOP and DO...+LOOP<\/h4>\n<pre>\\ DO...LOOP \u2014 iterates from start to limit-1\n: count-to ( n -- )\n    0 DO\n        i .          \\ 'i' pushes current loop index\n    LOOP cr\n;\n5 count-to           \\ prints: 0 1 2 3 4<\/pre>\n<h4>Indefinite Loops \u2014 BEGIN...UNTIL and BEGIN...WHILE...REPEAT<\/h4>\n<pre>\\ BEGIN...UNTIL (post-test \u2014 always executes at least once)\n: countdown ( n -- )\n    BEGIN\n        dup . 1 -\n        dup 0 =\n    UNTIL drop cr\n;<\/pre>\n<h3>V. Command Reference \u2014 The GridScript Vocabulary<\/h3>\n<p>GridScript&#8217;s vocabulary encompasses 312 codewords. Each entry shows the command name, its stack effect notation <code>( before -- after )<\/code>, and its purpose.<\/p>\n<h3>Data Input<\/h3>\n<ul>\n<li><code>data64<\/code> <code>( -- ptr )<\/code> \u2014 Push base64Check-decoded binary data onto the stack<\/li>\n<li><code>data<\/code> <code>( -- ptr )<\/code> \u2014 Push UTF-8 string data onto the stack<\/li>\n<li><code>data58<\/code> <code>( -- ptr )<\/code> \u2014 Push base58Check-decoded binary data (e.g. addresses)<\/li>\n<\/ul>\n<h3>Assertions<\/h3>\n<ul>\n<li><code>assert<\/code> <code>( flag -- )<\/code> \u2014 Abort execution if flag is false (0)<\/li>\n<li><code>assert0<\/code> <code>( flag -- )<\/code> \u2014 Abort execution if flag is true (nonzero)<\/li>\n<li><code>anz<\/code> <code>( n -- n )<\/code> \u2014 Assert non-zero: aborts execution if <code>n<\/code> is zero<\/li>\n<\/ul>\n<h3>Memory Access<\/h3>\n<ul>\n<li><code>!<\/code> <code>( value addr -- )<\/code> \u2014 Store cell value at address<\/li>\n<li><code>@<\/code> <code>( addr -- value )<\/code> \u2014 Fetch cell value from address<\/li>\n<li><code>alloc<\/code> <code>( size -- addr ior )<\/code> \u2014 Allocate memory block (1 ERG per byte)<\/li>\n<li><code>free<\/code> <code>( addr -- )<\/code> \u2014 Free previously allocated memory<\/li>\n<\/ul>\n<h3>Arithmetic & Logic<\/h3>\n<ul>\n<li><code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code>, <code>mod<\/code> \u2014 Standard arithmetic operations<\/li>\n<li><code><<\/code>, <code>=<\/code>, <code>><\/code> \u2014 Basic comparison (returns 1 for true, 0 for false)<\/li>\n<li><code>and<\/code>, <code>or<\/code>, <code>xor<\/code> \u2014 Bitwise logic<\/li>\n<li><code>fcomp<\/code> <code>( ptr1 ptr2 -- flag )<\/code> \u2014 Compare two binary pointers<\/li>\n<\/ul>\n<h3>State & Persistence<\/h3>\n<ul>\n<li><code>cd<\/code> <code>( -- )<\/code> \u2014 Change directory \/ State Domain (inline param: <code>\/path<\/code>)<\/li>\n<li><code>getVar<\/code> <code>( -- ptr )<\/code> \u2014 Read variable state (inline param: <code>name<\/code>)<\/li>\n<li><code>setVarEx<\/code> <code>( value isHidden name -- )<\/code> \u2014 Write persistent state variable<\/li>\n<li><code>ls<\/code> <code>( -- )<\/code> \u2014 List directory\/domain contents<\/li>\n<li><code>write<\/code> <code>( -- )<\/code> \u2014 Write data to DFS file (inline param: <code>filename<\/code>)<\/li>\n<li><code>cat<\/code> <code>( -- ptr )<\/code> \u2014 Read data from DFS file (inline param: <code>filename<\/code>)<\/li>\n<\/ul>\n<h3>Value Transfer (GNC)<\/h3>\n<ul>\n<li><code>send<\/code> <code>( -- )<\/code> \u2014 Send GNC (inline params: <code>address amount<\/code>)<\/li>\n<li><code>sendEx<\/code> <code>( flags target amount -- )<\/code> \u2014 Explicit value transfer<\/li>\n<li><code>balance<\/code> <code>( -- ptr )<\/code> \u2014 Query GNC balance (inline param: <code>address<\/code>)<\/li>\n<li><code>xvalue<\/code> <code>( -- value )<\/code> \u2014 Get GNC amount attached to transaction<\/li>\n<li><code>gotGNC<\/code> <code>( -- flag )<\/code> \u2014 Returns 1 if GNC was received in this context<\/li>\n<\/ul>\n<h3>Transaction & Code<\/h3>\n<ul>\n<li><code>BT<\/code> \u2014 Begin transaction formulation (Accumulation mode)<\/li>\n<li><code>CT<\/code> \u2014 Commit threads (Sign and submit transaction)<\/li>\n<li><code>BC<\/code> \/ <code>EC<\/code> \u2014 Begin\/End Code accumulation (Smart contract deployment)<\/li>\n<li><code>call<\/code> <code>( -- )<\/code> \u2014 Invoke smart contract (inline param: <code>path<\/code>)<\/li>\n<li><code>callEx<\/code> <code>( pathPtr -- )<\/code> \u2014 Invoke contract from pointer<\/li>\n<li><code>evalGPP<\/code> <code>( -- )<\/code> \u2014 Invoke GridScript++ JavaScript engine<\/li>\n<li><code>txconfig<\/code> \u2014 Unified ERG and Nonce configuration utility<\/li>\n<\/ul>\n<h3>Operating System (DFS Management)<\/h3>\n<ul>\n<li><code>mkdir<\/code> <code>( -- )<\/code> \u2014 Create a new directory in the current State Domain (inline param: <code>\/path<\/code>)<\/li>\n<li><code>touch<\/code> <code>( -- )<\/code> \u2014 Create an empty file if it does not exist (inline param: <code>filename<\/code>)<\/li>\n<li><code>rm<\/code> <code>( -- )<\/code> \u2014 Remove a file or directory from the DFS (inline param: <code>path<\/code>)<\/li>\n<li><code>setfacl<\/code> <code>( -- )<\/code> \u2014 Set Access Control List permissions for a path (inline param: <code>path permissions<\/code>)<\/li>\n<li><code>chown<\/code> <code>( -- )<\/code> \u2014 Change the owner identity of a specific path (inline param: <code>path owner<\/code>)<\/li>\n<li><code>poll<\/code> <code>( -- )<\/code> \u2014 Block execution until a state change or network event occurs (EPHEMERAL ONLY)<\/li>\n<\/ul>\n<h3>The <code>context<\/code> Codeword (Remote API Gateway)<\/h3>\n<p>The <code>context<\/code> codeword is the primary interface used by dApps to perform high-level queries against the decentralized state machine. It provides structured results packaged for the <code>CVMContext<\/code> JavaScript API.<\/p>\n<p><strong>Syntax:<\/strong> <code>context -c [subcommand] [options]<\/code><\/p>\n<ul>\n<li><code>-c searchBlockchain<\/code> \u2014 Perform a global search using <code>mkfilter<\/code> objects.<\/li>\n<li><code>-c getBlocks<\/code> \u2014 Fetch paginated block headers.<\/li>\n<li><code>-c getTransactionDetails<\/code> \u2014 Fetch metadata for a transaction ID.<\/li>\n<li><code>-c getDomainDetails<\/code> \u2014 Fetch metadata for a State Domain.<\/li>\n<\/ul>\n<h3>Section V-E. Advanced dApp Integration Patterns<\/h3>\n<p>Professional dApp development on GRIDNET OS leverages the seamless bridge between JavaScript (Control context) and GridScript (Consensus context). Patterns in <code>VMContext.js<\/code> facilitate 'Transaction Chaining,' where multiple <code>BT\/CT<\/code> sequences are formulated based on the results of previous ephemeral calls, all managed by the <code>CVMContext<\/code> singleton for a seamless user experience.<\/p>\n<h4>1. Automated Transaction Formulation<\/h4>\n<p>Modern dApps automate transaction creation using <code>CVMContext<\/code>. The <code>Magic Button<\/code> (\u27ea\u27ea\u27ea) mechanism provides the standard user confirmation flow, ensuring that even complex transaction sequences are presented clearly to the user for final approval.<\/p>\n<pre>\/\/ Programmatic transfer via CVMContext\nasync function commitPayment(to, amountGNC) {\n    const vm = CVMContext.getInstance();\n    const atto = CTools.getInstance().GNCToAtto(amountGNC);\n    \n    \/\/ Formulate the DPT\n    let code = `BT\ntxconfig -setbid 100 -setlimit 50000\nsend ${to} ${atto}\nRT`;\n\n    \/\/ Send to remote node. CVMContext handles Magic Button trigger.\n    return await vm.processGridScriptA(code, vm.getSystemThreadID());\n}<\/pre>\n<h4>2. Identity Proxy Patterns<\/h4>\n<p>Using <code>REG_AUTHENTICATED_SD<\/code> (12), a dApp can execute operations on behalf of a user. The VM ensures that <code>REG_CALLERS_ID<\/code> (signer) remains the authoritative record of who originated the transaction.<\/p>\n<h4>3. Transmission Token Security<\/h4>\n<p>GridScript VM supports two levels for off-chain settlement via Transmission Tokens:<\/p>\n<ul>\n<li><strong>Bearer Mode<\/strong>: Unsigned tokens. Used for faucets.<\/li>\n<li><strong>ECC-Locked Mode<\/strong>: Signed by the pool owner. The <code>XTTEX<\/code> command verifies the signature against the pool owner's registered <code>Identity Token<\/code>.<\/li>\n<\/ul>\n<h2>V-F. Memory Safety & Pointer Internal Logic<\/h2>\n<p>GridScript enforces strict memory safety through internal registration. When <code>alloc<\/code> or internal operators are called, the VM records the memory boundaries. If code attempts to access an invalid pointer or performs an out-of-bounds offset, the VM throws <code>Prohibited memory access<\/code> and rolls back the transaction state immediately.<\/p>\n<h2>V-G. Metadata, Flagging, and Advanced Persistence<\/h2>\n<p>GridScript extends traditional state persistence with a rich metadata layer. This allows developers to attach auxiliary information to state entries without polluting the primary data payload, facilitating a separation between content and categorization.<\/p>\n<h3>Metadata and Flags<\/h3>\n<ul>\n<li><code>setMeta<\/code> <code>( -- )<\/code> \u2014 Attach metadata to the current context (inline params: <code>key value<\/code>)<\/li>\n<li><code>setMetaEx<\/code> <code>( key value isHidden -- )<\/code> \u2014 Explicit metadata assignment with visibility control<\/li>\n<li><code>flag<\/code> <code>( -- )<\/code> \u2014 Set a boolean flag on the current State Domain or object (inline params: <code>name value<\/code>)<\/li>\n<li><code>flagEx<\/code> <code>( flagName value -- )<\/code> \u2014 Explicit flag management<\/li>\n<\/ul>\n<p>This \"ghost state\" is critical for decentralized indexing and UI hints. For example, a file can be flagged as <code>system-critical<\/code> or <code>deprecated<\/code> without changing its binary content. Metadata is indexed by remote nodes, making it searchable via <code>context -c searchBlockchain<\/code> without heavy trie traversal. In <code>VMContext.js<\/code>, these flags are often used to trigger specific UI rendering modes or security warnings before a transaction is finalized by the user.<\/p>\n<h2>VI. Practical Examples \u2014 GridScript in Action<\/h2>\n<h3>Example 1: Full Smart Contract Lifecycle<\/h3>\n<p>This example demonstrates how to initialize state, deploy a contract, and invoke it.<\/p>\n<pre>\\ 1. Initialize State\nBT\ncd \/MyDomain\nsetvar 'counter' '0'\nCT\n\n\\ 2. Deploy Contract\nBT\ncd \/MyDomain\/contracts\nBC\ncd \/MyDomain\ngetVar 'counter' 1 +\n0 \"counter\" setVarEx\nEC\nCT\n\n\\ 3. Invoke Contract\nBT\ncall \/MyDomain\/contracts\nCT<\/pre>\n<h3>A Note on Value Transfer Safety<\/h3>\n<p>As of late 2025, the <code>sendEx<\/code> codeword incorporates a mandatory Treasury Balance Check (enforcing network stability). Any attempt to transfer value that results in a sub-zero State Domain balance (accounting for pending sacrifices) will be rejected by the VM before consensus propagation, saving developer and user ERG.<\/p>\n<h3>Example 2: Payable Smart Contract<\/h3>\n<pre>\\ Inside smart contract code:\ngotGNC IF\n    xvalue                       \\ Pushes BigInt amount\n    11 getReg                    \\ Caller address\n    .\" Received payment from: \" .s\nELSE\n    abort-message \"Payment required\"\nTHEN<\/pre>\n<h2>VII. GridScript in the JavaScript Ecosystem \u2014 The Browser Bridge<\/h2>\n<p>Every GRIDNET UI dApp running in the browser uses <strong>Shadow DOM<\/strong> for isolation. Your application logic lives in an ES6 class extending <code>CWindow<\/code>. Interaction with the blockchain occurs through the <code>CVMContext<\/code> singleton.<\/p>\n<pre>export default class HelloDApp extends CWindow {\n    getPackageID() { return \"org.gridnetproject.UIdApps.Hello\"; }\n    onReady() {\n        const body = this.getBodyElement();\n        body.innerHTML = \"&lt;button id='btn'&gt;Query&lt;\/button&gt;\";\n        body.querySelector(\"#btn\").onclick = async () => {\n            const bal = await this.vmContext.getBalanceA(\"MyAddr\");\n            alert(\"Balance: \" + bal);\n        };\n    }\n}<\/pre>\n<h2>VIII. The ERG System \u2014 Real-time Metering<\/h2>\n<p>Computation in GRIDNET OS is an economic event. Every instruction consumes ERG. The cost per RAM byte (<code>ERG_COST_PER_RAM_BYTE<\/code>) is currently <strong>1 ERG<\/strong>.<\/p>\n<table style=\"width:100%; border-collapse:collapse; margin:1em 0;\">\n<tr>\n<th style=\"padding:8px; border:1px solid #555; background:#1a1a2e; color:#0f0;\">Instruction<\/th>\n<th style=\"padding:8px; border:1px solid #555; background:#1a1a2e; color:#0f0;\">Cost (ERG)<\/th>\n<\/tr>\n<tr>\n<td>Pure Stack Ops (+, dup, drop)<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>State Trie Write (setVarEx)<\/td>\n<td>1 + (data_size \/ 256)<\/td>\n<\/tr>\n<tr>\n<td>Signature Verification (versig)<\/td>\n<td>80<\/td>\n<\/tr>\n<tr>\n<td>ECC Key Generation (keygen)<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>Memory Allocation (alloc)<\/td>\n<td>1 per byte<\/td>\n<\/tr>\n<\/table>\n<h2>IX. GridScript++ \u2014 The Extended Language<\/h2>\n<p>GridScript++ brings JavaScript syntax to the blockchain VM using <code>evalGPP<\/code>. It provides access to the <code>system<\/code>, <code>Assets<\/code>, <code>Filesystem<\/code>, <code>DAO<\/code>, and <code>crypto<\/code> objects for high-level logic. GridScript V2 bytecode introduces a cryptographic <strong>Hash Chain<\/strong> for opcode integrity. Every V2 codeword is verified against a sequence of SHA256 hashes derived from keywords starting with the prefix <code>GRIDSCRIPT_V2_KEYWORD_IMAGE<\/code>. This ensures that the execution engine and the bytecode remain synchronized with mathematical rigor, preventing mid-flight opcode redefinition or injection.<\/p>\n<pre>evalGPP \"\n    var bal = system.assets.balance();\n    if (bal > 1.0) {\n        system.assets.sendGNCFloat('Recipient', 1.5);\n    }\n\"<\/pre>\n<h2>XI. The Power of Reproducible, Verifiable Code<\/h2>\n<p>Every action in GRIDNET OS is a GridScript program. It is compiled to bit-identical V2 bytecode whether on a C++ node or a browser. This ensures that every developer action is <strong>reproducible, committable, and mathematically verifiable<\/strong>.<\/p>\n<hr>\n<p><em>GridScript and GRIDNET OS are created by Rafa\u0142 Skowro\u0144ski. For more information, visit <a href=\"https:\/\/gridnet.org\">gridnet.org<\/a> or join the community at <a href=\"https:\/\/talk.gridnet.org\">talk.gridnet.org<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Preface \u2014 The Language That Became an Operating System In 1970, Charles H. Moore created Forth \u2014 a programming language so minimal,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[162,125,10,17],"tags":[139,145,150,211,147,144,142,143,148,146,210,149],"class_list":["post-835236","post","type-post","status-publish","format-standard","hentry","category-development","category-documentation","category-os-news","category-tutorial","tag-blockchain","tag-decentralized","tag-decentralized-processing-threads","tag-developer-tools","tag-dpt","tag-forth","tag-gridnet-os","tag-gridscript","tag-programming","tag-smart-contracts","tag-state-machine","tag-terminal"],"_links":{"self":[{"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/posts\/835236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/comments?post=835236"}],"version-history":[{"count":54,"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/posts\/835236\/revisions"}],"predecessor-version":[{"id":849015,"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/posts\/835236\/revisions\/849015"}],"wp:attachment":[{"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/media?parent=835236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/categories?post=835236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mag.gridnet.org\/index.php\/wp-json\/wp\/v2\/tags?post=835236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}