Assembler code in C ++ code

How can I put asm code in my C ++ application? I am using Dev-C ++.

I want to do this:

int temp = 0;
int usernb = 3;

pusha
mov eax, temp
inc eax
xor usernb, usernb
mov eax, usernb
popa

This is just an example. How can i do this?

UPDATE: How does it look in Visual Studio?

+3
source share
5 answers

UPDATE: what does it look like in Visual Studio?

If you create for 64 bits, you cannot use the built-in assembly in Visual Studio. If you are building for 32 bits, you use __asmto make an attachment.

Using ASM is generally a bad idea.

  • You will probably create worse ASM than the compiler.
  • Using any ASM in a method usually leads to victory over any optimizations that try to touch this method (i.e., embeddings).
  • , ++ (, SIMD), , . Intrinsics "" , , .
+2

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);
}

- :

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

-masm = intel, Intel:)

+4

, GCC/Dev-++:

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

. , gcc/g++, gcc inline . intel, .

EDIT: Visual Studio ( Visual ++) , Intel.

+2

, - , /.

GCC .

If you are ready to try MSVC (not sure if GCC is a must), I would suggest you take a look at the interpretation of MSVC, which (in my opinion) is much easier to read / understand, especially for assembler learning. An example can be found here .

+1
source

All Articles