8085 program to find the factorial of a number
Problem – Write an assembly language program for calculating the factorial of a number using 8085 microprocessor.
Input : 04H Output : 18H as 04*03*02*01 = 24 in decimal => 18H
In 8085 microprocessor, no direct instruction exists to multiply two numbers, so multiplication is done by repeated addition as 4×3 is equivalent to 4+4+4 (i.e., 3 times).
Load 04H in D register -> Add 04H 3 times -> D register now contains 0CH -> Add 0CH 2 times -> D register now contains 18H -> Add 18H 1 time -> D register now contains 18H -> Output is 18H
- Load the data into register B
- To start multiplication set D to 01H
- Jump to step 7
- Decrements B to multiply previous number
- Jump to step 3 till value of B>0
- Take memory pointer to next location and store result
- Load E with contents of B and clear accumulator
- Repeatedly add contents of D to accumulator E times
- Store accumulator content to D
- Go to step 4
|2001H||Result||Result of factorial|
|2002H||LXI H, 2000H||Load data from memory|
|2005H||MOV B, M||Load data to B register|
|2006H||MVI D, 01H||Set D register with 1|
|2008H||FACTORIAL||CALL MULTIPLY||Subroutine call for multiplication|
|200BH||DCR B||Decrement B|
|200CH||JNZ FACTORIAL||Call factorial till B becomes 0|
|200FH||INX H||Increment memory|
|2010H||MOV M, D||Store result in memory|
|2100H||MULTIPLY||MOV E, B||Transfer contents of B to C|
|2101H||MVI A, 00H||Clear accumulator to store result|
|2103H||MULTIPLYLOOP||ADD D||Add contents of D to A|
|2104H||DCR E||Decrement E|
|2105H||JNZ MULTIPLYLOOP||Repeated addition|
|2108H||MOV D, A||Transfer contents of A to D|
|2109H||RET||Return from subroutine|
- First set register B with data.
- Set register D with data by calling MULTIPLY subroutine one time.
- Decrement B and add D to itself B times by calling MULTIPLY subroutine as 4*3 is equivalent to 4+4+4 (i.e., 3 times).
- Repeat the above step till B reaches 0 and then exit the program.
- The result is obtained in D register which is stored in memory
Learn CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.