Skip to content

Changing constant variable in inline assembly #1744

Discussion options

You must be logged in to vote

I got it.

According to docs

The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value.

In this case :

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract changeConstant {
    uint256 private constant v = 5;
    function increment() public {
        assembly{   
            sstore(v, add(sload(v), 1))
        }
    }
    function getValue() public view returns(uint256 result){
        assembly {
            result := sload(v)
        }
    }
}

When
sstore(v, add(sload(v), 1)) is run, solidity compiler converts it into sstore(5, add(sload(5), 1)), and storage is an array, so it read / write in storage array at i…

Replies: 3 comments 13 replies

Comment options

You must be logged in to vote
3 replies
@Muhammad-Abdullah012
Comment options

@Muhammad-Abdullah012
Comment options

@krakxn
Comment options

Comment options

You must be logged in to vote
8 replies
@Muhammad-Abdullah012
Comment options

@Muhammad-Abdullah012
Comment options

@Muhammad-Abdullah012
Comment options

@alymurtazamemon
Comment options

@Muhammad-Abdullah012
Comment options

Comment options

You must be logged in to vote
2 replies
@PatrickAlphaC
Comment options

@albatros-github
Comment options

Answer selected by Muhammad-Abdullah012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants