Learn EVM Opcodes IV

Solidity Programming Language
4 min readMar 13, 2023

--

Learn EVM Opcodes IV

We are continuing to progress in opcode business. In this lesson you will learn many new opcodes. These are MOD, EXP, LT,GT, EQ, ISZERO, SHL,SHR and BYTE

MOD Opcode

It gives the remaining value in the division operation on the stack. First we enter the value 2 and then the value 7. With the help of the MOD opcode, we learn the result of the 7%2 operation.

Execute it

ADDMOD Opcode

It is an opcode used with 3 values. The first and second values are summed and the mode of the total result is taken according to the third value.

Like (10+12)%7

MULMOD Opcode

It is an opcode used with 3 values. The first and second values are multiplied and the mode of the total result is taken according to the third value.

Like (10+12)%7

EXP Opcode

The exponential function is one of the most important functions in mathematics. We can use function thanks to the EXP opcode.

Execute it

The 7² (7 to the second power) operation was performed and the result was represented on the stack as hexadecimal(31).

LT Opcode

Returns 1 if the last value on the stack is less than the previous value, 0 otherwise.

PUSH1 5
PUSH1 4
LT

LT Opcode

GT Opcode

Returns 1 if the last value on the stack is more than the previous value, 0 otherwise.

GT Opcode

EQ Opcode

Returns 1 if the last value on the stack is equal to the previous value, 0 otherwise.

EQ Opcode

ISZERO Opcode

Returns 1 if the last value on the stack is equal to 0, 0 otherwise.

ISZERO OPCODE

BYTE Opcode

Consider the data on the stack as an array. With this opcode, we can get data from any point of the array.

Execute it

We wrote 32 byte sized value.

This is: 0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE

The first byte of this data (you must call it 0) is CC.

The last byte of this data (you must call it 31) is EE.

We called byte 27. in this example. This is AA.

Execute it

Would you like to read more ? Let’s get more practice.

PUSH2 0xCCFF
PUSH1 30
BYTE

We wrote a 2 byte sized value : 0xCCFF

Let’s ask it to return whatever is in the 30th byte on this data.

Execute it

The answer is CC.

Why CC, because values are written starting from the far right of each 32 byte slot.

It’s like this;

0x000000000000000000000000000000000000000000000000000000000000CCFF

SHL Opcode

We use it to shift the value to the left in “Bits”.

Execute it

We wrote 32 byte sized value.

0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE

When we shift this data 4 BITS to the left, the first letter “C” representing the first 4 bits disappears and is replaced by “0” at the end (right) of the data.

Execute it

We need to understand this opcode.

Execute it

We wrote 32 byte sized value.

0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE

When we shift this data 8 BITS to the left, the first two letter “CC” representing the first 8 bits disappears and is replaced by “00” at the end (right) of the data.

Execute it

SHR Opcode

We use it to shift the value to the right in “Bits”.

Execute it

We wrote 32 byte sized value.

0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE

When we shift this data 4 BITS to the right, the last letter “E” representing the last 4 bits disappears and is replaced by “0” at the start (left) of the data.

Execute it

You can’t see “0” in the start (left) of the data but there is “0” in the start.

Execute it

We wrote 32 byte sized value.

0xCCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAABBCCDDEE

When we shift this data 8 BITS to the right, the last two letters “EE” representing the last 8 bits disappears and is replaced by “00” at the start (left) of the data.

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