Monday, October 26, 2015

8085 Program to Find the Square of a Number

Let us suppose that the number whose square root is to be found is stored in memory location 9000H and the result is to be stored in memory location 9100H.

Algorithm:

  1. Start.
  2. Load into register pair HL from memory address 9000H.
  3. Initialize accumulator A with 00.
  4. Move contents of memory M into register C.
  5. Add the contents of memory M with the contents of accumulator A and store the result in accumulator A.
  6. Decrement the register C by 1.
  7. If no zero is present, go to step 5 else go to step 8.
  8. Store the contents of accumulator into memory location 9100H.
  9. Terminate the program.

Program Code:


LXI H, 9000H
MVI A, 00H
MOV C, M
UP:
ADD M
DCR C
JNZ UP
STA 9100H
HLT


We can obtain the square root of a number by adding the number with itself for its own number of times. For eg.
To find the value of 22, we add 2 twice i.e. 2+2 =4
To find the value of 32, we add 3 thrice i.e. 3+3+3 = 9
To find the value of 42, we add 4 four times i.e. 4+4+4+4 = 16
and so on.
Register pair HL is loaded with memory location 9000H (i.e. HL points to the memory location 9000H). Accumulator is initialized with 00 to avoid addition of garbage values and to store the correct result. The contents of memory M is moved into register C (here, the move instruction makes a copy of the content of memory and places the copy in the C register) and the value in register C is used as a counter. Now we repeatedly add the contents of memory M with the contents of accumulator A and decrement the counter (register C) by 1 until the counter is 0. Then, the content of the accumulator which is the desired result is stored into memory location 9100H.

Output:


Example 1
Example 2

                                                                                        

In these examples, the data in memory address 9000 is the data whose square is to be found and the data in memory address 9100 is the squared value.


4 comments:

  1. Thanks brother for the awsome explanation. Love form India.

    ReplyDelete
  2. thank you great work keep putting stuffs for students like us...love from india :)

    ReplyDelete
  3. how to find square of a number using subroutine

    ReplyDelete
  4. it fail in finding square of 4 5 6 7 8 9......
    it just work for 1 2 & 3 why please give me answer.

    ReplyDelete