9. Program to Recursive Procedure in Assembly Language using Visual Studio

Chapter 5

Procedures

Assembly Language Programming Exercise

Problem # 9:

Direct recursion is the term we use when a procedure calls itself. Of course, you never want to let a procedure keep calling itself forever, because the runtime stack would fill up. Instead, you must limit the recursion in some way. Write a program that calls a recursive procedure. Inside this procedure, add 1 to a counter so you can verify the number of times it executes. Run your program with a debugger, and at the end of the program, check the counter’s value. Put a number in ECX that specifies the number of times you want to allow the recursion to continue. Using only the LOOP instruction (and no other conditional statements from later chapters), find a way for the recursive procedure to call itself a fixed number of times.

Solution:

INCLUDE Irvine32.inc

.data
count DWORD ?

.code
    main PROC
        mov count, 0
        mov ecx, 10
        call generateRecursion

        exit
    main ENDP

    generateRecursion PROC
        inc count
        cmp count, ecx
        jz quit
        call generateRecursion
        quit:
        ret
    generateRecursion ENDP

END main

Let me know in the comment section if you have any question.

Previous Post:
Program to Color Matrix in Assembly Language using Visual Studio