Virtual machine compilation analysis of Ethereum source scenario analysis

Source: Internet
Author: User
Development EnvironmentBrowser-solidity is an solidity online web development IDE https://ethereum.github.io/browser-solidity remember to use Chrome browser to open the relevant actions as follows







Contract Compilation Analysis
EVM variable storage is <key (variable), variable_value> storage, the core is the calculation of key, for different types of variables, this key calculation method is not the same
Simple Variables
Contract C {uint256 a = 2; uint256 B = 3;  The corresponding assembly code PUSH1 0x2 PUSH1 0x0 sstore PUSH1 0x3 PUSH1 0x1 sstore The variable's key that is visible in this case is the ordinal p (0, 1, 2, 3, ...) of the variable. In this case, the key = P
fixed-length arrays
Contract D {uint256[6] ar;     function D () {ar[5] = 0X0ABCD; } }
PUSH2 0xABCD PUSH1 0x0 PUSH1 0x5 PUSH1 0x6 DUP2 LT iszero iszero PUSH1 0x6A jumpi INVALID jumpdest ADD DUP2 SWAP1 sstore
PUSH2 0xabcd                        & nbsp [0xABCD] PUSH1 0x0                             [0xABCD, 0x0] PUSH1 0x5                             [0xABCD, 0x0, 0x5] PUSH1 0x6     & nbsp                      [0xABCD, 0x0, 0x5, 0x6]   DUP2 LT iszero iszero PUSH1 0x6A     [0xABCD, 0x0, 0x5, 0x0, 0x6A]//Determine if the array range is exceeded Jumpi INVALID   &nbs P                   &NBSP;[0XABCD, 0x0, 0x5]  jumpdest    & nbsp;                        [ 0xABCD, 0x0, 0x5]  add                               &NBSP;&NBSP;&NBSP;[0XABCD, 0x5] dup2                                  [0xabcd, 0x5, &NBSP;0XABCD] swap1                                 [0xabcd, 0xabcd, 0x5] sstore       & nbsp                    &NBSP;&NBSP;&NBSP;[0XABCD]
Add another assignment when contract D {uint256[6] ar;       function D () {ar[4] = 0x02345;     AR[5] = 0X0ABCD; }} corresponding assembly for PUSH2 0x2345 PUSH1 0x0 PUSH1 0x4 PUSH1 0x6 DUP2 LT iszero iszero PUSH1 0x6A jumpi INVALID jumpdest ADD DUP2 SWA P1 Sstore POP
PUSH2 0xABCD PUSH1 0x0 PUSH1 0x5 PUSH1 0x6 DUP2 LT iszero iszero PUSH1 0x81 jumpi INVALID jumpdest ADD DUP2 SWAP1 sstore
Visible in this case key = P + Index
If our index above is changed to a parameter contract D {uint256[6] ar;     function D (uint256 index) {Ar[index] = 0x0789a; } }
PUSH2 0x789a PUSH1 0x0 DUP3 PUSH1 0x6 DUP2 LT iszero iszero PUSH1 0x7F jumpi INVALID jumpdest ADD DUP2 SWAP1 sstore
Visible index is DUP3 can be obtained, that this DUP3 corresponds to is the parameter, that this parameter how to explain it. is actually the problem of function parameter interpretation
Function name Test PUSH1 0x4 calldatasize LT PUSH1 0x3F jumpi PUSH1 0x0 calldataload PUSH29 0x10000000000000000000000000000000000000000 0000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF and DUP1 PUSH4 0x6e9ed8cf EQ PUSH1 0x44 Jumpi
Gets the first four bytes of the Calldata and functions generated by the hash before their own byte (0X6E9ED8CF) comparison, if the same is a valid call
Get function ArgumentsThen continue to get real parameters POP PUSH1 0x6c PUSH1 0x4 DUP1 calldatasize SUB DUP2 ADD SWAP1 DUP1 DUP1 calldataload SWAP1 PUSH1 0x20 ADD SWAP 1 SWAP3 SWAP2 SWAP1 pop pop pop PUSH1 0x6e jump above is optimized code, below is optimized code PUSH1 0x58 PUSH1 0x4 calldataload PUSH1 0x5A Jump DEST STOP Jumpdest
PUSH1 0x58 [0x58] PUSH1 0x4 [0x58, 0x4] Calldataload [0x58, Arg] PUSH1 0x5A [] Jump jumpdest STOP jumpdest
PUSH1 0x6c                                [0x6C]  PUSH1 0x4                                 [0x6c, 0x4]  DUP1                               nbsp   &NBSP;&NBSP;[0X6C, 0x4, 0x4] calldatasize                     &NB Sp       [0x6c, 0x4, 0x4, 0x36] sub                                       &NBSP;[0X6C, 0x4, 0x32] dup2                   & nbsp;                [0x6c, 0x4, 0x32, 0x4] ADD                                      [0x6c, 0x4, 0x36] SWAP1                                    [0x6c, 0x36, 0x4] DUP1   &N Bsp                               &NBSP;&NBSP;[0X6C , 0x36, 0x4, 0x4] DUP1 calldataload SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 pop pop pop PUSH1 0x6e jump

Map Instance
Contract C {mapping (UINT = UINT) map;     function C () {map[1] = 2; } }
The corresponding assembly is as follows: PUSH1 0x2 PUSH1 0x0 DUP1 PUSH1 0x1 DUP2 mstore PUSH1 0x20 add SWAP1 DUP2 mstore PUSH1 0x20 add PUSH1 0x0 KECCAK25 6 DUP2 SWAP1 Sstore
  push1 2                     [ 0X2]//Right [] for stack PUSH1 0                     [0x2, 0x0] dup1                       [0x2, 0x0, 0x0]    PUSH1 1                      [0x2, 0x0, 0x0, 0x01]      dup2                   [0x2, 0x0, 0x0, 0x01, 0x0]     Mstore [0x2,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.