A lot of people have expressed excitement at the potential of using inline assembly in smart contracts. After seeing this comment on reddit I'm curious to see some basic (and more advanced, if you like) examples of how inline assembly may be used to save gas or otherwise augment efficiency.

3 Answers 3


As the author of that module, a few things that came up:

  • Inline assembly allows you to read entire words (256 bits) from data types like string and bytes in a single operation. Solidity-stringutils uses that to do very fast string comparisons by doing subtraction on 32-byte chunks of the two strings being compared. Without assembly, you have to do this byte-by-byte.
  • Some operations aren't exposed in Solidity. For instance, the sha3 opcode takes a byte range in memory to hash, while the Solidity function of the same name takes a string. Thus, hashing part of a string would require costly string copying operations. With inline assembly, you can pass in a string and hash just the bit you care about.
  • Some things are flat out impossible without inline assembly. Solidity doesn't support getting return values from external functions that return variable length types like dynamic arrays, bytes or string, but if you know the length to expect, you can call them using inline assembly.
  • still of the opinion that many of the "libraries" that you are creating can be fully implemented into the regular compiler. Strcat for instance.
    – VoR0220
    Commented Apr 19, 2016 at 18:17
  • 1
    @VoR0220 I don't think anyone said they couldn't, though I'm not sure why you're putting scare quotes around "libraries". If you look at other comparable languages like C, though, you'll notice these things are mostly the responsibility of a standard library. Keeping unnecessary complexity out of the compiler is usually a good idea. Commented Apr 20, 2016 at 9:48
  • And whether or not they could be implemented elsewhere is irrelevant if you need the function and it doesn't yet exist. Commented Apr 20, 2016 at 9:49
  • sorry...the scare quotes were a bad idea :) bad communication. It was late. I think that concatenation of strings for example should work in the compiler as they do already in javascript...makes for an easier scripting language, you know?
    – VoR0220
    Commented Apr 20, 2016 at 18:19
  • 1
    "Some things are flat out impossible without inline assembly" -> I think that this is not as true today as it used to be in 2016. Now we have the RETURNDATASIZE and RETURNDATACOPY opcodes, we can query the code of a contract via <address>code, and much more. Commented Sep 5, 2021 at 15:19

I know this is an old topic, but here's a couple of tutorials I put together to cover assembly in solidity:

Functional Assembly - https://www.youtube.com/watch?v=nkGN6GwkMzU

Instructional Assembly - https://www.youtube.com/watch?v=axZJ2NFMH5Q

I go into the benefits, disadvantages, examples and debugging assembly.


Well for one you can do things that you cannot do with traditional assembly...a perfect example is the new string utils setup per Nick Johnson. https://github.com/Arachnid/solidity-stringutils

The feature is fairly new, but whatever your imagination takes you to and whatever you deem useful, you can do in inline assembly per the requirements of the EVM. There's even something in the docs about it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.