How do you use gcc to generate assembly code in In

2019-01-03 01:29发布

The gcc -S option will generate assembly code in AT&T syntax, is there a way to generate files in Intel syntax? Or is there a way to convert between the two?

3条回答
Summer. ? 凉城
2楼-- · 2019-01-03 01:33

The

gcc -S -masm=intel test.c

Does work with me. But i can tell another way, although this has nothing to do with running gcc. Compile the executable or the object code file and then disassemble the object code in Intel asm syntax with objdump as below:

 objdump -d --disassembler-options=intel a.out

This might help.

查看更多
Bombasti
3楼-- · 2019-01-03 01:36

I have this code in CPP file:

#include <conio.h>
#include <stdio.h>
#include <windows.h>

int a = 0;
int main(int argc, char *argv[]) {
    asm("mov eax, 0xFF");
    asm("mov _a, eax");
    printf("Result of a = %d\n", a);
    getch();
    return 0;
 };

That's code worked with this GCC command line:

gcc.exe File.cpp -masm=intel -mconsole -o File.exe

It will result *.exe file, and it worked in my experience.

Notes:
immediate operand must be use _variable in global variabel, not local variable.
example: mov _nLength, eax NOT mov $nLength, eax or mov nLength, eax

A number in hexadecimal format must use at&t syntax, cannot use intel syntax.
example: mov eax, 0xFF -> TRUE, mov eax, 0FFh -> FALSE.

That's all.

查看更多
迷人小祖宗
4楼-- · 2019-01-03 02:00

Have you tried this?

gcc -S -masm=intel test.c

Untested, but I found it in this forum where someone claimed it worked for them.

I just tried this on the mac and it failed, so I looked in my man page:

   -masm=dialect
       Output asm instructions using selected dialect.  Supported choices
       are intel or att (the default one).  Darwin does not support intel.

It may work on your platform.

For Mac OSX:

clang++ -S -mllvm --x86-asm-syntax=intel test.cpp

Source: https://stackoverflow.com/a/11957826/950427

查看更多
登录 后发表回答