简体   繁体   English

了解程序集递归函数

[英]Understanding assembly recursive function

I am learning assembly and I have this function that contains some lines I just don't understand: 我正在学习汇编,并且我具有包含一些我不明白的几行的函数:

. globl
. text

factR:
 cmpl $0 ,4(% esp )
 jne cont
 movl $1 ,%eax
 ret

cont :
 movl 4(%esp),%eax
 decl %eax
 pushl %eax          // (1)
 call factR          // (2)
 addl $4,%esp        // (3)
 imull 4(%esp),%eax 
 ret

and the C code corresponding to it is: 对应的C代码为:

int factR ( int n ) {
    if ( n != 0 )
        return n;
    else
        return n ∗ factR ( n − 1 );
}

I am not sure about the lines marked with numbers. 我不确定标有数字的行。

  1. pushl %eax : does it mean we put the contents of %eax in %esp ? pushl %eax :这是否意味着我们将%eax的内容放入%esp

  2. So we call factR() . 因此,我们将其称为factR() Will the result of that be in %esp when we come back here to the next instructions? 当我们回到这里下一条指令时,结果将在%esp吗?

  3. addl $4,%esp not sure about this one, are we adding 4 to the number stored in %esp or do we add 4 to the pointer to get the next number or something similar? addl $4,%esp不确定这一点,我们将4添加到%esp存储的数字还是将4添加到指针以获得下一个数字或类似的东西?

It appears that the factR() function follows the C calling convention ( cdecl ). 事实是factR()函数遵循C调用约定( cdecl )。 It is where the caller pushes the arguments to the function call onto the stack and the caller cleans up the stack (undoes the changes to the stack that was made to do the function call) when the function returns. 在此, 调用者将函数调用的参数推送到堆栈上,并且在函数返回时, 调用者将清理堆栈(撤消对执行函数调用所做的堆栈更改)。

The first push (1) is putting the contents of the %eax register as the argument to the following call. 第一次推送(1)将%eax寄存器的内容作为后续调用的参数。 Then the actual call to the function is made (2). 然后对函数进行实际调用(2)。 Then the stack is cleaned (3) by resetting the stack pointer %esp back to the state when it didn't have the argument pushed back in step 1. It pushed one 32-bit value so it must adjust the pointer by 4-bytes. 然后,通过将堆栈指针%esp重置为没有在步骤1中推回参数的状态来清理(3)堆栈。它压入了一个32位值,因此必须将指针调整4个字节。

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

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