Problem – Write an assembly language program for calculating the sum of first n natural numbers using 8085 microprocessor.
Input : 04H Output : 0AH as 01+02+03+04 = 10 in decimal => 0AH
The formula for calculating the sum of first n natural numbers is .
- With n as the input, increment it to obtain n+1.
- Multiply n with n+1.
- Divide the product obtained by 2.
In 8085 microprocessor, no direct instruction exists to multiply two numbers, so multiplication is done by repeated addition as 4×5 is equivalent to 4+4+4+4+4 (i.e., 5 times).
Add 04H 5 times
Similarly, in 8085 microprocessor, no direct instruction exists to divide two numbers, so division is done by repeated subtraction.
Keep subtracting 2 from the input till it reduces to 0.
Since subtraction has to be performed 1010 times before 14H becomes 0, the quotient is 1010 => 0AH.
- Load the data from the memory location (201BH, arbitrary choice) into the accumulator
- Move this data into B
- Increment the value in the accumulator by one and move it to the register C
- Initialise the accumulator with 0
- Multiplication: Keep adding B to accumulator. The number of times B has to be added is equal to the value of C
- Initialise B with 00H. B will store the quotient of the division
- Initialise C with 02H. This is the divisor for the division
- Division: Keep subtracting C from A till A becomes 0. For each subtraction, increment B by one
- The final answer is in B. Move it to A. Then store the value of A in 201CH (arbitrary choice again)
201CH contains the final answer.
|2003H||MOV B, A|
|2005H||MOV C, A|
|2006H||MVI A, 00H|
|200DH||MVI C, 02H|
|200FH||MVI B, 00H|
|2016H||MOV A, B|
Store the value of n in 201BH. The sum can be found at 201CH.
- 8085 program to find larger of two 8 bit numbers
- 8085 program to find the sum of series of even numbers
- 8085 program to find maximum of two 8 bit numbers
- 8085 program to find smallest number between two numbers
- 8085 program to find maximum and minimum of 10 numbers
- 8085 program to count total odd numbers in series of 10 numbers
- 8085 program to count total even numbers in series of 10 numbers
- 8085 program to add 2-BCD numbers
- 8085 program to add two 16 bit numbers
- 8085 program to add two 8 bit numbers
- 8085 program to multiply two 16-bit numbers
- 8085 program to multiply two 8 bit numbers
- 8085 program to divide two 8 bit numbers
- 8085 program to add numbers in an array
- 8085 program to swap two 8-bit 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 firstname.lastname@example.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.
Improved By : nidhi_biet