The MASM Forum

General => The Campus => Topic started by: imadhx30 on February 03, 2014, 04:03:17 AM

Title: recursive functions
Post by: imadhx30 on February 03, 2014, 04:03:17 AM
give the recursive functions of this function
n1^0*n2^p+...................+n1^p*n2^0
fonc proc ,n1:DWORD,n2:DWORD,p:DWORD
mov ecx,1
mov esi,p
repete:
cmp i,esi
ja fin
mov eax,ecx
mov ebx,n2
mul ebx
mov ecx,eax
inc i
jmp repete
fin:
mov eax,ecx
repete1:
cmp j,esi
ja fin1
mov ebx,n2
div ebx
mov ebx,n1
mul ebx
add ecx,eax
inc j
jmp repete1
fin1:
mov eax,ecx
ret
fonc endp
end start
Title: Re: recursive functions
Post by: K_F on February 04, 2014, 07:10:18 AM
I was fiddling with some recursive stuff the other day... It's all about complexity and speed...

I made a nice proc that worked only with local vars and parameters, but it was computationally intensive.
I found I could do the same thing with external variables a gazillion times faster that worked in simple loops.

It depends on the 'depth of recursiveness' that you want to go :-
- If the depth is unknown go the complex route - it's slower, but watch your stack
- For know max depth, go loops and external variables = much faster and easier to control.

:)
Title: Re: recursive functions
Post by: hutch-- on February 04, 2014, 09:27:25 AM
Recursion is at its most useful when you need to store each iteration's local data on the stack so that you can either recurse above it or return to it from a higher recursive iteration. Its often used to move up and down a dynamically created tree, a quick sort is one example. If you do not need to store local variables then as Van suggested, a purely iterative approach using global variables is usually a lot faster as it does not have the stack overhead. It is usually a lot less memory intensive as well.
Title: Re: recursive functions
Post by: habran on February 04, 2014, 02:11:35 PM
have look here (http://www.tutorialspoint.com/cprogramming/c_recursion.htm) 8)