<?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 Naemmastae on Medium]]></title>
        <description><![CDATA[Stories by Naemmastae on Medium]]></description>
        <link>https://medium.com/@emmagamerwangari?source=rss-4b246ca1c25d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SKJeDrdZQB0092cuWzDd2A.jpeg</url>
            <title>Stories by Naemmastae on Medium</title>
            <link>https://medium.com/@emmagamerwangari?source=rss-4b246ca1c25d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:06:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emmagamerwangari/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[Reverse Engineering #1]]></title>
            <link>https://medium.com/@emmagamerwangari/reverse-engineering-1-96ea586c2eb1?source=rss-4b246ca1c25d------2</link>
            <guid isPermaLink="false">https://medium.com/p/96ea586c2eb1</guid>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <dc:creator><![CDATA[Naemmastae]]></dc:creator>
            <pubDate>Sat, 11 Oct 2025 07:28:46 GMT</pubDate>
            <atom:updated>2025-10-11T07:28:46.254Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*SmWluxNM1QDmHYCtMagndA.jpeg" /><figcaption>Rearranging Bits</figcaption></figure><p>On my quest to learn reverse engineering and understand assembly code, I found this challenge from <a href="https://challenges.re/2/">challenges.re #2</a>.</p><p>I decided to dive deep and explain <em>everything</em> down to the nitty-gritty. Let’s examine the provided code.</p><pre>&lt;f&gt;:<br>   0:   mov    eax, DWORD PTR [esp+0x4]<br>   4:   bswap  eax<br>   6:   mov    edx, eax<br>   8:   and    eax, 0x0F0F0F0F<br>   d:   and    edx, 0xF0F0F0F0<br>  13:   shr    edx, 0x4<br>  16:   shl    eax, 0x4<br>  19:   or     eax, edx<br>  1b:   mov    edx, eax<br>  1d:   and    eax, 0x33333333<br>  22:   and    edx, 0xCCCCCCCC<br>  28:   shr    edx, 0x2<br>  2b:   shl    eax, 0x2<br>  2e:   or     eax, edx<br>  30:   mov    edx, eax<br>  32:   and    eax, 0x55555555<br>  37:   and    edx, 0xAAAAAAAA<br>  3d:   add    eax, eax<br>  3f:   shr    edx, 1<br>  41:   or     eax, edx<br>  43:   ret</pre><h3>Line-by-line explanation</h3><p><strong>0:</strong><br> mov eax, DWORD PTR [esp+0x4]<br> → Moves the first argument (from the stack) into EAX.</p><p><strong>4:</strong><br> bswap eax<br> → Reverses the order of the <strong>bytes</strong> in EAX.</p><p>Example:<br> 0x12345678 → 0x78563412<br> Why not 0x87654321? Because bswap reverses <strong>bytes</strong>, not bits.<br> A byte = 8 bits (e.g., 0x78 = 01111000).</p><p><strong>6:</strong><br> Copies the value of EAX into EDX — both registers now hold the same 32-bit value.</p><h3>Swapping nibbles (4-bit halves)</h3><p><strong>8:</strong><br> and eax, 0x0F0F0F0F → keeps the <em>lower</em> 4 bits (nibbles) of every byte.<br> Example (eax = 0xB2 = 10110010):</p><pre>10110010<br>&amp; 00001111<br>-----------<br>00000010</pre><p><strong>d:</strong><br> and edx, 0xF0F0F0F0 → keeps the <em>upper</em> 4 bits of every byte.</p><pre>10110010<br>&amp; 11110000<br>-----------<br>10110000</pre><p><strong>13–19:</strong><br> shr edx, 4 (shift high nibble down)<br> shl eax, 4 (shift low nibble up)<br> or eax, edx → combines them, effectively swapping nibbles.</p><p>Example result:</p><pre>eax: 00100000<br>edx: 00001011<br>or  = 00101011  (0xB2 → 0x2B)</pre><h3>Swapping 2-bit pairs</h3><p><strong>1b–22:</strong><br> 0x33333333 = 00110011… keeps the <em>lower</em> 2 bits of every nibble.<br> 0xCCCCCCCC = 11001100… keeps the <em>upper</em> 2 bits of every nibble.</p><pre>eax = 00101011<br>and  00110011 → 00100011<br>edx = 00101011<br>and  11001100 → 00001000</pre><p><strong>28–2e:</strong><br> shr edx, 2 and shl eax, 2 shift both halves in opposite directions,<br> then or eax, edx swaps the 2-bit pairs.</p><p>Example:</p><pre>eax = 11000100<br>edx = 10000000<br>or  = 01000100</pre><h3>Swapping individual bits</h3><p><strong>30–37:</strong><br> Now for the smallest unit — bit pairs!</p><pre>0x55555555 = 01010101... → keeps even bits<br>0xAAAAAAAA = 10101010... → keeps odd bits</pre><pre>eax = 01000100<br>and  01010101 = 01000100 (keep even bits)<br>edx = 01000100<br>and  10101010 = 00000000 (no odd bits set)</pre><p><strong>3d–3f:</strong><br> add eax, eax (same as shl eax, 1) → move even bits left into odd positions.<br> shr edx, 1 → move odd bits right into even positions.</p><pre>eax = 01000100 &lt;&lt; 1 = 10001000<br>edx = 00000000 &gt;&gt; 1 = 00000000</pre><p><strong>41:</strong><br> or eax, edx → combines results.<br> Final value = 10001000.</p><p>This function performs <strong>a full bit reversal</strong> on a 32-bit value:</p><ul><li>First reverses <strong>bytes</strong> (bswap)</li><li>Then reverses <strong>nibbles</strong> (4-bit swaps)</li><li>Then <strong>2-bit pairs</strong></li><li>Then <strong>individual bits</strong></li></ul><p>So if you input a 32-bit number, the output will be that number with all its bits reversed — from least significant to most significant.</p><p>This is a brilliant example of how low-level code manipulates data efficiently using bitwise logic.<br> Each step progressively reverses smaller bit groups until the entire 32-bit value is flipped.</p><p>Understanding this teaches you not just about assembly, but also the <em>beauty of how data symmetry and binary operations intertwine.</em></p><p>Can’t wait for #2.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96ea586c2eb1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>