> For the complete documentation index, see [llms.txt](https://whs3-doge5k.gitbook.io/smartcontract-security-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whs3-doge5k.gitbook.io/smartcontract-security-docs/basics/3.-evm/evm-4.md).

# EVM 명령어

EVM은 일반 컴퓨터와 달리 스택만으로 작동한다. 즉, 레지스터나 변수를 사용하지 않는다. 스택은 최대 1024개의 항목을 담을 수 있으며 각 항목은 256비트 크기의 단어로 구성되어있다.

EVM은 스마트 계약 바이트 코드를 opcode 단위로 해석하고, 트랜잭션이 발생할 때마다 휘발성 메모리에서 계산한 후, 영구 저장소에 상태를 반영한다.                                                                                        &#x20;

[<mark style="background-color:blue;">출처</mark>](https://ethereum.org/en/developers/docs/evm/#evm-instructions)

<figure><img src="https://2462457462-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLf9vwRuaws92ULgGvzTi%2Fuploads%2FHxGQ5vcUSRBh2VkLqInQ%2Fimage.png?alt=media&amp;token=5bbc1183-b076-4b71-923e-160559f85147" alt=""><figcaption><p><a href="https://ethereum.org/en/developers/docs/evm/">https://ethereum.org/en/developers/docs/evm/</a></p></figcaption></figure>

***

### 예시

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// 1 + 2의 결과를 리턴하는 Simple 컨트랙트
contract Simple {
    function add() public pure returns (uint) {
        return 1 + 2;
    }
}

```

```nasm
PUSH1 0x01      // 스택에 1 올리기
PUSH1 0x02      // 스택에 2 올리기
ADD             // 스택에서 두 개 꺼내 더한 뒤 다시 스택에 넣기
PUSH1 0x20      // 값을 메모리 위치 지정 (예: 0x20)
MSTORE          // 계산 결과(3)를 메모리에 저장
PUSH1 0x20      // 리턴할 길이 (32바이트)
PUSH1 0x00      // 리턴할 위치 (0번부터)
RETURN          // 메모리에서 값을 꺼내 반환
```

각 Opcode를 실행할 때마다 가스를 소모한다. 각 연산에 따라 책정되는 가스비는 달라지며, 단순한 명령은 적은 가스를 쓰지만 저장/외부 호출과 같은 코드는 가스를 많이 사용한다. 이 때, 가스가 부족하면 실행 도중 실패처리가 되며 EVM은 모든 상태를 트랜잭션 이전으로 되돌린다.
