了解基本指针和堆栈指针:在用gcc输出上下文 [英] Understanding Base Pointer and Stack Pointers: In Context with gcc Output

查看:201
本文介绍了了解基本指针和堆栈指针:在用gcc输出上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的C程序:

int main()
{
    int c[10] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2};
    return c[0];
}

和使用时使用gcc的-S指令编译我得到以下组件:

and when compiled using the -S directive with gcc I get the following assembly:

    .file   "array.c"
    .text
.globl main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $0, -48(%rbp)
    movl    $0, -44(%rbp)
    movl    $0, -40(%rbp)
    movl    $0, -36(%rbp)
    movl    $0, -32(%rbp)
    movl    $0, -28(%rbp)
    movl    $0, -24(%rbp)
    movl    $0, -20(%rbp)
    movl    $1, -16(%rbp)
    movl    $2, -12(%rbp)
    movl    -48(%rbp), %eax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.4.5 20110214 (Red Hat 4.4.5-6)"
    .section        .note.GNU-stack,"",@progbits

我不明白的是为什么是从BP早前数组元素进一步?它似乎像阵列上的元件被放置在相反的顺序。

What I do not understand is why are the earlier array elements further from the bp? It almost seems like the elements on the array are being placed in opposite order.

此外,为什么不GCC使用推,而不是MOVL,数组元素压入栈?

Also why does gcc not use push instead of movl, to push the array elements onto the stack?

不同的看法

移动阵列全局命名空间作为一个静态变量的模块我得到:

Moving the array to global namespace as a static variable to the module I get:

    .file   "array.c"
    .data
    .align 32
    .type   c, @object
    .size   c, 40
c:
    .long   0
    .long   0
    .long   0
    .long   0
    .long   0
    .long   0
    .long   0
    .long   0
    .long   1
    .long   2
    .text
.globl main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    c(%rip), %eax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.4.5 20110214 (Red Hat 4.4.5-6)"
    .section    .note.GNU-stack,"",@progbits

使用下面的C程序:

static int c[10] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2};

int main() 
{
    return c[0];
}

这并没有给更多的有识之士到堆栈中。但它野趣看到使用语义稍有不同组件的输出differement

This doesn't give more insight to the stack. But it is intersting to see the differement output of assembly using slightly different semantics.

推荐答案

首先,在x86堆栈向下增长。按照惯例, RBP 存储 RSP 的初始值。因此,函数的参数居住在的的相对 RBP ,它的自动变量偏移居住在的的偏移量。自动数组的第一个元素具有比所有其他元素一个较低的地址,因而是最远的距离 RBP

First of all, the x86 stack grows downwards. By convention, rbp stores the original value of rsp. Therefore, the function's arguments reside at positive offsets relative to rbp, and its automatic variables reside at negative offsets. The first element of an automatic array has a lower address than all other elements, and thus is the furthest away from rbp.

下面是出现在这个页面一个方便的图:

Here is a handy diagram that appears on this page:

我不明白为什么编译器的无法的使用一系列指令来初始化数组。这是否是一个好主意,我不知道。

I see no reason why the compiler couldn't use a series of push instructions to initialize your array. Whether this would be a good idea, I am not sure.

这篇关于了解基本指针和堆栈指针:在用gcc输出上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆