Thread: Intel assembly syntax with GCC

  1. #1
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107

    Intel assembly syntax with GCC

    Greetings,

    GCC uses AT&T/UNIX assembly syntax which I really detest. Fortunately GCC has provided us with the '-masm=intel' switch to use Intel assembly syntax instead. The only problem that I have is to compile even the most simple inline assembly code when using the Intel assembly syntax. Does anyone has any experience with the Intel assembly syntax when compiling code with GCC?

    For example, the following two code snippets do not compile:

    Code:
    DWORD dwTest;
    
    main()
    {
    
        asm( ".intel_syntax noprefix;" );
        asm( "mov ds:_dwTest, 0xDEADBEEF;" );
    
        return 0;
    }
    Code:
    main()
    {
    
        DWORD dwTest;
    
        asm( ".intel_syntax noprefix;" );
        asm( "mov eax, _dwTest;" );
    
        return 0;
    }
    Could someone enlighten me or tell me how I should interpret the Intel assembly syntax with GCC?

    Thanks for your time,
    Ktulu
    This parameter is reserved

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The GCC produces AT&T syntax by default, therefore when using the inline switch to intel, the assembler is expecting Intel while GCC gives AT&T.

    EDIT: Therefore, when switching like that, in inline assembly, you must always switch back after your assembly code finishes.
    Last edited by GReaper; 05-26-2011 at 04:19 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Ktulu View Post
    Could someone enlighten me or tell me how I should interpret the Intel assembly syntax with GCC?

    Thanks for your time,
    Ktulu
    I believe what you're looking for is one of
    Code:
    mov dword ptr ds:dwTest, 0xDEADBEEF
    mov dword ptr [dwTest], 0xDEADBEEF
    You need to specify the operand size, and get rid of the underscore.


    Quote Originally Posted by Sipher View Post
    The GCC produces AT&T syntax by default, therefore when using the inline switch to intel, the assembler is expecting Intel while GCC gives AT&T.

    EDIT: Therefore, when switching like that, in inline assembly, you must always switch back after your assembly code finishes.
    Maybe I'm misunderstanding what you're trying to say; But if you mean that you have to switch back to AT&T or the compiler will output incorrect assembly code from C code, then no that's not true.
    Last edited by _Mike; 05-26-2011 at 04:33 PM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by _Mike View Post
    Maybe I'm misunderstanding what you're trying to say; But if you mean that you have to switch back to AT&T or the compiler will output incorrect assembly code from C code, then no that's not true.
    Not incorrect code, just the wrong type. And yes, it's true!
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Sipher View Post
    Not incorrect code, just the wrong type. And yes, it's true!
    'gcc -masm=intel -save-temps test.c'
    test.c
    Code:
    #include <stdio.h>
    
    unsigned int i = 0;
    
    int main()
    {
        printf("%d\n", i);
        asm(".intel_syntax noprefix\n");
        asm("mov dword ptr [i], 1\n");
        printf("%d\n", i);
    
        return 0;
    }
    test.s
    Code:
            .file   "test.c"
            .intel_syntax noprefix
    .globl i
            .bss
            .align 4
            .type   i, @object
            .size   i, 4
    i:
            .zero   4
            .section        .rodata
    .LC0:
            .string "%d\n"
            .text
    .globl main
            .type   main, @function
    main:
    .LFB0:
            .cfi_startproc
            push    rbp
            .cfi_def_cfa_offset 16
            mov     rbp, rsp
            .cfi_offset 6, -16
            .cfi_def_cfa_register 6
            mov     edx, DWORD PTR i[rip]
            mov     eax, OFFSET FLAT:.LC0
            mov     esi, edx
            mov     rdi, rax
            mov     eax, 0
            call    printf
    #APP
    # 8 "test.c" 1
            .intel_syntax noprefix
    
    # 0 "" 2
    # 9 "test.c" 1
            mov dword ptr [i], 1
    
    # 0 "" 2
    #NO_APP
            mov     edx, DWORD PTR i[rip]
            mov     eax, OFFSET FLAT:.LC0
            mov     esi, edx
            mov     rdi, rax
            mov     eax, 0
            call    printf
            mov     eax, 0
            leave
            ret
            .cfi_endproc
    .LFE0:
            .size   main, .-main
            .ident  "GCC: (GNU) 4.4.3"
    Compiles and runs just fine. Notice the lack of AT&T assembly.
    Btw, apparently the '-masm=intel' switch makes '.intel_syntax noprefix' implicitly specified. I didn't know that until now

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Try the exast same code without the command -masm=intel and see what happens.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intel 64 vs AMD 64
    By siavoshkc in forum Tech Board
    Replies: 6
    Last Post: 07-28-2007, 01:00 PM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. Intel on a Mac?
    By Syneris in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 02-19-2006, 07:45 PM
  4. Intel to AT&T Assembly
    By deoren in forum Linux Programming
    Replies: 2
    Last Post: 03-16-2003, 06:00 PM
  5. DJGPP assembly syntax ills...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-11-2001, 02:54 AM