Dahn
Dahn

Reputation: 149

Is it possible to code ASM code in Intel Syntax for AMD processors?

Using Intel Syntax or AT&T is independent from CPU microarchitecture? I mean, is it task of the compiler to translate the code (independently if it is AT&T or Intel Syntax) to binary so code can be written using any syntax?

Upvotes: 0

Views: 1398

Answers (2)

hookenz
hookenz

Reputation: 38897

According to this link, there are two main branches of assembler syntax.

x86 assembly language has two main syntax branches: Intel syntax, originally used for documentation of the x86 platform, and AT&T syntax.1 Intel syntax is dominant in the MS-DOS and Windows world, and AT&T syntax is dominant in the Unix world, since Unix was created at AT&T Bell Labs.[2]

Either can be used to write assembler for x86 based CPU's. The assembler will take that syntax and convert it into a binary format that the OS can load and that the CPU can execute.

So in theory at least, two programs written in either syntax should compile into the exact same binary code provided the assembler can understand the provided syntax.

Which format you use depends on what your assembler supports. And just to confuse things further, there are other variations such as nasm which is rather popular.

Upvotes: 2

TrueY
TrueY

Reputation: 7610

The syntax is independent from the CPU architecture. E.g. in or either syntax can be used. The default is the AT&T format, but adding ".intel_syntax" to the inline assembly the intel syntax can be used.

Example code

int main()     {
  __asm__ __volatile__ (
    ".intel_syntax noprefix\n"
    "jmp farlabel\n"
     "mov  EAX,EAX\n"
     "farlabel:\n"
     "mov  EAX,EAX\n"
     ".att_syntax prefix\n"
  );
  return 0;
}

(source)

Pragma noprefix means that % not needed to be added before the name of the register. The .att_syntax switches back to AT&T syntax as the rest of the generated assembly code is in this style.

Based on black's comment I checked which programs are called if I compile the above small code. Actually calls cc1plus which generates a .s file (this is the assembly code), then calls as which generates .o (object) file, then calls collect2 (I assume this adds the dynamic loader and other machinery around the user code), then cals ld to link the code together and create the executable.

If gcc -S x.cc is called then it stops right after the assembly code generation, so the temporary file can be seen. Here is the generated code:

        .file   "x.cc"
        .text
        .globl*  main
        .type   main, @function
main:        
.LFB0:        
        .cfi_startproc
        pushl   %ebp
        .cfi_def_cfa_offset 8
        .cfi_offset 5, -8
        movl    %esp, %ebp
        .cfi_def_cfa_register 5
#APP        
# 9 "x.cc" 1
        .intel_syntax noprefix
jmp farlabel    
mov  EAX,EAX   
farlabel:        
mov  EAX,EAX   
.att_syntax prefix

# 0 "" 2 
#NO_APP        
        movl    $0, %eax
        popl    %ebp
        .cfi_restore 5
        .cfi_def_cfa 4, 4
        ret
        .cfi_endproc
.LFE0:        
        .size   main, .-main
        .ident  "GCC: (Debian 4.7.2-5) 4.7.2"
        .section        .note.GNU-stack,"",@progbits

Here the two styles are intermixed...

Upvotes: 4

Related Questions