2

我不明白为什么gcc -S -m32会产生这些特定的代码行:

movl    %eax, 28(%esp)
movl    $desc, 4(%esp)
movl    28(%esp), %eax
movl    %eax, (%esp)
call    sort_gen_asm

我的问题是为什么%eax被推然后弹出?为什么分别movl使用而不是pushlpopl?它更快吗?有一些我还不知道的编码约定吗?我刚刚开始仔细研究 asm-output,所以我不太了解。

C代码:

void print_array(int *data, size_t sz);
void sort_gen_asm(array_t*, comparer_t);

int main(int argc, char *argv[]) {
    FILE *file;
    array_t *array;

    file = fopen("test", "rb");
    if (file == NULL) { 
        err(EXIT_FAILURE, NULL);
    }   

    array = array_get(file);
    sort_gen_asm(array, desc); 
    print_array(array->data, array->sz);

    array_destroy(array);
    fclose(file); 

    return 0;
}

它给出了这个输出:

    .file   "main.c"
    .section    .rodata
.LC0:
    .string "rb"
.LC1:
    .string "test"
    .text
    .globl  main
    .type   main, @function
main:
.LFB2:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $32, %esp
    movl    $.LC0, 4(%esp)
    movl    $.LC1, (%esp)
    call    fopen
    movl    %eax, 24(%esp)
    cmpl    $0, 24(%esp)
    jne .L2
    movl    $0, 4(%esp)
    movl    $1, (%esp)
    call    err
.L2:
    movl    24(%esp), %eax
    movl    %eax, (%esp)
    call    array_get
    movl    %eax, 28(%esp)
    movl    $desc, 4(%esp)
    movl    28(%esp), %eax
    movl    %eax, (%esp)
    call    sort_gen_asm
    movl    28(%esp), %eax
    movl    4(%eax), %edx
    movl    28(%esp), %eax
    movl    (%eax), %eax
    movl    %edx, 4(%esp)
    movl    %eax, (%esp)
    call    print_array
    movl    28(%esp), %eax
    movl    %eax, (%esp)
    call    array_destroy
    movl    24(%esp), %eax
    movl    %eax, (%esp)
    call    fclose
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE2:
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1"
    .section    .note.GNU-stack,"",@progbits
4

2 回答 2

6

的保存/加载eax是因为您没有使用优化进行编译。因此,对变量的任何读/写都会发出对内存地址的读/写。

实际上,对于(几乎)任何一行代码,您都可以识别出由它产生的确切的汇编代码(让我建议您使用gcc -g -c -O0then进行编译objdump -S file.o):

#array = array_get(file); 
call array_get
movl    %eax, 28(%esp) #write array

#sort_gen_asm(array, desc); 
movl    28(%esp), %eax  #read array
movl    %eax, (%esp)
...

关于不推送/弹出,这是一个标准的零成本优化。每次你想调用一个函数时,你只需将所需的最大空间减去函数esp开头的最大空间,然后将函数参数保存在空白空间的底部,而不是每次推送/弹出。有很多优点:更快的代码(不改变esp),它不需要以任何特定的顺序计算参数,并且esp无论如何都需要减去局部变量空间。

于 2013-11-07T11:54:19.597 回答
5

有些事情与调用约定有关。其他有优化的。

sort_gen_asm似乎使用cdecl调用约定,它要求将其参数以相反的顺序推入堆栈。因此:

movl    $desc, 4(%esp)
movl    %eax, (%esp)

其他动作是部分未优化的编译器例程:

movl    %eax, 28(%esp) # save contents of %eax on the stack before calling
movl    28(%esp), %eax # retrieve saved 28(%esp) in order to prepare it as an argument
                       # Unoptimised compiler seems to have forgotten that it's
                       # still in the register
于 2013-11-07T11:54:14.590 回答