You are on page 1of 2

SE Computer Engineering 2019 Course

Assignment No. 10

Title: Write x86 ALP to find the factorial of a given integer number on a command line by using
recursion. Explicit stack manipulation is expected in the code.
Working Environment:
1. CPU – Core 2 Duo, 64bit with 2.3 GHz clock frequency
2. OS – Fedora 18, 64bit
Tools:

• Editor – gedit, a GNU editor


• Assembler – NASM (Netwide Assembler)
• LINKER – LD , a GNU linker
Theory:

A recursive procedure is one that calls itself. There are two kind of recursion:
i) Direct
ii) Indirect.

In direct recursion, the procedure calls itself and in indirect recursion, the first procedure calls a second
procedure, which in turn calls the first procedure.
Recursion could be observed in numerous mathematical algorithms.
For example, consider the case of calculating the factorial of a number. Factorial of a number is given
by the equation −

Fact (n) = n * fact (n-1) for n > 0

For example: factorial of 5 is 1 x 2 x 3 x 4 x 5 = 5 x factorial of 4 and this can be a good example of


showing a recursive procedure. Every recursive algorithm must have an ending condition, i.e., the
recursive calling of the program should be stopped when a condition is fulfilled. In the case of
factorial algorithm, the end condition is reached when n is 0.

Recursion & Stack

Most compilers implement recursion using stacks. When a method is called the compiler pushes the
arguments to the method and the return address on the stack and then transfers the control to the
method. When the method returns, it pops these values off the stack. The arguments disappear and
the control returns to the return address
SE Computer Engineering 2019 Course

Algorithm:
1. Start.
2. Create the text file & write the input data into it and save.
3. Initialize required variables in .data section
4. Declare required variables in .bss section
5. Define macros to display message/data and accept numbers
6. In .text section, display msg "*****Program to find Factorial of a number***** "
7. Display msg "Enter the number : "
8. if(num==1)
9. Display "Factorial is : 0000001"
10. Else
11. call fact_proc (Goto step 14)
12. Exit
13. Stop
14. fact_proc Algorithm:
if (num===1)
{
call Calculate_proc
}
else
{
move 1 into Accumulator
}
Return (goto step 11)

Calculation Procedure:
i) num=num-1
ii) call fact_proc
iii) num=num+1
iv) Content of Accumulator * num
v) Return

Conclusion: Thus we find the factorial of a given integer number on a command line by using
recursion.

You might also like