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?
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
%eaxregister 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%espback 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.