Problem when trying to write decimal inside a recursive function

Started by HarryBao, November 04, 2020, 06:23:25 PM

Previous topic - Next topic

HarryBao

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.
INCLUDE Irvine32.inc

.data
prompt byte "Please enter a number: ",0
text1 byte "Just entered, about to calculate sum using ",0
number dword ?
.code
main PROC
    mov edx, offset prompt
    call WriteString
    call ReadInt
    mov number,eax
    mov ecx,number
    mov eax,0 
    call CalcSum
L1:
   
    call WriteDec
    call Crlf
    exit
main ENDP

CalcSum PROC

 
    cmp ecx, 0
    jz L2
    add eax,ecx
    dec ecx
    call Calcsum
L2:
    ret
Calcsum ENDP
END 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

Simply loop within your CalcSum procedure:
CalcSum PROC
L0:
    add eax,ecx
    dec ecx
    jnz L0
    ret
CalcSum ENDP

Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com