How to use Remix Debugger to Learn Solidity Assembly

David Kulman
3 min readJul 19, 2022

--

Hi, in this article, I will show you how to use the remix debugger to help you understand how assembly works.

To use the remix debugger, go to https://remix.ethereum.org/ and write some simple contract.

For me, it will be box.sol written in assembly that we created in the last article here: https://medium.com/@kulman.david/solidity-assembly-by-example-part-1-b58d6de9c1cd

Here is the code:

If you want to copy and paste it into remix, here is the link to my Github from where you can copy it: https://github.com/Kuly14/Solidity_Assembly_Resources/blob/main/contracts/Box.sol

Now compile and deploy the contract.

And call the store function with some uint in my case 22.

After the transaction is mined, you can click on the green checkmark and open the transaction details. It should look something like this:

Here we can see all the essential details about the transaction.

  1. The input: 22
  2. The output: Nothing in this case
  3. The logs: Emitted event, in this case, the NewValue Event

But this still doesn't show us what the EVM does when executing the transaction. To get more info click on the big blue Debug button.

This will open up the debugger. It should look something like this:

This is much better. Here we can see all the data about the transaction. From what is on the stack, what is stored in the memory, you can even simulate the transaction opcode by opcode by either moving the blue bar or pressing the different arrows.

What was most helpful to me, though, was the memory layout. If you scroll down a little, you should see this:

It may look different if you are at a different time in the transaction. This picture is after the transaction completes, so when the blue bar is all the way to the right.

Ok, so in the picture, we can see some great info.

The first 2 slots are just zeros because they are reserved as a scratch space for hashing operations. To learn more about memory layout here is link to the docs: https://docs.soliditylang.org/en/v0.8.15/internals/layout_in_memory.html

The third slot starting from 0x40 and ending at 0x50, is storing number 80. Keep in mind that all these values are in HEX, so this is number 128 in decimal.

This is a unique number called the free memory pointer. The number essentially points to the free memory. I won't go in-depth in this article, but I will write a separate one on this topic.

Then at the memory address, 0x80 is stored number 16 in HEX, so number 22 in decimal. This is the same number I gave as an argument to the function.

So to recap, we used the opcode mstore(0x80, newValue) to store newValue at memory address 0x80. And as we can see, that's precisely what happened.

This is an elementary example, but as you can see, it is very helpful when trying to understand how memory works. When using the assembly, you have to be sure that you are not overwriting any critical data, which can easily happen if you make mistakes.

This debugger can help in making sure the function behaves as intended. It is also a great tool to learn how the EVM works opcode by opcode since you can simulate the transaction that way.

I hope this article has been helpful. Thanks for reading!

--

--