简体   繁体   English

汇编-如何查找函数为其分配多少堆栈空间

[英]Assembly - How To Find How Much Stack Space A Function Allocates For Itself

I'm working with the GDB debugger right now. 我现在正在使用GDB调试器。 Question: In the disassembly file, locate the definition of main. 问题:在反汇编文件中,找到main的定义。 How much stack space (decimal number) does this function allocate for itself? 此函数为其分配多少堆栈空间(十进制数)?

Here is the function: 这是函数:

08048460 <main>:
8048460:       55                      push   %ebp
8048461:       89 e5                   mov    %esp,%ebp
8048463:       83 e4 f0                and    $0xfffffff0,%esp
8048466:       83 ec 40                sub    $0x40,%esp
8048469:       b8 db 0f 49 40          mov    $0x40490fdb,%eax
804846e:       89 44 24 08             mov    %eax,0x8(%esp)
8048472:       b8 ec 78 ad e0          mov    $0xe0ad78ec,%eax
8048477:       89 44 24 04             mov    %eax,0x4(%esp)
804847b:       b8 ec 78 ad 60          mov    $0x60ad78ec,%eax
8048480:       89 04 24                mov    %eax,(%esp)
8048483:       e8 bc ff ff ff          call   8048444 <fn1>
8048488:       d9 5c 24 3c             fstps  0x3c(%esp)
804848c:       b8 db 0f 49 40          mov    $0x40490fdb,%eax
8048491:       89 44 24 08             mov    %eax,0x8(%esp)
8048495:       b8 ec 78 ad e0          mov    $0xe0ad78ec,%eax
804849a:       89 44 24 04             mov    %eax,0x4(%esp)
804849e:       b8 ec 78 ad 60          mov    $0x60ad78ec,%eax
80484a3:       89 04 24                mov    %eax,(%esp)
80484a6:       e8 a7 ff ff ff          call   8048452 <fn2>
80484ab:       d9 5c 24 38             fstps  0x38(%esp)
80484af:       c7 04 24 e0 85 04 08    movl   $0x80485e0,(%esp)
80484b6:       e8 c5 fe ff ff          call   8048380 <puts@plt>
80484bb:       d9 44 24 3c             flds   0x3c(%esp)
80484bf:       d8 64 24 38             fsubs  0x38(%esp)
80484c3:       dd 1c 24                fstpl  (%esp)
80484c6:       e8 95 fe ff ff          call   8048360 <abs@plt>
80484cb:       89 44 24 2c             mov    %eax,0x2c(%esp)
80484cf:       db 44 24 2c             fildl  0x2c(%esp)
80484d3:       dd 05 20 86 04 08       fldl   0x8048620
80484d9:       d9 c9                   fxch   %st(1)
80484db:       da e9                   fucompp
80484dd:       df e0                   fnstsw %ax
80484df:       9e                      sahf
80484e0:       0f 97 c0                seta   %al
80484e3:       84 c0                   test   %al,%al
80484e5:       74 21                   je     8048508 <main+0xa8>
80484e7:       d9 44 24 38             flds   0x38(%esp)
80484eb:       d9 44 24 3c             flds   0x3c(%esp)
80484ef:       d9 c9                   fxch   %st(1)
80484f1:       b8 e8 85 04 08          mov    $0x80485e8,%eax
80484f6:       dd 5c 24 0c             fstpl  0xc(%esp)
80484fa:       dd 5c 24 04             fstpl  0x4(%esp)
80484fe:       89 04 24                mov    %eax,(%esp)
8048501:       e8 6a fe ff ff          call   8048370 <printf@plt>
8048506:       eb 0c                   jmp    8048514 <main+0xb4>
8048508:       c7 04 24 08 86 04 08    movl   $0x8048608,(%esp)
804850f:       e8 6c fe ff ff          call   8048380 <puts@plt>
8048514:       b8 00 00 00 00          mov    $0x0,%eax
8048519:       c9                      leave
804851a:       c3                      ret
804851b:       90                      nop
804851c:       90                      nop
804851d:       90                      nop
804851e:       90                      nop
804851f:       90                      nop

I don't understand how to determine this. 我不知道如何确定。 Is the question asking how much stack space the function allocates for itself before it's called or during the process it was being called? 是在问函数在调用之前或在调用过程中为其分配多少堆栈空间吗? Is it asking how many bytes or how many registers in the stack are being taken up by this function? 它问这个函数占用了多少字节或堆栈中的多少个寄存器吗?

I'm new to assembly, so is there a certain technique I should use to answer this question? 我是组装的新手,所以应该使用某种技术来回答这个问题吗?

First there's a push ebp, followed by rounding esp down to a 16 byte boundary, then 0x40 is subtracted from esp. 首先是一个推式ebp,然后将esp向下舍入到16字节边界,然后从esp中减去0x40。 This could get further complicated if something like _alloca() was used to dynamically allocate space from the stack. 如果使用_alloca()之类的方法从堆栈中动态分配空间,则可能会变得更加复杂。

This line: 这行:

8048466:       83 ec 40                sub    $0x40,%esp

tells you that the space allocated is 0x40 (64) bytes. 告诉您分配的空间为0x40(64)字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM