Solidity – Assembly

Solidity - Assembly

Solidity provides an option to use assembly language to write inline assembly within Solidity source code. We can also write a standalone assembly code which then be converted to bytecode. Standalone Assembly is an intermediate language for a Solidity compiler and it converts the Solidity code into a Standalone Assembly and then to byte code. We can used the same language used in Inline Assembly to write code in a Standalone assembly.

Inline Assembly

Inline assembly code can be interleaved within Solidity code base to have more fine-grain control over EVM and is used especially while writing the library functions.

An assembly code is written under assembly { … } block.

Example

Try the following code to understand how a Library works in Solidity.

pragma solidity ^0.5.0;

library Sum {   
   function sumUsingInlineAssembly(uint[] memory _data) public pure returns (uint o_sum) {
      for (uint i = 0; i < _data.length; ++i) {
         assembly {
            o_sum := add(o_sum, mload(add(add(_data, 0x20), mul(i, 0x20))))
         }
      }
   }
}
contract Test {
   uint[] data;
   
   constructor() public {
      data.push(1);
      data.push(2);
      data.push(3);
      data.push(4);
      data.push(5);
   }
   function sum() external view returns(uint){      
      return Sum.sumUsingInlineAssembly(data);
   }
}

Run the above program using steps provided in Solidity First Application chapter.

Note − Select Test from dropdown before clicking the deploy button.

Output

0: uint256: 15

To know more, Click Here.

This Post Has 5 Comments

  1. ikaria juice

    One thing I’d really like to say is that often car insurance cancellations is a feared experience and if you’re doing the proper things as being a driver you won’t get one. A number of people do obtain the notice that they’ve been officially dumped by the insurance company and many have to fight to get further insurance after the cancellation. Low-priced auto insurance rates are usually hard to get after a cancellation. Having the main reasons concerning the auto insurance canceling can help drivers prevent burning off one of the most critical privileges available. Thanks for the suggestions shared via your blog.

Leave a Reply