# 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.

**Example –**

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

**Algorithm –**

- 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

Address | Label | Mnemonic | Comment |
---|---|---|---|

2000H | Data | Data Byte | |

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 | |

2011H | HLT | Halt | |

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 |

**Explanation –**

- 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

## Recommended Posts:

- 8085 program to find square of a 8 bit number
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find nth power of a number
- 8085 program to find 1’s and 2’s complement of 16-bit number
- 8085 program to find sum of digits of 8 bit number
- 8085 program to find square root of a number
- 8085 program to find minimum value of digit in the 8 bit number
- 8085 program to find smallest number between two numbers
- 8086 program to find the factorial of a number
- 8085 program to find the sum of a series
- 8085 program to find the set bit of accumulator
- 8085 program to find maximum of two 8 bit numbers
- 8085 program to find the element that appears once
- 8085 program to find the sum of first n natural numbers
- 8085 program to find the sum of series of even numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.