Solved How do include inline assembly code in C/C++ programs?

I found a few things online using Google, but I still can't seem to make the compiler happy. I even uncovered a bug in the compiler while trying to get this to work. I have tried this a couple of different ways and clang is giving me fits. This is on FreeBSD 9.3.

Code:
  __asm__ ("pushl %%eax",
  "pushl %%edx",
  "pushf",
  "movl (bn1->dat[noparse][i][/noparse]), %%eax",
  "xorl %%edx, %%edx",
  "divl ($0x0c + bn2 + j)",
  "movl %%eax, (q)",
  "movl %%edx, (m)",
  "popf",
  "popl %%edx",
  "popl %%eax"
  );

In the above code snippet, clang wants me to replace the comma with a closing parenthesis on the first line. I am kinda at a loss on how this is supposed to work. I am familiar with x86 assembly, so that's not an issue here.

Here's the second code snippet that I tried:

Code:
  __asm__ (".intel_syntax noprefix");
  __asm__ ("pushl %eax");
  __asm__ ("pushl %edx");
  __asm__ ("pushf");
  __asm__ ("movl %eax, (bn1->dat[noparse][i][/noparse])");
  __asm__ ("xorl %edx, %edx");
  __asm__ ("divl ($0x0c + bn2 + j)");
  __asm__ ("movl (q), %eax");
  __asm__ ("movl (m), %edx");
  __asm__ ("popf");
  __asm__ ("popl %edx");
  __asm__ ("popl %eax");

But, it does not like how I am accessing the structure bn1. I've tried two different ways to see which one works. I was told however that clang may reorder the statements, which is what I definitely do not want.

How do you access a structure in inline asm?
How do you access local variables?

My big problem is stemming from the fact that I have never used inline assembly in C before. I've either used it as a stand alone asm file or inline in Borland Pascal (many moons ago).
 
You can also put assembler in a .s file and compile it to an object file. You will need to declare extern symbols in C to be able to access it.
 
Someone else on a different forum (stackoverflow, actually) provided this link: http://gcc.godbolt.org Looking at the generated asm, it turns out that the compiler is smart enough to recognize that consecutive divide and modulus operations with the same operands need only one div instruction as it uses both results. Seeing this, the entire reason for using inline assembler in the first place has been completely blown out of the water. Since inline assembly is not needed, this question, and the associated thread is pretty much moot.
 
Back
Top