Skip to content
Related Articles

Related Articles

8085 program to find nth power of a number

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 07 Sep, 2018

Problem – Write an assembly language code for calculating the nth power of a number using 8085 microprocessor.

Example –

Input : Base=>02H
Output :08H 

In 8085 microprocessor, no direct instruction exists to multiply two numbers, so multiplication is done by repeated addition as 4*4 is equivalent to 4+4+4+4(ie 4 times).
Load 02H(base) to register B and 03H(exponent) to register C -> set D register to 02H -> Add 02H B(ie 2) times -> D register now contains 04H -> Add 04H B(ie 2) times -> D register now contains 08H -> Output is 08H.

Algorithm –

  1. Load the base into register B and exponent into register C.
  2. To start multiplication set D to 01H.
  3. Jump to step 7.
  4. Decrements C.
  5. Jump to step 3 till value of C>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.

Program –

2000HBaseData Byte for base
2001HExponentData Byte for exponent
2002HResultResult of factorial
2003HLXI H, 2000HLoad data from memory
2006HMOV B, MLoad base to B register
2007HINX HIncrement memory
2008HMOV C, MLoad exponent to C register
2009HMVI D, 01HSet D register to 1
200BHPOWER_LOOPCALL MULTIPLYSubroutine call for multiplication
200EHDCR CDecrement C
200FHJNZ POWER_LOOPCall power_loop till C becomes 0
2012HINX HIncrement memory
2013HMOV M, DStore result in memory
2100HMULTIPLYMOV E, BTransfer contents of B to E
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. Set register B with base and register C with exponent.
  2. Set register D with base by calling MULTIPLY subroutine one time.
  3. Decrement C and add D to itself B times by calling MULTIPLY subroutine.
  4. Repeat the above step till C 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 :

Start Your Coding Journey Now!