Collecting resources to start exploring interop between C, C++ and x86 Assembly.

[ Check out all posts in “low-level” series here. ]

I originally studied Architecture (as in buildings).

Niklaus Wirth is among the computer science pioneers that I started reading many years after I got into programming. I have been reading Algorithms and Data Structures recently, and I am really enjoying it. I am, as many others, a fan of his work by now.

Wirth has died last week.

In my last post, I shared some useful resources on the topic of “instruction encoding”, and wrapped that topic, for now. I needed a new topic, and I needed a word to mark the first post.

So I thought BEGIN is a suitable word.

And regarding the topic: I wonder if I should focus on C/C++ and Assembly interop next.1

Of course, one aspect is inline assembly. You can read up on it in language references:

I used this kind of interop a few times, but my knowledge is limited to just a few patterns.

Actually, I used asm declaration the most for the rather famous functions below:

static void
 escape( void * p )
{
    asm volatile( "" : : "g"( p ) : "memory" );
}

static void
 clobber( )
{
    asm volatile( "" : : : "memory" );
}

These functions are verbatim copied from Chandler Carruth’s CppCon 2015 talk “Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!”. (I highly recommend watching this!)

They are basically used to prevent compiler optimizations from messing with the benchmarking attempt. Chandler describes it in detail in the talk. Here are some other explanations I found on StackOverflow regarding this, and another related pattern: Link 0 , Link 1 , Link 2 , Link 3 .

OK, back to resources. The online documentation on the topic of inline assembly seems pretty good.

Here is some documents I started reading:

Other than that, I found these really cool online lecture notes that describe calling C from assembly, and vice versa. That is, non-inline, link level interop. In fact, I got the impression that the course notes are generally really good.

That’s it for today! I am still trying to teach myself how to write a useful/interesting post every day while also making progress in my other work. I might prioritize some other topics in the following days, but I will return to this.

Thanks for reading!

  1. If you find technical errors, please report in the blog’s Issues page