Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In x86, why do I have the same instruction two times, with reversed operands?

I am doing several experiments with x86 asm trying to see how common language constructs map into assembly. In my current experiment, I am trying to see specifically how C language pointers map to register-indirect addressing. I have written a fairly hello-world like pointer program:

#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;
}

and compiled it to the following asm using: gcc -o pointer.s -fno-asynchronous-unwind-tables pointer.c:[1][2]

        .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

My issue is that I don't understand why it contains the instruction movq two times, with reversed operands. Could someone explain it to me?

[1]: I want to avoid having my asm code interspersed with cfi directives when I don't need them at all.

[2]: My environment is Ubuntu 14.10, gcc 4.9.1 (modified by ubuntu), and Gnu assembler (GNU Binutils for Ubuntu) 2.24.90.20141014, configured to target x86_64-linux-gnu

like image 684
NlightNFotis Avatar asked Jan 27 '15 16:01

NlightNFotis


1 Answers

Maybe it will be clearer if you reorganize your blocks:

;----------------------------------
    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
;----------------------------------

The first block performs int *int_val = &value;, the second block performs printf .... Without optimization, the blocks are independent.

like image 170
rkhb Avatar answered Nov 12 '22 08:11

rkhb