【发布时间】:2011-12-29 08:55:09
【问题描述】:
我正在学习汇编,我有这个函数,其中包含一些我不明白的行:
. 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 以获得下一个数字或类似的东西?
【问题讨论】:
-
C 代码应为
if ( n == 1 ) return 1; else return n ∗ factR ( n − 1 );以等效于汇编代码。 (另外,这是阶乘的正确实现)
标签: c assembly intel factorial i386