# 8085 program to find nth power of a number

• Last Updated : 07 Sep, 2018

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

Example –

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

```Input : Base=>02H
Exponent=>03H
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.
4. Decrements C.
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
2014HHLTHalt
2100HMULTIPLYMOV E, BTransfer contents of B to E
2101HMVI A, 00HClear accumulator to store result
2104HDCR EDecrement E