Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stack Execution

The AVM approves a program execution if it ends with:

  • A single non-zero value on top of the stack,

The AVM rejects a program execution if it ends with:

  • A single zero value on top of the stack,
  • Multiple values on top of the stack,
  • No value on top of the stack,

Or in case of run-time errors.

A Simple TEAL Program

Let’s consider the following TEAL program:

#pragma version X

// Macros
#define compareAndReturn ==; return

// Program
int 1; int 2; +;
int 3;
compareAndReturn

The TEAL program above, although minimal, showcases most of the features of the AVM assembly language:

  1. The first line (#pragma version X) directs the assembler to generate bytecode targeting a specific AVM version,

  2. The // prefixes a line comment,

  3. The #define directive is used to define TEAL Macros,

  4. The // Program section lists the opcode instructions of the TEAL program:

    • TEAL supports the Reverse Polish notation (RPN),
    • TEAL lines may end with a newline \n or ;.

For a complete description of the AVM instruction set, refer to the TEAL normative specification.

A Simple TEAL Execution

The AVM bytecode, resulting from the TEAL source code assembly and compilation, is executed on the stack.

Suppose we want the AVM to approve a transaction if the following condition is true:

$$ 1 + 2 = 3 $$

This would be an insecure program, since its approval condition is a tautology, which would approve any transaction regardless of the execution context.

The following illustrations show the program execution step-by-step.

Stack