The relationship between function recursion and stack

Source: Internet
Author: User
Tags dname

First, through the anti-assembly language, let's look at the simplest recursive function and the relationship between the stack.

How to get anti-assembly language, in Visual Studio 2008, in the debug environment, you can view the post-disassembly languages in debug/windows/disassembly. Now let's take a look at factorial n! The implementation

Its C language implementation code is as follows

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. int factorial (int n);
  3. int main (void)
  4. {
  5. int fact;
  6. Fact = factorial (4);
  7. printf ("%d\n", fact);
  8. return 0;
  9. }
  10. int factorial (int n)
  11. {
  12. if (1 = = N)
  13. return 1;
  14. return n * factorial (n-1);
  15. }


The following languages are disassembled:

Main program Main

[Plain]View Plaincopy
  1. int main (void)
  2. {
  3. 00DB1FD0 Push EBP
  4. 00DB1FD1 mov Ebp,esp
  5. 00db1fd3 Sub Esp,0cch
  6. 00DB1FD9 push EBX
  7. 00DB1FDA push ESI
  8. 00db1fdb Push EDI
  9. 00DB1FDC Lea EDI,[EBP-0CCH]
  10. 00DB1FE2 mov ecx,33h
  11. 00DB1FE7 mov eax,0cccccccch
  12. 00DB1FEC Rep STOs dword ptr Es:[edi]
  13. int fact;
  14. Fact = factorial (4);
  15. 00db1fee Push 4
  16. 00DB1FF0 call @ILT +475 (_factorial) (0db11e0h)
  17. 00db1ff5 Add esp,4
  18. 00DB1FF8 mov dword ptr [Fact],eax
  19. printf ("%d\n", fact);
  20. 00DB1FFB mov Esi,esp
  21. 00DB1FFD mov eax,dword ptr [fact]
  22. 00db2000 push EAX
  23. 00db2001 Push offset string "%d\n" (0db5a38h)
  24. 00db2006 call DWORD ptr [__imp__printf (0DB82BCH)]
  25. 00db200c Add esp,8
  26. 00DB200F CMP ESI,ESP
  27. 00db2011 call @ILT +320 (__RTC_CHECKESP) (0db1145h)
  28. return 0;


The assembly of its factorial function is as follows

[Plain]View Plaincopy
  1. int factorial (int n)
  2. {
  3. 00DB1AF0 Push EBP
  4. 00DB1AF1 mov Ebp,esp
  5. 00db1af3 Sub esp,0c0h
  6. 00DB1AF9 push EBX
  7. 00db1afa push ESI
  8. 00DB1AFB Push EDI
  9. 00DB1AFC Lea EDI,[EBP-0C0H]
  10. 00DB1B02 mov ecx,30h
  11. 00DB1B07 mov eax,0cccccccch
  12. 00DB1B0C Rep STOs dword ptr Es:[edi]
  13. if (1 = = N)
  14. 00DB1B0E cmp DWORD ptr [n],1
  15. 00DB1B12 jne factorial+2bh (0DB1B1BH)
  16. return 1;
  17. 00DB1B14 mov eax,1
  18. 00db1b19 jmp Factorial+3eh (0db1b2eh)
  19. return n * factorial (n-1);
  20. 00DB1B1B mov Eax,dword ptr [n]
  21. 00db1b1e Sub eax,1
  22. 00DB1B21 push EAX
  23. 00db1b22 call @ILT +475 (_factorial) (0db11e0h)
  24. 00db1b27 Add esp,4
  25. 00DB1B2A Imul Eax,dword ptr [n]
  26. }
  27. 00DB1B2E Pop EDI
  28. 00DB1B2F pop ESI
  29. 00db1b30 pop ebx
  30. 00db1b31 Add esp,0c0h
  31. 00DB1B37 CMP EBP,ESP
  32. 00db1b39 call @ILT +320 (__RTC_CHECKESP) (0db1145h)
  33. 00DB1B3E mov esp,ebp
  34. 00DB1B40 Pop EBP
  35. 00DB1B41 ret


Throughout the assembly, the

[Plain]View Plaincopy
    1. Call @ILT +475 (_factorial) (0db11e0h)

The previous push is the stack of arguments. Here is the key, the other push we can think of is the system for the balance of the stack of necessary operations.

In the disassembly of factorial,

[Plain]View Plaincopy
    1. 00db1b39 call @ILT +320 (__RTC_CHECKESP) (0db1145h)

This sentence is the function factorial calls itself, that is, recursion.

Push eax, save the parameters of each stack into the EAX register, and then into the stack, so in n! = 1 o'clock, each parameter will be in the stack;

[Plain]View Plaincopy
    1. 00DB1B2A Imul Eax,dword ptr [n]

This step is for multiplication. Holds the multiplied value in the EAX register.

In fact, throughout the process, involving a series of operations on the stack frame in a function call, http://blog.csdn.net/free2011/article/details/6868334 this blog details a series of operations that call the stack frame during the function.

Make a summary:

function recursion is to use the stack in the system to operate, through a series of operations on the stack frame, so as to achieve recursion. This process is done by the system.

In factorial, we use the parameters of the factorial function into the stack, and then through the stack frame of a series of operations, so as to achieve the parameters of the stack, and then complete the factorial of this action. The whole process is actually a stack-up and stack-up problem.

Now we are going to implement recursive functions by defining a stack ourselves.

[CPP]View Plaincopy
  1. #include "Stack.h"
  2. #define Numofstack 10
  3. int main (void)
  4. {
  5. Stacknode * Pstacknode = NULL;
  6. int noffact;
  7. int tmp = 1,sum = 1;
  8. Pstacknode = Createstack (Numofstack);
  9. printf ("The number of factorial\n");
  10. scanf ("%d", &noffact);
  11. While (noffact)
  12. {
  13. Push (pstacknode,noffact--);
  14. }
  15. While (pstacknode->top)
  16. {
  17. Pop (PSTACKNODE,&TMP);
  18. Sum *= tmp;
  19. }
  20. printf ("sum is%d\n", sum);
  21. return 0;
  22. }


Only the main program section is rendered. In the main program, we first load the parameters into the stack, that is, N, n-1 、... 1 into the stack, and then out of the stack to operate.

import org.junit.Test;/** * Created by Yuchao on 2015/9/13.*/ Public classDigui {@Test Public voidHello () {DG ("Recursive initial",0); }     Public voidDG (String Dname,inti) {System. out. println ("DG ("+dname+","+i+")"); I++; if(i<4) {DG ("Upper Recursion", i); DG ("Lower level recursion", i); }Else{System. out. println ("--"); }    }}


DG (recursive initial, 0)
DG (Upper recursion, 1)
DG (Upper recursion, 2)
DG (Upper recursion, 3)
--
DG (Lower recursion, 3)
--
DG (Lower recursion, 2)
DG (Upper recursion, 3)
--
DG (Lower recursion, 3)
--
DG (Lower recursion, 1)
DG (Upper recursion, 2)
DG (Upper recursion, 3)
--
DG (Lower recursion, 3)
--
DG (Lower recursion, 2)
DG (Upper recursion, 3)
--
DG (Lower recursion, 3)
--

Process finished with exit code 0

This article is written in a more general, I would like to tell you that by watching the anti-assembly language on the recursive implementation of the process and steps of the procedure, can deepen our understanding of function recursion and stack. Although the assembly language is somewhat difficult to understand, but by reading the above for everyone to recommend blog, I believe we can understand.

The relationship between function recursion and stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.