Learn EVM Opcodes I

Solidity Programming Language
4 min readMar 9, 2023

--

Learn EVM opcodes

Would you like to learn what bytecode and opcode are ? Welcome ! If you want to improve yourself in the field of security in Solidity language, you should definitely learn the opcodes. In this serie, I will try to explain the basics of opcodes in detail.

What is bytecode in EVM?

Like many other popular programming languages, Solidity is a high-level programming language. When we compile Solidity code using the solc compiler, it will translate our code into bytecode, something that only the EVM can understand. In short, bytecode is the machine language code structure that results from code written in Solidity and converted by the solc compiler into code that the EVM can understand.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract StorageContract {
uint public x=2;

When you compile-deploy this code on remix, click the Debug tab, go to the input line and click copy.

You will get the following result. This is bytecode.

0x6080604052600260005534801561001557600080fd5b5060b3806100246000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80630c55699c14602d575b600080fd5b60336047565b604051603e91906064565b60405180910390f35b60005481565b6000819050919050565b605e81604d565b82525050565b6000602082019050607760008301846057565b9291505056fea264697066735822122040d72790712387380cc9d75024fcfa2d40ba3ec182cd49afa48224043d7f3d6f64736f6c63430008130033

How to Analyze the Bytecode?

a-Remix

I think you understand the subject a bit more with the remix image above. You can see the details of the input line seen under the Debug tab in more detail with the left side Debugger (you need to install it in remix). By clicking on the button I circled in the image above, you can watch how the byte view of our code changes at each step.

b-evm.codes

We can also run the bytecode on another platform, evm.codes. Notice that we will remove the “0x” at the beginning of the bytecode and paste it on the screen.

After doing this, by pressing the “Run” button I have circled and pressing the button I have squared at the top right, you will watch how the code realizes the process from the following memory, stack, storage(The first 3 text areas are in below) and return (the last area is in below) screens.

c-bytegraph.xyz

We can run the bytecode on bytegraph.xyz. Notice that we will remove the “0x” at the beginning of the bytecode and paste it on the screen.

Afterwards, the “Bytecode” button circled on the screen will be selected and the analysis will continue as follows.

First(1) click on the airplane sign (execute button) in the left menu. Clicking on this will open a menu on the right side. From here you can click on the step button and watch the process unfold in sequence. To see the progress of these steps in more detail, click on the file button (Instruction Info) in the left menu (2).

What is Opcode ?

Opcodes are instructions inside bytecodes.

Let’s remember the first part of the bytecode.

6080604052

Here 60 actually refers to the PUSH1 opcode.

60 or PUSH1 opcode tells us that contract adds 1 byte sized value to the stack. (When you add the 2 byte sized value, use need to use PUSH2 or 61)

[ 6080 ]

In the first operation, we load the value 80 into the Stack with the opcode 60 or PUSH1.

[ 6040 ]

In the second operation, we load the value 40 into the Stack with the code 60 or PUSH1.

[ 52 ]

In the third operation, we add the values previously added to the Stack to the memory with code 52 or MSTORE.

You can try to execute only this small byte code in evm.codes. I prepare this code for you and you only click this for executing.

Associate Professor Engin YILMAZ (VeriDelisi)

--

--

Solidity Programming Language
Solidity Programming Language

Written by Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps

No responses yet