Assembler code in C++ code

16,494

Solution 1

UPDATE: How does it look in Visual Studio ?

If you are building for 64 bit, you cannot use inline assembly in Visual Studio. If you are building for 32 bit, then you use __asm to do the embedding.

Generally, using inline ASM is a bad idea.

  1. You're probably going to produce worse ASM than a compiler.
  2. Using any ASM in a method generally defeats any optimizations which try to touch that method (i.e. inlining).
  3. If you need to access specific features of the processor not obvious in C++ (e.g. SIMD instructions) then you can use much more consistent with the language intrinsics provided by most any compiler vendor. Intrinsics give you all the speed of that "special" instruction but in a way which is compatible with the language semantics and with optimizers.

Solution 2

You can find a complete howto here http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

#include <stdlib.h>

int main()
{
     int temp = 0;
     int usernb = 3;

     __asm__ volatile (
          "pusha \n"
          "mov eax, %0 \n"
          "inc eax \n"
          "mov ecx, %1 \n"
          "xor ecx, %1 \n"
          "mov %1, ecx \n"
          "mov eax, %1 \n"
          "popa \n"
          : // no output
          : "m" (temp), "m" (usernb) ); // input
     exit(0);
}

After that you need to compile with something like:

gcc -m32 -std=c99 -Wall -Wextra -masm=intel -o casm casmt.c && ./casm && echo $?
output:
0

You need to compile with the -masm=intel flag since you want intel assembly syntax :)

Solution 3

It depends on your compiler. But from your tags I guess you use gcc/g++ then you can use gcc inline assembler. But the syntax is quite weird and a bit different from intel syntax, although it achieves the same.

EDIT: With Visual Studio (or the Visual C++ compiler) it get's much easier, as it uses the usual Intel syntax.

Solution 4

Here's a simple example to show the syntax for GCC/Dev-C++:

int main(void)
{
    int x = 10, y;

    asm ("movl %1, %%eax;"
         "movl %%eax, %0;"
        :"=r"(y)    /* y is output operand */
        :"r"(x)     /* x is input operand */
        :"%eax");   /* %eax is clobbered register */
}

Solution 5

If it's for some exercices I'd recommend some real assembler avoiding inlined code as it can get rather messy/confusing.

Some basics using GCC can be found here.

If you're open to trying MSVC (not sure if GCC is a requirement), I'd suggest you have a look at MSVC's interpretation which is (in my opinion) a lot easier to read/understand, especially for learning assembler. An example can be found here.

Share:
16,494
Hooch
Author by

Hooch

Updated on June 26, 2022

Comments

  • Hooch
    Hooch almost 2 years

    How can I put Intel asm code into my c++ application? I'm using Dev-C++.

    I want to do sth like that:

    int temp = 0;
    int usernb = 3;
    
    pusha
    mov eax, temp
    inc eax
    xor usernb, usernb
    mov eax, usernb
    popa
    

    This is only example. How can I do sth like that?

    UPDATE: How does it look in Visual Studio ?

  • DipSwitch
    DipSwitch almost 13 years
    gcc and g++ can also compile intel syntax asm you need need to add the flag -masm=intel :)
  • Christian Rau
    Christian Rau almost 13 years
    Wow, now I didn't know that. Makes gcc inline assembler much more usable to me, as I'm a real enemy of this ugly gcc syntax (even if it might be more powerful). But actually I try to avoid asm anyways, as intrinsics are most times sufficient (for e.g. SSE).
  • DipSwitch
    DipSwitch almost 13 years
    hehe yeah, I don't like AT&T that much as well ;)
  • WiSaGaN
    WiSaGaN over 10 years
    Is there a way of writing this that also works on microsoft visual studio c++ compiler?
  • Mateen Ulhaq
    Mateen Ulhaq almost 6 years
    This answer is technically correct but not particularly helpful.