---
# System prepended metadata

title: 'EVM, a very brief introduction'

---

# EVM, a very brief introduction
###### tags: rsk iov evm solidity 

Here you'll find what we saw in the EVM class, plus some more relevant data :)

*you can also check the [slides](https://docs.google.com/presentation/d/1TZpZzTe4ReHWLMKdsSyz7IHZSdxcvUk2/edit?usp=sharing&ouid=107797593205448052911&rtpof=true&sd=true) and the [recorded session](https://drive.google.com/file/d/1ABYxJdFvmYmI9knASFyacJudt9ch__3G/view?usp=sharing)*

---

## VMs

In computing, a virtual machine (VM) is the virtualization/emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve specialized hardware, software, or a combination. Virtual machines differ and are organized by their function, as shown here:

- **System virtual machines** (also termed full virtualization VMs) provide a substitute for a real machine. They provide functionality needed to execute entire operating systems.
- **Process virtual machines** are designed to execute computer programs in a platform-independent environment.
    
*to read more about VMs you can go [here](https://en.wikipedia.org/wiki/Virtual_machine) or [here](https://hackmd.io/@v1d/S1MD7Xu2s)*

## EVM

While rootstock has btc as a native cryptocurrency (rbtc) that follows almost the same intuitive rules, it also enables a much more powerful function: smart contracts. For this more complex feature, a more sophisticated analogy is required. Instead of a distributed ledger, rsk is a distributed [state machine](https://en.wikipedia.org/wiki/Finite-state_machine). RSK's state is a large data structure that holds not only all accounts and balances but a machine state, which can change from block to block according to a pre-defined set of rules, and which can execute arbitrary machine code. The EVM defines the specific rules of changing state from block to block.

### Architecture

The Ethereum Virtual Machine (EVM) is a Turing-complete virtual machine that executes code on any EVM-compatible network. It is designed to be sandboxed, meaning that it provides a secure environment for executing smart contracts.

The EVM architecture consists of:

- A stack-based instruction set to process data and execute code
- A memory area for storing data temporarily during the execution
- A storage area for permanent data storage of the contract
- A set of registers for storing values used in computation (not accessible)
- An instruction pointer that tracks the execution of code

![](https://i.imgur.com/UgCjBYM.png)

The EVM executes bytecode, a compiled form of smart contract code that the EVM can interpret. The EVM runs on each node in the network, ensuring that all nodes execute the same code and produce the same results. This enables the decentralized nature of the network and ensures the integrity of the state of the blockchain.

#### Stack

The [stack](https://www.geeksforgeeks.org/stack-data-structure/) in the EVM is a temporary data storage area that is used to hold intermediate values during computation. The stack is a last-in, first-out (LIFO) data structure, meaning that the last value pushed onto the stack is the first value that will be popped off.

Access to the stack is performed through a set of stack operations, such as PUSH and POP, which allow values to be pushed onto the stack and popped off the stack, respectively. Contract code can use these operations to store intermediate values during computation and manipulate the values stored on the stack.

#### Memory

The memory area is a more general-purpose storage area that is used to store larger amounts of data. Memory can be read from and written to by contract code, and its size can dynamically grow as needed. The memory area is slower to access than registers, but it is much more flexible.

#### Registers

The registers in the EVM are used to store temporary values during the execution of a contract. Unlike the memory area, registers are much faster to access, but they have a limited size. Registers are not directly accessible to contract code, but they are used by the EVM to perform computations and manipulate data stored in memory.

##### Memory vs Register

The EVM uses registers for fast computation and memory for general-purpose data storage. The choice between using registers or memory will depend on the contract's specific requirements, such as the amount of data that needs to be stored and the speed at which it needs to be accessed.

#### Storage Area 

The storage area is a key-value store, where each contract has its own storage space, and each storage space can be accessed using a unique key.

It's where smart contracts store their data that needs to persist beyond the lifetime of a single transaction. The storage area is persistent, meaning that data stored in the storage area will remain even after the contract has finished executing. This is important because smart contracts need to be able to persist data across multiple transactions, such as the current balance of an account in a decentralized exchange.

We say that the storage area is also transparent, meaning that the data stored in this area is visible to all participants in the network. This is important for ensuring the transparency and accountability of smart contracts.

In summary, the storage area of the EVM provides a persistent and transparent storage solution for smart contracts, allowing them to store data that needs to persist beyond the lifetime of a single transaction.

### Gas and gas fees

Gas interacts with the EVM by serving as a means of paying for computation on the network. When a transaction or smart contract execution is initiated, the sender must specify the maximum amount of gas they are willing to pay for the computation. The EVM then calculates the actual amount of gas consumed by executing the operation and charges the sender accordingly.

Suppose the amount of gas specified is not enough to cover the computational effort required to execute the operation. In that case, the EVM will halt execution and refund any unused gas to the sender. If the amount of gas specified is more than what's needed, the excess gas is not refunded but rather goes to the miner who includes the transaction in a block.

Gas serves as a mechanism for determining the cost of computation on the rsk network and is used by the EVM to execute operations within the smart contracts and ensure that infinite or excessively complex operations do not overload the network.

## Useful Links

- [Mastering Ethereum, The Ethereum Virtual Machine.](https://github.com/ethereumbook/ethereumbook/blob/develop/13evm.asciidoc)
- [Ethereum official documentation.](https://ethereum.org/en/developers/docs/evm/)
- [EVM Illustrated.](https://takenobu-hs.github.io/downloads/ethereum_evm_illustrated.pdf)
- [Getting deep into EVM.](https://medium.com/swlh/getting-deep-into-evm-how-ethereum-works-backstage-ab6ad9c0d0bf)
- [This Twitter thread.](https://twitter.com/SalomonCrypto/status/1555684780921892864)
- [Inevitableeth.](https://inevitableeth.com/en/home/ethereum/evm)


### EVM Tools

- [geth-tools](https://geth.ethereum.org/downloads) comes with a cli-tool to play with geth's evm.
- [solc](https://docs.soliditylang.org/en/v0.8.14/installing-solidity.html) solidity compiler.
- [Remix IDE](https://remix.ethereum.org/) a very known online IDE, with a nice debugging tool and lots of useful stuff.
- [Mythril](https://github.com/ConsenSys/mythril) a security analysis tool for EVM bytecode.
- [pyevmasm](https://github.com/crytic/pyevmasm) a python-based EVM disassembler and assembler.