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 first “2” 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.
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.
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.
I hope that you understand LIFO concept on EVM stack .
You understood the ADD opcode.
At this point, we see the values 7 and 2 on 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) !
It can be seen that the last value “7” on the stack is removed.
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
The decimal values we send to stack are processed as hexadecimal on 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
Result is feff01!
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
Result is fefeff!
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.
Result is 101.
You understood the DIV opcode.
Thank you for listening to me.