Understanding EVM Opcodes: A Simple Guide

Understanding EVM Opcodes: A Simple Guide

In the Ethereum Virtual Machine (EVM), opcodes are fundamental instructions that the virtual machine uses to execute smart contracts and manage transactions. Each opcode performs a specific operation, such as arithmetic, data manipulation, or control flow. Here’s a simplified guide to some common EVM opcodes:

1. PUSH1-PUSH32
- Pushes a constant value (1 to 32 bytes) onto the stack.

2. POP
- Removes the top item from the stack.

3. ADD
- Adds the top two numbers on the stack and pushes the result.

4. MUL
- Multiplies the top two numbers on the stack and pushes the result.

5. SUB
- Subtracts the second number from the top number and pushes the result.

6. DIV
- Divides the second number from the top number and pushes the result.

7. SDIV
- Performs signed division of the top two numbers and pushes the result.

8. MOD
- Calculates the remainder of the division of the top two numbers and pushes the result.

9. SMOD
- Computes the signed remainder of the division of the top two numbers and pushes the result.

10. ADDMOD
- Adds the top two numbers, then takes the modulo of the result by a third number.

11. MULMOD
- Multiplies the top two numbers, then takes the modulo of the result by a third number.

12. EXP
- Raises the top number to the power of the second number and pushes the result.

13. SIGNEXTEND
- Extends the sign of a number.

14. LT
- Compares the top two numbers; pushes 1 if the second is less than the top, otherwise 0.

15. GT
- Compares the top two numbers; pushes 1 if the second is greater than the top, otherwise 0.

16. EQ
- Compares the top two numbers; pushes 1 if they are equal, otherwise 0.

17. ISZERO
- Checks if the top number is zero; pushes 1 if true, otherwise 0.

18. AND
- Performs a bitwise AND operation on the top two numbers and pushes the result.

19. OR
- Performs a bitwise OR operation on the top two numbers and pushes the result.

20. XOR
- Performs a bitwise XOR operation on the top two numbers and pushes the result.

21. NOT
- Performs a bitwise NOT operation on the top number and pushes the result.

22. BYTE
- Extracts a byte from the top number based on the index provided by the second number.

23. SHA3
- Computes the Keccak-256 hash of the data at the top of the stack.

24. ADDRESS
- Pushes the address of the current contract onto the stack.

25. BALANCE
- Pushes the balance of the given address onto the stack.

26. ORIGIN
- Pushes the address of the transaction origin (external account) onto the stack.

27. CALLER
- Pushes the address of the caller (who initiated the call) onto the stack.

28. CALLVALUE
- Pushes the value (in wei) sent with the call onto the stack.

29. CALLDATALOAD
- Loads a specific 32-byte word from the call data.

30. CALLDATASIZE
- Pushes the size of the call data onto the stack.

31. CALLDATACOPY
- Copies call data to memory.

32. CODESIZE
- Pushes the size of the contract code onto the stack.

33. CODECOPY
- Copies contract code to memory.

34. GASPRICE
- Pushes the current gas price onto the stack.

35. EXTCODESIZE
- Pushes the size of the code at a given address.

36. EXTCODECOPY
- Copies code from a given address to memory.

37. RETURNDATASIZE
- Pushes the size of the return data from the last call onto the stack.

38. RETURNDATACOPY
- Copies return data from the last call to memory.

39. EXTCODEHASH
- Computes the Keccak-256 hash of the code at a given address.

40. BLOCKHASH
- Pushes the hash of a block at a given index.

41. COINBASE
- Pushes the coinbase (miner’s address) of the current block.

42. TIMESTAMP
- Pushes the timestamp of the current block.

43. NUMBER
- Pushes the number of the current block.

44. DIFFICULTY
- Pushes the difficulty of the current block.

45. GASLIMIT
- Pushes the gas limit of the current block.

46. CHAINID
- Pushes the chain ID of the current network.

47. SELFBALANCE
- Pushes the balance of the current contract.

48. BASEFEE
- Pushes the base fee per gas of the current block.

49. SLOAD
- Loads a value from contract storage.

50. SSTORE
- Stores a value in contract storage.

51. LOG0-LOG4
- Emits log entries with up to four topics.

52. CREATE
- Creates a new contract with a specified amount of gas, code, and value.

53. CALL
- Calls another contract with specified gas, value, and inputs.

54. CALLCODE
- Calls another contract with specified gas and inputs, but retains the context of the current contract.

55. DELEGATECALL
- Calls another contract with specified gas and inputs, retaining the context of the current contract.

56. STATICCALL
- Calls another contract without modifying state.

57. REVERT
- Reverts the current transaction and optionally provides an error message.

58. INVALID
- An invalid opcode that causes an exception when executed.

59. SELFDESTRUCT
- Destroys the contract and sends its balance to a specified address.