Open In App

Introduction to Bytecode and Opcode in Solidity

Improve
Improve
Like Article
Like
Save
Share
Report

Solidity is a high-level programming language and when an Ethereum client like geth is installed, it comes with a lightweight operating system called Ethereum Virtual Machine (EVM) that is specially created to run smart contracts. 

Ethereum Virtual Machine (EVM) is a virtual machine that is a collection of individual, networked machines called nodes that act as a single machine. In EVM, each node runs a client software that implements Ethereum specifications and they form a network. This network of nodes synchronizes its state so that they together form a giant database that is always synchronized. The agreement on the state of the data is achieved through a consensus algorithm and must be achieved across the network of nodes.  When the solidity code is compiled it will be converted to bytecode that only EVM can understand. The article focuses on discussing bytecode and opcode of solidity.

What is Bytecode?

Bytecode is a software computer language for instruction-level programming, in contrast to the machine code level of programming. It is the information that the Solidity code gets translated into. 

  • It is generally compact numeric codes, constants, and other pieces of information. 
  • It contains instructions to the computer in binary form.
  • Each instruction step in bytecode is an operation that is referred to as opcode. 
  • Opcodes are 1-byte long. This is the reason they are called bytecodes i.e. one byte codes.

Every line of code in the smart contract gets converted to opcode so that the computer knows what to do when running our codes. In Ethereum, bytecode is the actual thing that is getting deployed to the Ethereum Blockchain. 
There are many tools and libraries that will help to compile Solidity code to bytecode. One common approach is to compile the solidity code in Remix IDE and copy the bytecode. 

Follow the steps below:

1. Open Remix IDE and write the following smart contract.

Solidity




// Solidity program to show 
// bytecode
pragma solidity ^0.5.0;
  
contract helloGeeks {
 function renderHelloGeeks () public pure returns (string memory) {
   return 'helloGeeks';
 }
}


2. Compile the smart contract helloGeeks.sol.

 

3. Copy the bytecode and paste it into an editor like Wordpad, notepad, etc.

 

Here,

linkReference: deployed address of address of other smart contracts on which the current smart contract has a dependency.

object: Current smart contract bytecode.

opcodes: Operation codes that are human-readable low-level instructions.

sourceMap: ource map is to match each contract instruction to the section of the source code from which it was generated.

What is Opcode?

Opcodes are low-level human-readable instructions of the program that have hexadecimal counterparts. For example, MSTORE is 0x52, etc. To store opcodes efficiently they are encoded into a bytecode. Opcodes can be divided into the following categories:

Category Opcode
Stack Management  POP, PUSH, UP, SWAP
Arithmetic/ Comparison/ Bitwise ADD, SUB, GT, LT, AND, OR.
Environmental CALLER, CALL VALUE, NUMBER
Opcodes that control memory LOAD, STORE, MSSTORE 8, MSIZE
Memory Management LOAD, STORE
Opcodes related to the program counter JUMP, JUMPTO, JUMPIF, JUMPSUB, JUMPSUBV, JUMPDEST
Stop Opcodes STOP, RETURN, REVERT, INVALID, SELF, DESTRUCT.

Conclusion

Solidity code must be compiled to bytecode before being deployed on EVM. This bytecode corresponds to the series of opcodes that EVM can interpret.

Solidity -> Byte Code -> Opcode

Source code: Source code is a file in programming language like Solidity, Java.

Bytecode: Bytecode are obtained from source code and run on a virtual machine such as EVM.

Machine code: Byte code is converted to machine code and finally executed and this is the code that operating system can read. 



Last Updated : 03 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads