Update smart contracts 1: Assembly

Inline Assembly

You can interleave Solidity statements with inline assembly in a language close to one of the Ethereum virtual machine. This gives you more fine-grained control, which is especially useful when you are enhancing the language by writing libraries.

You only use assembly if you have no other choice because assembly manipulates directly the memory and it is kind of dangerous.


Let's see a simple example.

Assembly function:  we need to put assembly inside the function. You can manipulate variables declared outside. And also you can declare a local variable.

Everything in the EVM is stored in a slot of 256bits.


Another example. In order to make it easier to see, I have put the code explanation in the comments.


Let's look at 2 other slightly more complicated examples.

The first one: Detect if an address is a smart contract(it can only be done on assembly)

The second one: Cast bytes to bytes32

Subscribe to Hicss
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.