<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ruud Christopher Saimplice</title>
    <description>The latest articles on DEV Community by Ruud Christopher Saimplice (@web3_ruud).</description>
    <link>https://dev.to/web3_ruud</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1026770%2F8588fadd-0ce1-420b-890c-5e18aa045b3c.jpg</url>
      <title>DEV Community: Ruud Christopher Saimplice</title>
      <link>https://dev.to/web3_ruud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/web3_ruud"/>
    <language>en</language>
    <item>
      <title>Assembly x86-64 Linux: Discover the bare Bones !</title>
      <dc:creator>Ruud Christopher Saimplice</dc:creator>
      <pubDate>Tue, 16 May 2023 20:50:54 +0000</pubDate>
      <link>https://dev.to/web3_ruud/assembly-x86-64-linux-discover-the-bare-bones--1p1e</link>
      <guid>https://dev.to/web3_ruud/assembly-x86-64-linux-discover-the-bare-bones--1p1e</guid>
      <description>&lt;p&gt;In this journey, we will dive into the intriguing world of low-level programming, where we uncover the fundamental building blocks that power modern computer systems. As we peel back the layers, we will explore the bare bones of assembly language and witness the direct manipulation of the processor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Assembly(ASM)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Assembly language&lt;/strong&gt; serves as the bridge between &lt;strong&gt;high-level languages&lt;/strong&gt; and &lt;strong&gt;machine code&lt;/strong&gt;, allowing programmers to interact intimately with the hardware. With its concise syntax and fine-grained control over system resources, assembly programming enables optimized performance, deep system understanding, and the ability to overcome complex challenges.&lt;/p&gt;

&lt;p&gt;In this blog series, we will focus specifically on &lt;strong&gt;x86-64 assembly&lt;/strong&gt;, the dominant architecture in modern desktops, laptops, and servers. We will unravel the intricacies of the x86-64 instruction set, learn how to write efficient and concise assembly code, and gain insights into the inner workings of the processor.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Central Processing Unit (CPU)&lt;/strong&gt; of any computer is a microprocessor chip that conducts system functions such as accepting keyboard input and displaying output on the screen.&lt;/p&gt;

&lt;p&gt;Only &lt;strong&gt;machine language instructions&lt;/strong&gt; are understood by the CPU. These are binary sequences of ones and zeros, which are too cryptic for software development. The Assembly language solves this problem by giving symbols that express machine language instructions in an understandable fashion. As a result, assembly is also known as symbolic machine code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of learning Assembly
&lt;/h2&gt;

&lt;p&gt;Learning Assembly will teach you... &lt;/p&gt;

&lt;p&gt;• How the CPU obtains and executes instructions.&lt;br&gt;
• The manner in which data is stored in computer memory.&lt;br&gt;
• The manner in which instructions access and process data.&lt;br&gt;
• The manner in which programs interact with the operating system.&lt;br&gt;
• Using disassembly to debug a high-level program.&lt;br&gt;
• Understanding memory locations and pointers.&lt;br&gt;
• How to write quick applications that use less memory.&lt;/p&gt;

&lt;p&gt;By the end of this serie, you will have a solid foundation in x64 assembly language programming using ASMX64 and will be able to write efficient and optimized low-level code for the x64 architecture.&lt;/p&gt;

&lt;p&gt;Follow me on Twitter and wait for the next lesson !!&lt;br&gt;
&lt;a href="https://twitter.com/web3_ruud"&gt;https://twitter.com/web3_ruud&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m on @buymeacoffee. If you like my work, you can buy me a and share your thoughts 🎉☕ &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/officialsa4"&gt;https://www.buymeacoffee.com/officialsa4&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advance Solidity Assembly: Bit Shifting and Storage Offsets</title>
      <dc:creator>Ruud Christopher Saimplice</dc:creator>
      <pubDate>Fri, 21 Apr 2023 17:13:02 +0000</pubDate>
      <link>https://dev.to/web3_ruud/advance-solidity-assembly-bitshifting-and-storage-offsets-2mai</link>
      <guid>https://dev.to/web3_ruud/advance-solidity-assembly-bitshifting-and-storage-offsets-2mai</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Bit Shifting Overview&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Bit shifting is an important notion in Solidity that enables for efficient binary data manipulation. In this blog post, we will look into bit shifting in Solidity and how it may be used. It can be used to transform data kinds, extract individual bits, and set specific bits. Understanding bit shifting is critical for producing efficient and optimized code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bit shifting&lt;/strong&gt; is the process of shifting the bits of a binary number to the left or right. This procedure can be used to manipulate binary data, such as converting between data types, extracting certain bits, or setting specific bits.&lt;/p&gt;

&lt;p&gt;We can execute bit shifting in Solidity by using the and &lt;strong&gt;&amp;gt;&amp;gt;&lt;/strong&gt; operators. The operator shifts a number's bits to the left, whereas the &lt;strong&gt;&amp;gt;&amp;gt;&lt;/strong&gt; operator shifts the bits to the right.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Packed Variables&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You will occasionally need to access or write variables while using assembly.&lt;/p&gt;

&lt;p&gt;There is a specific instance in which numerous variables are packed.&lt;/p&gt;

&lt;p&gt;Because the EVM has 256 bits (32bytes) slots, you can't directly access the packed variables; instead, you'll access the storage slot and find the offset of said variable.&lt;/p&gt;

&lt;p&gt;Then, shift the bits right until you reach the offset where the variable was stored inside the slot.&lt;/p&gt;

&lt;p&gt;If necessary, mask some bits so that it returns the expected value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;
&lt;span class="kt"&gt;uint128&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint104&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint16&lt;/span&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;E&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint8&lt;/span&gt;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;G&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;readBySlot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="c1"&gt;// return 0x0100040000000000000000000000000600000000000000000000000000000002
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of the variables are crammed into the same &lt;strong&gt;32-byte slot&lt;/strong&gt;.Variables will always be packed from the bottom to the top.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How can we get the value of the &lt;strong&gt;variable E&lt;/strong&gt; ? To do so, we need to know the variable offset, which is the location of the variable in bytes from right to left.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;offsetE&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;_off&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// return 29 bytes left
&lt;/span&gt;        &lt;span class="n"&gt;_off&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have everything we require, we will begin the &lt;strong&gt;shiffting&lt;/strong&gt; process.We need to move the E value &lt;strong&gt;29 bytes&lt;/strong&gt; to the right in order to return it. We'll need to use &lt;strong&gt;shr&lt;/strong&gt; to determine the number of bits to shift right. Because we have &lt;strong&gt;bytes&lt;/strong&gt;, we must multiply that amount by &lt;strong&gt;8&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;shiftE&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;  &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;shr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;return &lt;strong&gt;0x0000000000000000000000000000000000000000000000000000000000010004&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We're getting there; we now have &lt;strong&gt;E&lt;/strong&gt; in the last byte, but there's something unusual going on. We still have the &lt;strong&gt;G variable 1&lt;/strong&gt;.We shall now begin &lt;strong&gt;masking&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;shiftE&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;  &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;shr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0xffff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;return &lt;br&gt;
1:&lt;strong&gt;0x0000000000000000000000000000000000000000000000000000000000010004&lt;/strong&gt;.&lt;br&gt;
2:&lt;strong&gt;4&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Write to storage without causing any problems with the code
&lt;/h2&gt;

&lt;p&gt;Because the EVM can only write in &lt;strong&gt;32-byte&lt;/strong&gt; increments, writing can be challenging.We're going to write to a packed variable, say E, which is uint16.Let's put something in storage! Change the &lt;strong&gt;E&lt;/strong&gt; value to &lt;strong&gt;4&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint16&lt;/span&gt; &lt;span class="n"&gt;newE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;external&lt;/span&gt;  &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;clearedE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;newV&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;shifted&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;//VALUE -&amp;gt; 0x0100040000000000000000000000000600000000000000000000000000000002
&lt;/span&gt;
        &lt;span class="c1"&gt;//WE WANT TO DELETE E
&lt;/span&gt;        &lt;span class="n"&gt;clearedE&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0xff0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;//CLEARED E -&amp;gt; 0x0100000000000000000000000000000600000000000000000000000000000002
&lt;/span&gt;
        &lt;span class="n"&gt;shifted&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;shl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="n"&gt;newE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;//0x0000030000000000000000000000000000000000000000000000000000000000
&lt;/span&gt;
        &lt;span class="n"&gt;newV&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shifted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;clearedE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;//NEW VALUE -&amp;gt; 0x0100030000000000000000000000000600000000000000000000000000000002
&lt;/span&gt;        &lt;span class="n"&gt;sstore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;newV&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Follow me on Twitter and wait for the next serie !!&lt;br&gt;
&lt;a href="https://twitter.com/web3_ruud"&gt;https://twitter.com/web3_ruud&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m on @buymeacoffee. If you like my work, you can buy me a and share your thoughts 🎉☕ &lt;a href="https://www.buymeacoffee.com/officialsa4"&gt;https://www.buymeacoffee.com/officialsa4&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advance Solidity Assembly: Calldata Part 1</title>
      <dc:creator>Ruud Christopher Saimplice</dc:creator>
      <pubDate>Sat, 04 Mar 2023 13:48:09 +0000</pubDate>
      <link>https://dev.to/web3_ruud/advance-27hn</link>
      <guid>https://dev.to/web3_ruud/advance-27hn</guid>
      <description>&lt;p&gt;In Solidity, &lt;strong&gt;calldata&lt;/strong&gt; is a special data location that is used to store the function arguments and other data that is passed to a function when it is called. &lt;strong&gt;calldata is read-only&lt;/strong&gt;, which means that the function cannot modify the data in calldata.&lt;/p&gt;

&lt;p&gt;When a Solidity function is called, its &lt;strong&gt;arguments&lt;/strong&gt; are copied to calldata, which is a portion of the transaction data. This is different from the &lt;strong&gt;memory data location&lt;/strong&gt;, which is used to store data that is only used within the function and is not part of the transaction data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calldata in Gory Details !!!!
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Calldata&lt;/strong&gt; is a continuous sequence of bytes that is structured in the same way as memory, as opposed to other data locations like storage or stack, which are composed of 32-byte words.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When reading from calldata&lt;/strong&gt;, the behavior is the same as memory: you can load 32 bytes at a time using calldataload or mload. However, calldata is different from memory because you cannot write to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calldata is a data location&lt;/strong&gt; that is unique to EVM-based blockchains, and it has a specific layout that includes:&lt;/p&gt;

&lt;p&gt;1- The &lt;strong&gt;first 4 bytes&lt;/strong&gt; of calldata represent the &lt;strong&gt;function signature selector&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;2- The remaining bytes of calldata represent the &lt;strong&gt;input parameters of the function&lt;/strong&gt;. Each input argument is always 32 bytes long, regardless of its actual size. If an argument's &lt;strong&gt;type is smaller than 32 bytes&lt;/strong&gt;, it will be &lt;strong&gt;padded&lt;/strong&gt; to fill the remaining space.&lt;/p&gt;

&lt;p&gt;Input arguments in Solidity are padded either on the right or left depending on their type. For example, uintN and address types are padded on the left, while bytesN types are padded on the right.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advance Solidity Assembly: Storage Slots Part 2</title>
      <dc:creator>Ruud Christopher Saimplice</dc:creator>
      <pubDate>Sun, 26 Feb 2023 22:22:24 +0000</pubDate>
      <link>https://dev.to/web3_ruud/advance-solidity-assembly-storage-slots-part-2-197e</link>
      <guid>https://dev.to/web3_ruud/advance-solidity-assembly-storage-slots-part-2-197e</guid>
      <description>&lt;h2&gt;
  
  
  Arrays with Fixed Sized
&lt;/h2&gt;

&lt;p&gt;In Solidity, fixed-size arrays are stored in memory as &lt;strong&gt;contiguous blocks of memory&lt;/strong&gt;. Each element in the array is stored in the next &lt;strong&gt;available memory slot&lt;/strong&gt;, starting from the &lt;strong&gt;base memory address of the array&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fixedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fixedArraySlot&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;_slot&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//return 0
&lt;/span&gt;     &lt;span class="n"&gt;_slot&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;fixedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To read a &lt;strong&gt;specific element&lt;/strong&gt; of a &lt;strong&gt;fixed-size array&lt;/strong&gt; in &lt;strong&gt;Solidity&lt;/strong&gt;, you need to calculate the memory location of the element by adding the &lt;strong&gt;memory slot&lt;/strong&gt; of the start of the array to the &lt;strong&gt;index&lt;/strong&gt; of the element you want to access. This calculation is necessary because &lt;strong&gt;fixed-size arrays&lt;/strong&gt; in &lt;strong&gt;Solidity&lt;/strong&gt; are stored in contiguous blocks of memory, with each element occupying a fixed amount of space. By adding the appropriate &lt;strong&gt;offset&lt;/strong&gt; to the start of the array, you can find the &lt;strong&gt;memory location&lt;/strong&gt; of the element you want to read or modify.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fixedArrayView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="n"&gt;_value&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrays with No-Fixed Size
&lt;/h2&gt;

&lt;p&gt;To obtain the length of a dynamic array in Solidity, you need to access the slot in memory where the length of the array is stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;bigArray&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;bigArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt; &lt;span class="c1"&gt;// return 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;keccak256&lt;/strong&gt; function is used to generate a unique identifier for the &lt;strong&gt;storage slot&lt;/strong&gt; where the dynamic array is stored. This identifier, represented as a &lt;strong&gt;bytes32&lt;/strong&gt; value, is then used to determine the precise location of the array within the storage space. By using this identifier to calculate the &lt;strong&gt;location of the array&lt;/strong&gt; in storage, you can access its elements or modify its contents using &lt;strong&gt;Solidity assembly code&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;readBigArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;_slot&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_slot&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;bigArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//hash of the slot give us the location
&lt;/span&gt;    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_slot&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_data&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Small Arrays !! It's the same process&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint8&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;smallArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;readSmallArrayLocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;slot&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;smallArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Mapping
&lt;/h2&gt;

&lt;p&gt;When a &lt;strong&gt;mapping&lt;/strong&gt; is declared, Solidity reserves a space in storage to store the mapping data. The &lt;strong&gt;location&lt;/strong&gt; of this space is determined by a unique identifier generated by the keccak256 function, which is concatenated with the key of the storage slot and the slot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;slot&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;myMapping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

    &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Follow me on Twitter !! &lt;a href="https://twitter.com/web3_ruud"&gt;https://twitter.com/web3_ruud&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m on @buymeacoffee. If you like my work, you can buy me a and share your thoughts 🎉☕ &lt;a href="https://www.buymeacoffee.com/officialsa4"&gt;https://www.buymeacoffee.com/officialsa4&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advance Solidity Assembly: Storage Slots Part 1</title>
      <dc:creator>Ruud Christopher Saimplice</dc:creator>
      <pubDate>Sat, 25 Feb 2023 23:41:21 +0000</pubDate>
      <link>https://dev.to/web3_ruud/advance-soliditymastering-storage-slot-c38</link>
      <guid>https://dev.to/web3_ruud/advance-soliditymastering-storage-slot-c38</guid>
      <description>&lt;p&gt;&lt;strong&gt;Solidity's storage&lt;/strong&gt; is organized into slots of &lt;strong&gt;256 bits each&lt;/strong&gt;, and variables are packed into these slots to make more efficient use of storage. When variables are packed into a slot, they are placed in the lowest-indexed positions in the slot first, with any remaining bits at the end of the slot unused.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Storage Slot&lt;/strong&gt;.
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;storage slot is a fixed-sized unit of storage that can hold 256 bits of data&lt;/strong&gt;. Solidity's storage is organized into a virtual array of these slots, indexed by &lt;strong&gt;256-bit unsigned integers&lt;/strong&gt;. Each storage slot can hold one or more variables, &lt;strong&gt;depending on their size and packing rules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a variable is declared in a Solidity contract, it is assigned to a storage slot in a deterministic manner, based on its type and position in the contract's state variables. The Solidity compiler calculates the correct slot number for each variable and generates the necessary bytecode to access or modify its value in storage.&lt;/p&gt;

&lt;p&gt;In Solidity, the &lt;strong&gt;.slot&lt;/strong&gt; keyword is used to access the &lt;strong&gt;storage slot of a contract's state variable&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(variable_name).slot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract GetBySlot{

    uint256 public a = 34;
    uint256 public b = 122;

    function getSlotVarA() external pure returns(uint _slot){
        assembly{
            //return 0
            _slot:= a.slot
        }
    }
    function getSlotVarB() external pure returns(uint _slot){
        assembly{
            //return 1
            _slot:= b.slot
        }
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Update Value using Storage Slot.
&lt;/h2&gt;

&lt;p&gt;In Solidity assembly, the &lt;strong&gt;sstore opcode&lt;/strong&gt; is used to store a value in the contract's storage.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Solidity storage is a persistent key-value&lt;/strong&gt; store that is used to store the &lt;strong&gt;state variables&lt;/strong&gt; of the contract. Each key-value pair is stored in a unique storage slot, which is identified by a 256-bit key. The sstore opcode is used to store a 256-bit value in a specific storage slot, identified by its &lt;strong&gt;key&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Be carefull with this instruction in your code
function setBySlot(uint256 slot,uint256 value) external {
  assembly {
     sstore(slot,value)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Warning ⚠️
&lt;/h2&gt;

&lt;p&gt;When using Solidity assembly to manipulate storage slots, it is important to keep in mind that storage operations are very expensive in terms of gas consumption. Every read or write operation to a storage slot incurs a gas cost, which is significantly higher than the gas cost of equivalent operations on memory or stack.&lt;/p&gt;

&lt;p&gt;As a result, it is generally a good practice to minimize the number of storage operations in your contract, and to carefully consider the layout of your data in storage to optimize for gas efficiency.&lt;/p&gt;

&lt;p&gt;In addition, it is important to be aware of the potential risks of using assembly to directly manipulate storage slots, as this can introduce subtle bugs and security vulnerabilities in your contract. If you are new to Solidity programming, it is recommended that you start with simpler, high-level abstractions before diving into low-level assembly programming.&lt;/p&gt;

&lt;p&gt;When working with storage slots in Solidity assembly, it is also important to be aware of the potential for collisions between different contract variables that share the same storage slot. This can occur if two variables are declared with the same storage location using the slot keyword, or if the same storage slot is used for different purposes at different points in the contract execution.&lt;/p&gt;

&lt;p&gt;To avoid these risks, it is important to carefully manage your use of storage slots and to ensure that each variable has a unique storage location. It is also recommended to use higher-level Solidity constructs, such as structs and mappings, to manage complex data structures in storage, as these can help to simplify the logic of your code and reduce the risk of errors.&lt;/p&gt;

&lt;p&gt;Follow me on Twitter !! &lt;a href="https://twitter.com/web3_ruud" rel="noopener noreferrer"&gt;https://twitter.com/web3_ruud&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m on @buymeacoffee. If you like my work, you can buy me a  and share your thoughts 🎉☕ &lt;a href="https://www.buymeacoffee.com/officialsa4" rel="noopener noreferrer"&gt;https://www.buymeacoffee.com/officialsa4&lt;/a&gt; &lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Unlocking the Power of Solidity: Dive into the Basics of Assembly Language</title>
      <dc:creator>Ruud Christopher Saimplice</dc:creator>
      <pubDate>Thu, 23 Feb 2023 22:08:19 +0000</pubDate>
      <link>https://dev.to/web3_ruud/solidity-assembly-gd3</link>
      <guid>https://dev.to/web3_ruud/solidity-assembly-gd3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Solidity assembly&lt;/strong&gt; is a low-level programming language that is used in the &lt;strong&gt;Ethereum Virtual Machine (EVM)&lt;/strong&gt;. It is a set of instructions that can be used to interact directly with the EVM, which is responsible for executing smart contracts on the Ethereum blockchain. Assembly language provides a way to write more optimized and efficient code by directly accessing and manipulating the EVM's memory and registers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Solidity Assembly ?
&lt;/h2&gt;

&lt;p&gt;While Solidity is a high-level programming language, it has certain limitations when it comes to efficiency and control over the EVM's execution. Solidity assembly allows you to bypass these limitations and write code that can be executed directly on the EVM. This can be particularly useful in situations where you need to optimize gas usage, perform low-level operations, or access specific parts of the EVM that are not accessible through Solidity.&lt;/p&gt;

&lt;p&gt;Solidity assembly also provides a way to write more complex logic that cannot be easily expressed in Solidity. For example, you can use assembly to perform bitwise operations, which are not natively supported in Solidity. Additionally, assembly provides a way to implement cryptographic algorithms and perform other complex calculations that require low-level access to the EVM.&lt;/p&gt;

&lt;h2&gt;
  
  
  EVM (Ethereum Virtual Machine)
&lt;/h2&gt;

&lt;p&gt;The EVM is a stack machine.A stack is a collection of elements or items, and two main operations can be performed on it: push and pop. Pushing an item onto the stack adds it to the top, and popping an item from the stack removes the top item. The stack is typically visualized as a vertical structure, with the top of the stack at the top of the visualization and the bottom of the stack at the bottom.&lt;/p&gt;

&lt;p&gt;A stack machine is a type of computer architecture that uses a stack data structure to manage its memory,computation and also store all operands.In assembly language,an operand is a value (an argument) on which the instruction,named by mnemonics operates.&lt;/p&gt;

&lt;p&gt;In order to be useful the state machine have to implement some basic functionality like ADD,MUL... Without forgetting instructions usually pop one or more values from the stack to do some computation and push the result .This process is AKA &lt;strong&gt;Reverse Polish Notation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Reverse Polish Notation (RPN), AKA postfix notation, is a mathematical notation where operators come after the operands. In RPN, an expression is evaluated by scanning it from left to right, and pushing operands onto a stack. When an operator is encountered, it is applied to the top two operands on the stack, and the result is pushed back onto the stack.&lt;/p&gt;

&lt;p&gt;For example, the infix expression &lt;strong&gt;"3 + 4"&lt;/strong&gt; can be written in RPN as &lt;strong&gt;"3 4 +"&lt;/strong&gt;. Here, the operands 3 and 4 are pushed onto the stack, and the operator &lt;strong&gt;"+"&lt;/strong&gt; is applied to them. The result, &lt;strong&gt;7&lt;/strong&gt;, is pushed back onto the stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;  &lt;span class="c1"&gt;// Standard notation.
&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="c1"&gt;// Reverse polish notation.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Different Types of Assembly in Solidity
&lt;/h2&gt;

&lt;p&gt;1- Inline Assembly     : Inside the solidity source code.&lt;br&gt;
2- Standalone Assembly : Can use without the solidity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Inline Assembly
&lt;/span&gt;&lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Use Solidity Assembly ?
&lt;/h2&gt;

&lt;p&gt;Solidity assembly is written using the assembly keyword and is enclosed in curly braces. Inside the braces, you can write a series of assembly instructions that will be executed directly on the EVM. Assembly instructions are written in a specific syntax that is similar to other assembly languages. Here is an example of Solidity assembly that add two values to memory location 0x0 and returns it after:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="cm"&gt;/*This line is calling the add operation provided by the 
    assembly language to add x and y, and storing the result 
    in a variable res.*/&lt;/span&gt;

    &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="cm"&gt;/*This line is using the mstore operation to store the 
    value of res at memory location 0x0. mstore takes two 
    arguments: the memory location to store the data and the 
    data to store.*/&lt;/span&gt;

    &lt;span class="n"&gt;mstore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="cm"&gt;/*This line is returning the result of the addition as a 
    32-byte value starting at memory location 0x0. The return 
    statement takes two arguments: the memory location of the 
    data to return and the size of the data to return.*/&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables declarations and Assignments
&lt;/h2&gt;

&lt;p&gt;1- &lt;strong&gt;Stack Variables&lt;/strong&gt;: In assembly language, variables can be declared as stack variables using the mstore operation. Stack variables are used to store values on the stack and are local to the current execution context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;mstore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Declare stack variable and assign value 5
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- &lt;strong&gt;Memory Variables&lt;/strong&gt;: Memory variables are used to store data in memory and are declared using the mload operation. Memory variables can be global or local to a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;myVariable&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;mload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Declare memory variable
&lt;/span&gt;  &lt;span class="n"&gt;mstore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myVariable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Assign value 5 to memory variable
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- &lt;strong&gt;Storage Variables&lt;/strong&gt;: Storage variables are used to store data in storage and are declared using the sload operation. Storage variables can be global or local to a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;myVariable&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Declare storage variable
&lt;/span&gt;  &lt;span class="n"&gt;sstore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Assign value 5 to storage variable
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the let keyword.You can then assign it value with the &lt;strong&gt;:=&lt;/strong&gt; operator.When declaring a new local variable with let, Solidity will automatically allocate a new memory slot for the variable and initialize it with the default value (which is zero for all types except bool, which is false). The variable's type is determined by the type of the assigned value.Variables declared in let are not visible outside the assembly block.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's important to note that using assembly language for variable declaration and assignment can be more complex than using Solidity's built-in syntax. Additionally, assembly language can be more difficult to read and write, and can introduce security risks if not used correctly. It is recommended to use assembly language only when necessary and with caution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Follow me on Twitter !! &lt;a href="https://twitter.com/web3_ruud"&gt;https://twitter.com/web3_ruud&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m on @buymeacoffee. If you like my work, you can buy me a  and share your thoughts 🎉☕ &lt;a href="https://www.buymeacoffee.com/officialsa4"&gt;https://www.buymeacoffee.com/officialsa4&lt;/a&gt; &lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
