# Assembly language program to find largest number in an array

**Problem –** Determine largest number in an array of n elements. Value of n is stored at address 2050 and array starts from address 2051. Result is stored at address 3050. Starting address of program is taken as 2000.

**Example –**

**Algorithm –**

- We are taking first element of array in A
- Comparing A with other elements of array, if A is smaller then store that element in A otherwise compare with next element
- The value of A is the answer

**Program –**

Memory Address | Mnemonics | Comment |
---|---|---|

2000 | LXI H 2050 | H←20, L←50 |

2003 | MOV C, M | C←M |

2004 | DCR C | C←C-01 |

2005 | INX H | HL←HL+0001 |

2006 | MOV A, M | A←M |

2007 | INX H | HL←HL+0001 |

2008 | CMP M | A-M |

2009 | JNC 200D | If Carry Flag=0, goto 200D |

200C | MOV A, M | A←M |

200D | DCR C | C←C-1 |

200E | JNZ 2007 | If Zero Flag=0, goto 2007 |

2011 | STA 3050 | A→3050 |

2014 | HLT |

**Explanation –** Registers used: **A, H, L, C**

**LXI 2050**assigns 20 to H and 50 to L**MOV C, M**copies content of memory (specified by HL register pair) to C (this is used as a counter)**DCR C**decrements value of C by 1**INX H**increases value of HL by 1. This is done to visit next memory location**MOV A, M**copies content of memory (specified by HL register pair) to A**INX H**increases value of HL by 1. This is done to visit next memory location**CMP M**compares A and M by subtracting M from A.**Carry flag and sign flag becomes set if A-M is negative****JNC 200D**jumps program counter to 200D if carry flag = 0**MOV A, M**copies content of memory (specified by HL register pair) to A**DCR C**decrements value of C by 1**JNZ 2007**jumps program counter to 2007 if zero flag = 0**STA 3050**stores value of A at 3050 memory location**HLT**stops executing the program and halts any further execution

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Assembly language program to find the range of bytes
- Convert C/C++ code to assembly language
- Program to find largest element in an array
- 8086 program to determine largest number in an array of n numbers
- Assembly program to transfer the status of switches
- Number of ways to change the Array such that largest element is LCM of array
- Find largest d in array such that a + b + c = d
- Largest number dividing maximum number of elements in the array
- Find Second largest element in an array
- Find the largest three elements in an array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find the largest contiguous pair sum in given Array
- Find the Largest divisor Subset in the Array
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Find largest element from array without using conditional operator
- Largest palindromic number in an array
- Largest number in the Array having frequency same as value
- Find the largest number that can be formed with the given digits
- Largest Divisor for each element in an array other than 1 and the number itself
- Largest number in an array that is not a perfect cube

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.