了解汇编递归函数
我正在学习汇编,我有一个函数,其中包含一些我不明白的行:
. 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
与它对应的 C 代码是:
int factR ( int n ) {
if ( n != 0 )
return n;
else
return n ∗ factR ( n − 1 );
}
我不确定标有数字的行。
pushl %eax
:是否意味着我们将%eax
的内容放入%esp
?所以我们调用
factR()
。当我们回到这里执行下一条指令时,结果会在%esp
中吗?addl $4,%esp
不确定这个,我们是在%esp
中存储的数字上加 4 还是在指针上加 4 以获得下一个数字或类似的数字?
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:
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.
pushl %eax
: does it mean we put the contents of%eax
in%esp
?So we call
factR()
. Will the result of that be in%esp
when we come back here to the next instructions?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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来
factR()
函数遵循 C 调用约定 (cdecl< /a>)。这是调用者将函数调用的参数推入堆栈的地方,调用者清理堆栈(撤消为执行该函数而对堆栈所做的更改)调用)当函数返回时。第一个推送 (1) 将
%eax
寄存器的内容作为以下调用的参数。然后实际调用该函数 (2)。然后,通过将堆栈指针%esp
重置回步骤 1 中没有推回参数时的状态来清理堆栈 (3)。它推入了一个 32 位值,因此必须将指针调整 4 个字节。It appears that the
factR()
function follows the C calling convention (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. Then the actual call to the function is made (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.