Monday, October 26, 2015

8085 Program to Find the Greatest Data

Let us suppose that we have 10 data’s which are stored from memory address 9000H to 9009H. We find the largest number from these data’s and store the value in 9100H.

Algorithm:

  1.   Start.
  2. Load into register pair HL from memory location 9000H.
  3. Move 09 into register C.
  4. Move the contents of memory M into accumulator A.
  5. Increment register pair HL by 1.
  6. Compare the contents of memory M with the contents of the accumulator A.
  7. If no carry is present go to step 9 else go to 8.
  8. Move the contents of memory M into accumulator A.
  9. Decrement register C by 1.
  10. If no zero is present, go to step 5.
  11. Store the contents of accumulator A into memory location 9100H.
  12. Terminate the program.

Program Code:


LXI H, 9000H
MVI C, 09H
MOV A, M
UP:
INX H
CMP M
JNC DOWN
MOV A, M
DOWN:
DCR C
JNZ UP
STA 9100H
HLT

Register pair HL is loaded from the memory location 9000H. 09 is moved into register C to use it as counter as we need to make 9 comparisons. The content of memory is moved into the accumulator A (meaning that the first data is now in accumulator A). The register pair HL is incremented by 1(meaning that HL now points to the next data). The content of accumulator is compared with the content of memory. During comparison, if accumulator A < (is less than) content of memory M then the carry flag is set. So if carry flag is set then the content of memory is moved into accumulator A, else this instruction is skipped and register C is decreased by 1. We again increment the register pair HL by 1 and perform these same instructions as long as the counter is greater than 0. Then the content of accumulator A, which is the highest number, is stored in memory location 9100H.

Output:


Example 1



In this example we can see that the greatest number (i.e. in address 9100) is 96. If we see the data’s then the greatest number is 99. But since we compare only 10 data’s i.e. data’s from address 9000 to 9009 so we don’t compare 99. Therefore, 96 is the highest data.

No comments:

Post a Comment