Skip to content
Related Articles

Related Articles

8085 program to find the factorial of a number
  • Difficulty Level : Hard
  • Last Updated : 07 Sep, 2018

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

Data result store

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

factorial iterations

Algorithm –

  1. Load the data into register B
  2. To start multiplication set D to 01H
  3. Jump to step 7
  4. Decrements B to multiply previous number
  5. Jump to step 3 till value of B>0
  6. Take memory pointer to next location and store result
  7. Load E with contents of B and clear accumulator
  8. Repeatedly add contents of D to accumulator E times
  9. Store accumulator content to D
  10. Go to step 4
2000HDataData Byte
2001HResultResult of factorial
2002HLXI H, 2000HLoad data from memory
2005HMOV B, MLoad data to B register
2006HMVI D, 01HSet D register with 1
2008HFACTORIALCALL MULTIPLYSubroutine call for multiplication
200BHDCR BDecrement B
200CHJNZ FACTORIALCall factorial till B becomes 0
200FHINX HIncrement memory
2010HMOV M, DStore result in memory
2100HMULTIPLYMOV E, BTransfer contents of B to C
2101HMVI A, 00HClear accumulator to store result
2103HMULTIPLYLOOPADD DAdd contents of D to A
2104HDCR EDecrement E
2105HJNZ MULTIPLYLOOPRepeated addition
2108HMOV D, ATransfer contents of A to D
2109HRETReturn from subroutine

Explanation –

  1. First set register B with data.
  2. Set register D with data by calling MULTIPLY subroutine one time.
  3. 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).
  4. Repeat the above step till B reaches 0 and then exit the program.
  5. The result is obtained in D register which is stored in memory
My Personal Notes arrow_drop_up
Recommended Articles
Page :