简体   繁体   中英

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:

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 ?

  2. So we call factR() . Will the result of that be in %esp when we come back here to the next instructions?

  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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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