简体   繁体   中英

Assembler code in C++ code

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 ?

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 :)

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 */
}

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.

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 (ie inlining).
  3. If you need to access specific features of the processor not obvious in C++ (eg 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM