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%eaxin%esp?So we call
factR(). Will the result of that be in%espwhen we come back here to the next instructions?addl $4,%espnot sure about this one, are we adding 4 to the number stored in%espor do we add 4 to the pointer to get the next number or something similar?
if ( n == 1 ) return 1; else return n ∗ factR ( n − 1 );to be equivalent to the assembly code. (Also, this is the correct implementation for factorial)