Search code examples
assemblysyntaxcompiler-constructionintelatt

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?


Solution

  • 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...