How Can Objdump Emit Intel Syntax

How can objdump emit intel syntax

What you're looking for is -M intel. Use it as follows.

objdump -M intel -d program_name

Why does the assembly encoding of objdump vary?

obdjump defaults to -Matt AT&T syntax (like your 2nd code block). See att vs. intel-syntax. The tag wikis have some info about the syntax differences: https://stackoverflow.com/tags/att/info vs. https://stackoverflow.com/tags/intel-syntax/info

Either syntax has the same limitations, imposed by what the machine itself can do, and what's encodeable in machine code. They're just different ways of expressing that in text.


Use objdump -d -Mintel for Intel syntax. I use alias disas='objdump -drwC -Mintel' in my .bashrc, so I can disas foo.o and get the format I want, with relocations printed (important for making sense of a non-linked .o), without line-wrapping for long instructions, and with C++ symbol names demangled.


In inline asm, you can use either syntax, as long as it matches what the compiler is expecting. The default is AT&T, and that's what I'd recommend using for compatibility with clang. Maybe there's a way, but clang doesn't work the same way as GCC with -masm=intel.

Also, AT&T is basically standard for GNU C inline asm on x86, and it means you don't need special build options for your code to work.

But you can use gcc -masm=intel to compile source files that use Intel syntax in their asm statements. This is fine for your own use if you don't care about clang.


If you're writing code for a header, you can make it portable between AT&T and Intel syntax using dialect alternatives, at least for GCC:

static inline
void atomic_inc(volatile int *p) {
// use __asm__ instead of asm in headers, so it works even with -std=c11 instead of gnu11
__asm__("lock {addl $1, %0 | add %0, 1}": "+m"(*p));
// TODO: flag output for return value?
// maybe doesn't need to be asm volatile; compilers know that modifying pointed-to memory is a visible side-effect unless it's a local that fully optimizes away.
// If you want this to work as a memory barrier, use a `"memory"` clobber to stop compile-time memory reordering. The lock prefix provides a runtime full barrier
}

source+asm outputs for gcc/clang on the Godbolt compiler explorer.

With g++ -O3 (default or -masm=att), we get

atomic_inc(int volatile*):
lock addl $1, (%rdi) # operand-size is from my explicit addl suffix
ret

With g++ -O3 -masm=intel, we get

atomic_inc(int volatile*):
lock add DWORD PTR [rdi], 1 # operand-size came from the %0 expansion
ret

clang works with the AT&T version, but fails with -masm=intel (or the -mllvm --x86-asm-syntax=intel which that implies), because that apparently only applies to code emitted by LLVM, not for how the front-end fills in the asm template.

The clang error message is:

<source>:4:13: error: unknown use of instruction mnemonic without a size suffix
__asm__("lock {addl $1, %0 | add %0, 1}": "+m"(*p));
^
<inline asm>:1:2: note: instantiated into assembly here
lock add (%rdi), 1
^
1 error generated.

It picked the "Intel" syntax alternative, but still filled in the template with an AT&T memory operand.

How to jump to memory location in intel syntax for both x64 and x32

It's unclear whether you need assembly or machine code. If you want to jump to an absolute address then in 64 bit mode use an embedded pointer addressed rip relative:

jmp [rip+foo]
foo: .quad target_address

Machine code: ff 25 00 00 00 00 xx xx xx xx xx xx xx xx (with the last 8 bytes being the target absolute address).

In 32 bit code you can use the push+ret trick if you are not worried about branch prediction:

push offset foo
ret

Machine code: 68 xx xx xx xx c3

If you can calculate the relative address you can of course just use a normal jmp which is e9 xx xx xx xx with the last 4 bytes being the distance to jump (counted from the byte following the instruction, where execution would normally continue).

How do you use gcc to generate assembly code in Intel syntax?


Use -masm=intel

gcc -S -masm=intel -Og -fverbose-asm test.c

That works with GCC, and clang3.5 and later. GCC manual:

  • -masm=dialect

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

For Mac OSX, note that by default, the gcc command actually runs clang. Modern clang supports -masm=intel as a synonym for this, but this always works with clang:

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

Note that until clang 14, this does not change how clang processes inline asm() statements, unlike for GCC.

These are the options used by Matt Godbolt's Compiler Explorer site by default: https://godbolt.org/

See also How to remove "noise" from GCC/clang assembly output? for other options and tips for getting asm output that's interesting to look at.



Related Topics



Leave a reply



Submit