### Author Topic: Problem when trying to write decimal inside a recursive function  (Read 2028 times)

#### HarryBao

• Regular Member
• • Posts: 1 ##### Problem when trying to write decimal inside a recursive function
« on: November 04, 2020, 06:23:25 PM »
Hello everyone, can you help me with this particular problem I'm facing when I try to write decimal inside of a recursive function? Here's the code, and the function basically calculates the sum of an input integer up to and including that number using recursive calls.
Code: [Select]
`INCLUDE Irvine32.inc.dataprompt byte "Please enter a number: ",0text1 byte "Just entered, about to calculate sum using ",0number dword ?.codemain PROC    mov edx, offset prompt    call WriteString    call ReadInt    mov number,eax    mov ecx,number    mov eax,0      call CalcSumL1:        call WriteDec    call Crlf    exitmain ENDPCalcSum PROC      cmp ecx, 0    jz L2    add eax,ecx    dec ecx    call CalcsumL2:    retCalcsum ENDPEND main`What I'm stuck on is that every time the recursive function is called, 2 sets of messages will pop up, saying:
"Just entered, about to calculate sum using xxx" where xxx is the value it received and "About to return yyy as my answer for xxx" where xxx is again what it received and yyy is what it calculated. For example, if the input number is 2. I want the message to show:
"Just entered, about to calculate sum using 2"
"About to return 1 as my answer for 2"
"Just entered, about to calculate sum using 1"
"About to return 3 as my answer for 1"
In conclusion, I don't know how to save the number values after each changed so that they are saved every time a recursive function is called to be output onto the screen. By the way, using functions inside of the Irvine library would be great! Thank you for your time and consideration!

#### raymond ##### Re: Problem when trying to write decimal inside a recursive function
« Reply #1 on: November 05, 2020, 04:49:55 AM »
Simply loop within your CalcSum procedure:
Code: [Select]
`CalcSum PROCL0:    add eax,ecx    dec ecx    jnz L0    retCalcSum ENDP`
Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com/