- 8051

8051 code to find a square of first 10 natural numbers and store it in an array.

8051 code to find a square of first 10 natural numbers and store it in an array.

In this article let’s learn how to find the square of first 10 natural numbers and store them in an array.

Algorithm:

1)Initialize R0 with memory address where all squares are stored and R1 with 0.
2)Increment R1 to find the square of a number.
3)Find the square of a number by multiplying A and B with same number.
4)Store the result with the assigned address array.
5)Check whether R1 is 10 and if it not 10 repeat the above steps.
In this program let’s see how to find a square of first 10 natural numbers and store it in an array.

Code goes here:



ORG 0000h
LJMP MAIN
ORG 40h
MAIN: MOV R0,#10H ; memory address where all the squares will be stored
MOV R1,#00
LOOP: INC R1 ;incrementing each time to generate a new square
MOV A,R1
MOV B,R1
MUL AB ;finding the square of a number
MOV @R0,A ;storing the square of all the numbers in an array
INC R0
CJNE R1,#10,LOOP ; checking the counter value whether it has reached zero
END

Leave a Reply