[C/C++][Assembly][RevEng] Understanding C/C++ code by compiling it

Sometimes, in order to understand a tricky piece of C/C++ code, you have to compile it. And sometimes, assembly code is easier to grasp.

There are couple of my histories, when I compiled something to understand it better.

David Litchfield in his The Oracle Hacker's Handbook: Hacking and Defending Oracle book:

wordlen -= wordlen & 1;

I used C compiler + IDA to understand this. But today you can use Godbolt's compiler explorer, of course.

        mov     eax, edi
        and     eax, -2
        ret

-2 is 0xffff....fffe. So this code, in other words, clears the lower bit, and it would be possible to simplify it to:

wordlen = wordlen & 1;

(Without subtraction.) It seems, David Litchfield meant aligning by 2-byte boundary.

(UPD: Ouch! A typo. My bad. Thanks, masklinn at reddit. Correct would be : wordlen & ~1.)

Now my all-time favorite from SAT0W SAT solver by Donald Knuth:

p += p+(i&1)+2;

This is pretty close to IOCCC contests.

        and     esi, 1
        lea     eax, [rsi+2+rdi*2]
        ret

Let's rewrite:

esi&1 + 2 + rdi*2

Or:

p = i&1 + 2 + p*2

More examples in my book.

(UPD: as seen at reddit.)


List of my other blog posts. My company.

Yes, I know about these lousy Disqus ads. Please use adblocker. I would consider to subscribe to 'pro' version of Disqus if the signal/noise ratio in comments would be good enough.