アセンブラの基礎

Intel構文とAT&T構文

Intel構文をススメる理由

・CPU のメーカーが出しているインストラクションが、Intel構文に基づいて書かれているものなので、引用文献が多い。

アセンブラの種類

GNU as (gas)

・C言語のコードから、アセンブリ変換したコードを出力することができるので、わからない記述でも参考にできる。

NASM

・デフォルトで、Intel構文を採用している。

C言語からアセンブリ言語を出力してみる


"test001.c"
                
<stdio.h>

int main(void)
{
    printf("A");

    return 0;
}
                    


コンパイルオプションに、"-masm=intel"を指定すると、Intel構文でアセンブラが出力される。

gcc -masm=intel -S test001.c

"test001.s"
    .file   "test001.c"
    .intel_syntax noprefix
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    mov     rbp, rsp
    .cfi_def_cfa_register 6
    mov     edi, 65
    call    putchar
    mov     eax, 0
    pop     rbp
    .cfi_def_cfa 7, 8
    ret
                    


CFI Directive が邪魔してよく見えないので、外す。"-fno-asynchronous-unwind-tables"

gcc -fno-asynchronous-unwind-tables -masm=intel -S test001.c


        .file   "test001.c"
        .intel_syntax noprefix
        .text
        .globl  main
        .type   main, @function
main:
        push    rbp
        mov     rbp, rsp
        mov     edi, 65
        call    putchar
        mov     eax, 0
        pop     rbp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-36)"
        .section        .note.GNU-stack,"",@progbits
                    


因みに、AT&T構文配下の通り。
gcc -fno-asynchronous-unwind-tables -S test001.c


        .file   "test001.c"
        .text
        .globl  main
        .type   main, @function
main:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $65, %edi
        call    putchar
        movl    $0, %eax
        popq    %rbp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-36)"
        .section        .note.GNU-stack,"",@progbits