Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Bytecode and Opcode of Solidity

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to understand Solidity's Bytecode and Opcode. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Bytecode and Opcode of Solidity

As we go deeper into writing smart contracts, we will encounter terms such as "PUSH1", "SSTORE", "CALLVALUE" and so on. What are they and when should we use them?

To understand these commands, we must learn more about the Ethernet Fong Virtual Machine (EVM). This article will try to explain some of the basics of EVM as simply as possible. I hope you all get something.

Like many other popular programming languages, Solidity is a high-level programming language. We can read it, but the machine can't. If you have learned programming languages such as java,c++, it should be easy to understand this.

When we install Ethernet Square clients such as geth, it also comes with the Ethernet Fong virtual machine, a lightweight operating system created specifically to run smart contracts.

When we compile Solidity code using the solc compiler, it converts the code into bytecode that only EVM can understand.

Let's take a very simple contract as an example:

Pragma solidity ^ 0.4.26 position contract OpcodeContract {uint I = (10 + 2) * 2;}

If we run this code in a remix browser and then click on the contract details, we will see a lot of information.

In this case, the compiled code is:

BYTECODE {"linkReferences": {}, "object": "60806040526018600055348015601457600080fd5b5060358060226000396000f3006080604052600080fd00a165627a7a72305820db1d567e501f1682876df36eea80a02d25a8b2adb186da705e2e98e134b08cc60029", "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x18 PUSH1 0x0 SSTORE CALLVALUE DUP1 ISZERO PUSH1 0x14 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x35 DUP1 PUSH1 0x22 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 0xdb SAR JUMP PUSH31 0x501F1682876DF36EEA80A02D25A8B2ADB186DA705E2E98E134B08CC6002900", "sourceMap": "25 60806040526018600055348015601457600080fd5b5060358060226000396000f3006080604052600080fd00a165627a7a72305820db1d567e501f1682876df36eea80a02d25a8b2adb186da705e2e98e134b08cc60029", "540", "54", "54", "54", "54", "54", "5", "25", "25", "54", "54", "5", "25", "25", "54", "5", "25", "5", "5", "25", "5", "5", "5", "5", "5", "5", "5", "5". 30WR 1x 27th th 20V 12 x 5V 2x 25R 54R 0; "}

Where object is the compiled code. They are the hexadecimal representation of the final contract, also known as bytecode.

Under the "Web3 Deploy" section of the remix browser, we see:

Var opcodecontractContract = web3.eth.contract ([]); var opcodecontract = opcodecontractContract.new ({from: web3.eth.accounts [0], data: '0x60806040526018600055348015601457600080fd5b50603580226000396000f3006080604052600080fd00a165627a7a7305820db1d567e501f1682876df36eea80a02d25a8b2adb186da705e2e98e134b08cc60029bread, gas:' 4700000'}, function (e, contract) {console.log (e, contract); if (typeof contract.address! = = 'undefined') {console.log (Contract mined! address:' contract.address + transactionHash:'+ contract.transactionHash);})

Simply put, this means that when we deploy the contract, we need to pass the compiled hexadecimal code as data, and the recommended gas is 4700000.

Anything that starts with "0x" indicates that the value is in hexadecimal format. The "0x" before hexadecimal is not mandatory because EVM treats any value as hexadecimal.

We also saw the operation code (also known as Opcode):

"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x18 PUSH1 0x0 SSTORE CALLVALUE DUP1 ISZERO PUSH1 0x14 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x35 DUP1 PUSH1 0x22 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 0xdb SAR JUMP PUSH31 0x501F1682876DF36EEA80A02D25A8B2ADB186DA705E2E98E134B08CC6002900"

The opcode is the low-level readable instruction of the program. All opcodes have corresponding hexadecimal values, such as "MSTORE" is "0x52", SSTORE "is" 0x55 "... and so on.

The specific operation code corresponding to the value can refer to the relevant information of Etay Square.

EVM virtual machine is a medium stack virtual machine, the so-called stack is the last-in-first-out structure, in computer science terms, we call it LIFO.

For example, in the above intelligent contract, if we write the equation like this in ordinary arithmetic:

/ / Answer is 14. We do multiplication before addition.10 + 2 * 2

In the EVM stack virtual machine, it works according to the principle of LIFO, so we need to write:

2 2 * 10 +

This means putting "2" on the stack first, then another "2", and then multiplying. The result is "4" at the top of the stack. Now add the number "10" to the top of "4" and finally add the two numbers together. The final value of the stack is 14.

This arithmetic type is called suffix notation.

The action of putting data on the stack is called the "PUSH" instruction, and the action of removing the data from the stack is called the "POP" instruction. Obviously, the most common opcode we saw in the above example is "PUSH1", which means putting 1 byte of data on the stack.

Therefore, this directive:

PUSH1 0x80

Means to put the 1-byte value "0x80" on the stack. The hexadecimal value of "PUSH1" is exactly "0x60". Delete the non-mandatory "0x", and we can write this logic as "6080" in bytecode.

Let's go one step further.

PUSH1 0x80 PUSH1 0x40 MSTORE

Looking at the opcode diagram of Ethernet Square again, we see that MSTORE (0x52) accepts two inputs but does not produce any output. The above opcode says:

PUSH1 (0x60): puts 0x80 on the stack.

PUSH1 (0x40): puts 0x40 on the stack.

MSTORE (0x52): allocates the memory space of 0x80 and moves it to the location of 0x40.

The resulting bytecode is:

6080604052

In fact, at the beginning of any fixed bytecode, we always see the magic number "6080604052" because it is the way smart contracts guide.

Note that 0x40 or 0x60 cannot be interpreted as the real number 40 or 60 here. Because they are hexadecimal, 40 is actually 64 (16 x 4) in decimal, and 80 is 128 (16 x 8) in decimal.

In short, what "PUSH1 0x80 PUSH1 0x40 MSTORE" is doing is allocating 128 bytes of memory and moving the pointer to the beginning of the 64th byte. Now we have 64 bytes for temporary storage and 64 bytes for temporary memory storage.

In EVM, there are three places where data can be stored. First, in the stack, following the example above, we just used the "PUSH" opcode to store data here. Second, in memory that uses the "MSTORE" opcode (RAM), and finally in disk storage that uses "SSTORE" to store data. The gas required to store data on disk is the most expensive, while gas that stores data on the stack is the cheapest.

We sometimes use Assembly Language in our smart contracts in Solidity, and this Assembly Language uses such assembler Opcode to manipulate EVM bytecode. It is difficult for him to understand, but by using it, he can save fuel and do things that cannot be done through Solidity.

The above content is how to understand Solidity's Bytecode and Opcode. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report