Related Articles
Assembly language program to find largest number in an array
• Last Updated : 22 May, 2018

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 –

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

Program –

2000LXI H 2050H←20, L←50
2003MOV C, MC←M
2004DCR CC←C-01
2005INX HHL←HL+0001
2006MOV A, MA←M
2007INX HHL←HL+0001
2008CMP MA-M
2009JNC 200DIf Carry Flag=0, goto 200D
200CMOV A, MA←M
200DDCR CC←C-1
200EJNZ 2007If Zero Flag=0, goto 2007
2011STA 3050A→3050
2014HLT

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

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