在x86中,为什么我有两次相同的指令,使用反向操作数?

Nli*_*tis 10 c linux assembly gcc x86-64

我正在用x86asm 做几个实验,试图看看共同语言如何构建映射到汇编.在我目前的实验中,我试图具体了解C语言指针如何映射到寄存器间接寻址.我写了一个像指针程序一样的hello-world:

#include <stdio.h>

int
main (void)
{
    int value    = 5;
    int *int_val = &value;

    printf ("The value we have is %d\n", *int_val);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

并使用gcc -o pointer.s -fno-asynchronous-unwind-tables pointer.c:: [1] [2] 将其编译为以下asm

        .file   "pointer.c"
        .section        .rodata
.LC0:
        .string "The value we have is %d\n"
        .text
        .globl  main
        .type   main, @function
main:
;------- function prologue
        pushq   %rbp
        movq    %rsp, %rbp
;---------------------------------
        subq    $32, %rsp
        movq    %fs:40, %rax
        movq    %rax, -8(%rbp)
        xorl    %eax, %eax
;----------------------------------
        movl    $5, -20(%rbp)   ; This is where the value 5 is stored in `value` (automatic allocation)
;----------------------------------
        leaq    -20(%rbp), %rax ;; (GUESS) If I have understood correctly, this is where the address of `value` is 
                                ;; extracted, and stored into %rax
;----------------------------------
        movq    %rax, -16(%rbp) ;; 
        movq    -16(%rbp), %rax ;; Why do I have two times the same instructions, with reversed operands???
;----------------------------------
        movl    (%rax), %eax
        movl    %eax, %esi
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
;----------------------------------
        movl    $0, %eax
        movq    -8(%rbp), %rdx
        xorq    %fs:40, %rdx
        je      .L3
        call    __stack_chk_fail
.L3:
        leave
        ret
        .size   main, .-main
        .ident  "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
        .section        .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)

我的问题是,我不明白为什么它包含指令movq两次,反向操作数.有人可以向我解释一下吗?

[1]:当我根本不需要它时,我想避免让我的asm代码穿插cfi指令.

[2]:我的环境是Ubuntu 14.10,gcc 4.9.1(被Ubuntu修改),并且Gnu assembler (GNU Binutils for Ubuntu) 2.24.90.20141014,配置为针对x86_64-linux-gnu

rkh*_*khb 9

如果重组你的块,也许会更清楚:

;----------------------------------
    leaq    -20(%rbp), %rax     ; &value
    movq    %rax, -16(%rbp)     ; int_val
;----------------------------------
    movq    -16(%rbp), %rax     ; int_val
    movl    (%rax), %eax        ; *int_val
    movl    %eax, %esi          ; printf-argument
    movl    $.LC0, %edi         ; printf-argument (format-string)
    movl    $0, %eax            ; no floating-point numbers
    call    printf
;----------------------------------
Run Code Online (Sandbox Code Playgroud)

第一个块执行int *int_val = &value;,第二个块执行printf ....没有优化,块是独立的.