Learn EVM Opcodes II

Solidity Programming Language
4 min readMar 9, 2023

--

Learn EVM Opcodes II

It’s time to learn what the basic opcodes are. If you learn these opcodes, you can dominate the long bytecode sequences. You would like to understand ADD, SUB, POP,DIV and MUL opcodes. Welcome ! Each opcode will be shown both visually and in code implementation.

What is the LIFO ?

First of all, you should keep this information in mind. EVM Stack works with LIFO method. Last in First out.

For example, We add first2” value and add “3” value and add “4” value.

“4" will be first value which is executing by Stack.

Let’s say; respectively

PUSH1 2

PUSH1 3

PUSH1 4

values to EVM.

Execute it

Now let’s look at the reflection of the values we send on the stack. We sent lastly “4” value. You see that “4” value will be the top of the stack.

Understanding LIFO

This LIFO !

If we send an addition operation with ADD opcode after these values, the stack adds the last two variables.

4+3=7

and the stack image looks like this.

Execute it

I hope that you understand LIFO concept on EVM stack .

ADD opcode

You understood the ADD opcode.

At this point, we see the values 7 and 2 on the stack.

We have 7 and 2 in the stack

Let’s remove one value from the stack with the help of POP opcode. Could you guess which value will disappear?

Remember the motto ! Last in first out (LIFO) !

Execute it

It can be seen that the last value “7” on the stack is removed.

POP opcode

You understood the POP opcode.

Let’s send 2 more values to EVM. The first value is 1 byte sized value and the second value is 2 byte sized value. PUSH1 is used for 1 byte sized values and PUSH2 is used for 2 byte sized values. These values are as follows:

PUSH1 255

PUSH2 65535

Execute it

The decimal values we send to stack are processed as hexadecimal on the stack.

We add 2 values to the stack

You can easily see the decimal-hexadecimal changes from the following source.Click.

Let’s do a multiplication (MUL) and a subtraction (SUB) operation respectively.

MUL

SUB

In the Multiplication(MUL) operation, we performed the multiplication(MUL) operation using the last two values (ffff and ff) available on the stack.

You can verify the multiplication operation from the following source. Click

Execute it

Result is feff01!

MUL opcode

You understood the MUL opcode.

In the subtraction (SUB) operation, we performed the subtraction (SUB) operation using the last two values (feff01 and 2) on the stack. You can verify the subtraction operation from the following source.Click

Execute it

Result is fefeff!

SUB opcode

You understood the SUB opcode.

We add new different sized value.

PUSH4 4294967295

Also we add DIV operator. We will use this operator for division.

EXECUTE IT

Result is 101.

DIV opcode

You understood the DIV opcode.

ALL OPCODES

Thank you for listening to me.

Associate Professor Engin YILMAZ (VeriDelisi)

--

--

Solidity Programming Language

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