Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 813
Hooch Avatar asked Mar 21 '26 16:03

Hooch


2 Answers

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

like image 86
DipSwitch Avatar answered Mar 23 '26 06:03

DipSwitch


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.

like image 33
Christian Rau Avatar answered Mar 23 '26 04:03

Christian Rau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!