0

I'm trying to write Intel x86 Assembly code on macOS similarly to the following screenshot:

Assembly in Visual Studio

This screenshot is taken from my professor's Visual Studio application, where he writes assembly. He's also stated that this wouldn't work on a Mac. To do this, I need the C++ Console app on Visual Studio, which the Mac version does not offer.

I've tried to circumvent this issue by trying to compile it on Visual Studio Code on my Mac (M2 Chip), and also following this reference.

My current code snippet looks like this:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int num = 10;

    asm(
        "mov eax, num\n" 
         "add eax, 12\n" 
         "mov num, eax"
         );

        return 0;
}

However, upon running this through g++ test.cpp, I receive the following errors:

        "mov eax, num\n" 
        ^
<inline asm>:1:6: note: instantiated into assembly here
        mov eax, num
            ^
test.cpp:10:11: error: invalid operand for instruction
         "add eax, 12\n" 
          ^
<inline asm>:2:5: note: instantiated into assembly here
add eax, 12
    ^
test.cpp:11:11: error: invalid operand for instruction
         "mov num, eax"
          ^
<inline asm>:3:5: note: instantiated into assembly here
mov num, eax
    ^

Any ideas on how to solve this issue? (Note: not sure if this is relevant to the problem but I've also installed nasm through brew install nasm)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
aleptian
  • 71
  • 6
  • gcc uses AT&T assembly syntax if not told otherwise. – tkausl Feb 10 '23 at 18:37
  • Use `clang -fasm-blocks` to support MSVC-style `__asm{ ... }`. GNU C inline asm works *very* differently; you have to tell the compiler what registers you want C variables in as inputs and outputs. (https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html and https://stackoverflow.com/tags/inline-assembly/info). AT&T vs. Intel syntax is also an issue, but *far* from the only or most important one, @tkausl. [Why can't local variable be used in GNU C basic inline asm statements?](https://stackoverflow.com/q/60227941) – Peter Cordes Feb 10 '23 at 18:43

0 Answers0