<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vio Ariton on Medium]]></title>
        <description><![CDATA[Stories by Vio Ariton on Medium]]></description>
        <link>https://medium.com/@ariton.vio?source=rss-c8859f847fc1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Nv3aPhqIWQp4ANfw-ylPdA.jpeg</url>
            <title>Stories by Vio Ariton on Medium</title>
            <link>https://medium.com/@ariton.vio?source=rss-c8859f847fc1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 17:08:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ariton.vio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What is structure padding?]]></title>
            <link>https://medium.com/@ariton.vio/what-is-structure-padding-3484809213d6?source=rss-c8859f847fc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/3484809213d6</guid>
            <category><![CDATA[c]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Vio Ariton]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 22:57:52 GMT</pubDate>
            <atom:updated>2019-11-21T23:04:06.385Z</atom:updated>
            <content:encoded><![CDATA[<p>I could already answer the question: speed. But let’s understand as to why we need it and if it’s mandatory.</p><p>Let’s take a 32-bit computer. That is, the computer can access 4 bytes of data at once. The memory in your computer is structured as a 2D array in a C program. Whenever you access a row, it enables all the cells on that specific line</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*HdTi6ZBsD-xZzn9z_VxEXg.png" /></figure><p>The cells on the activated row can be access individually or as a whole — the lines are not represented in the picture above — . Let’s take as an example the following structure</p><pre>struct _example {<br>   char    ch;<br>   int32_t some_var;<br>}</pre><p>Without padding, the structure would look like this in memory</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*cvjC9itkjV1jQak1f6r-hA.png" /><figcaption>Green is the char and purple the integer</figcaption></figure><p>Now let’s assume that you would like to access the value of the integer. Now we have a problem — the value of the integer is on two lines, not one. The CPU would have to access the memory twice and apply shifts to get the value, in other words, slow. The solution is to add a padding.</p><p>We could modify the structure as follows</p><pre>struct _example {<br>   char    ch;<br>   char    pad[3];<br>   int32_t some_var;<br>}</pre><p>Now accessing the value of the integer can be done in one instruction. The variable is said to be 4-bytes aligned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*WpY5uwHYHZ0nDc1gqaEQAg.png" /><figcaption>After the padding</figcaption></figure><p>Is this mandatory? Not really. The compiler does this for you automatically. You can, however, tell it not to do it. Perhaps you want to save space. But before you do so, you should make sure the CPU can access unaligned variables as some cannot.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3484809213d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding how functions work]]></title>
            <link>https://medium.com/@ariton.vio/understanding-how-functions-work-d1ff24e8053d?source=rss-c8859f847fc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1ff24e8053d</guid>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[Vio Ariton]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 22:15:43 GMT</pubDate>
            <atom:updated>2019-11-21T23:23:54.331Z</atom:updated>
            <content:encoded><![CDATA[<p>Before we continue, we must first understand what a stack frame is and a stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jJXb1p9ExSgk4XU72K2f5Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@christianw?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Christian Wiediger</a> on <a href="https://unsplash.com/s/photos/cpu?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>You can think of a stack as a pile of objects that are placed onto each other. The last object you have added on the stack is also the first object to leave the stack. In the computing world, It’s known as a LIFO data structure — last in, first out.</p><p>What about a stack frame? It refers to the frame of a function that is on the stack of the memory. Every function call creates a “frame” that helps distinguish itself from the preceding function frame. The frames are structured like in the picture below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/235/1*KJMcEs0QFMvHIm5-h06GgQ.png" /><figcaption>The stack frame of a function</figcaption></figure><p>The <em>stack pointer</em> — ESP — as well as the <em>base pointer</em> — EBP — are both CPU registers that hold addresses. The stack pointer will contain the address of the last element of the last frame that was added onto the stack — in the picture above, it would contain the address <em>0xFF</em> — . The <em>base pointer</em> is used as a way to access the local variables as well as the parameters of the function.</p><p>Let’s have a look at the assembly of an example and construct the frame of the program below</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdint.h&gt;</pre><pre><br>int32_t square(int32_t x) {<br>   return(x * x);<br>}</pre><pre>int main(void) {<br>   int32_t local;<br>   local = square(0x4);<br>}</pre><p>We have a function named <em>square</em> that returns the square of the number that has been passed as a parameter.</p><p>Inside the main function, we have declared a variable named <em>local </em>that will be initialized with the value returned by the function <em>square. </em>The compiler would generate the following x86 assembly:</p><pre>square:<br> pushq   %rbp                  (6)<br> movq    %rsp,    %rbp         (7)<br> movl    8(%rbp), %edi         (8)<br> movl    8(%rbp), %eax         (9)<br> imull   %eax,    %eax         (10)<br> popq    %rbp                  (11)<br> ret                           (12)</pre><pre>main:<br> pushq   %rbp                  (1)<br> movq    %rsp, %rbp            (2)<br> subq    $4, %rsp              (3)<br> pushq   $4                    (4)<br> call    square                (5)<br> ...                           </pre><p>Before we continue, you should familiarize yourself with the <a href="https://en.wikipedia.org/wiki/X86_calling_conventions">calling conventions</a>. In our example, we will use <em>cdecl </em>convention<em>.</em></p><p>At <em>(4)</em> we can see that it pushes the value 4 onto the stack. The instruction at<em> (5) </em>calls — jumps to the address of the function — , but before it does so it first has to save the address of the next instruction by pushing the contents of the <em>EIP </em>— program counter — register onto the stack. The <em>program counter</em> contains the instruction to be executed next. Since <em>EIP</em> changes after every instruction, we have to preserve the address.</p><p>Note, however, that there are two types of call instructions: <em>near call</em> and <em>far call</em>. The <em>near call</em> has to push <strong>only the address</strong> of the next instruction whereas far call has to push <strong>the address as well as the address of the CS</strong> — code segment. You can find more information about far call <a href="https://c9x.me/x86/html/file_module_x86_id_26.html">here</a>.</p><p>Now we are inside the <em>square </em>function. First, we have to preserve the<em> base pointer </em>(6) of the previous function since the <em>base pointer</em> is a register and it cannot hold only one value at a time. We start by pushing the value contained — the address — of the previous base pointer on the stack, then we move the address of the <em>stack pointer</em> in the base <em>pointer register </em>(7)<em>.</em></p><pre>square:<br> pushq   %rbp                  (6)<br> movq    %rsp,    %rbp         (7)<br> movl    8(%rbp), %edi         (8)<br> movl    8(%rbp), %eax         (9)<br> imull   %eax,    %eax         (10)<br> popq    %rbp                  (11)<br> ret                           (12)</pre><p>Since we don’t have any local variables, we don’t have to move the stack pointer anymore. If we had any local variables, we would have had to subtract from the stack pointer to make room for the local variables — e.g <em>subq %rsp,size_of_locals_in_bytes.</em></p><p>The next instructions (8, 9, 10) move the value of the argument into two registers, multiplies them and saves the result into <em>EAX. </em>After we are done, we have to restore the base pointer (11) of the previous function and place it into the <em>RBP</em> register. The next instruction (12) jumps to the address that was pushed before we entered the function. Finally, the stack would look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*JrWfA55u81B8nLKZ_Vkd9g.png" /><figcaption>Simplified stack of the example code</figcaption></figure><p>The base pointer register contains the address <em>0x05</em> and the stack pointer as well since we don’t have any local variables.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1ff24e8053d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>