# Code Execution

The code in AIA Chain contracts is written in a stack-based low-level bytecode language known as **AIA Chain Virtual Machine Code** or **EVM code**. This code consists of a series of bytes, each representing an operation. Code execution typically follows an infinite loop, where the current program counter (starting from zero) repeatedly executes the operation at that position, increments the program counter, and continues until the code completes, an error occurs, or a **STOP** or **RETURN** instruction is encountered.

The operations in the code can access three types of data storage:

* **Stack**: A Last-In-First-Out (LIFO) container where values can be pushed onto and popped from the stack.
* **Memory**: An infinitely expandable byte array.
* **Contract's Persistent Storage**: A key-value store that persists beyond the execution of the code. Unlike stack and memory, which are reset after computation, the storage retains data over time.

The code can also access values from the incoming message, information about the sender, data from the block header, and return a data byte array as output.

The execution model of the AIA Chain virtual machine is remarkably simple. When the AIA Chain virtual machine runs, its complete computational state is defined by a tuple: (block\_state, transaction, message, code, memory, stack, pc, gas), where block\_state stands for the global state containing all accounts, including balances and storage.

At the start of each execution cycle, the virtual machine retrieves the current instruction by accessing the byte at position **pc** in the code (or the 0th byte if **pc >= len(code)**). Each instruction in the code has a specific effect on the tuple. For example:

* The **ADD** instruction will pop two items from the stack, add them, push the result back onto the stack, decrease the gas by 1, and increment the program counter by 1.
* The **SSTORE** instruction will pop the top two items from the stack and store the second item at the index specified by the first item in the contract's storage.

Although there are many ways to optimize AIA Chain virtual machine execution via Just-In-Time (JIT) compilation, the basic implementation of the AIA Chain virtual machine can be achieved with just a few hundred lines of code.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aia-chain-1.gitbook.io/aialabs/technical-framework/core-architecture/code-execution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
