Assembly Program to find Largest Number in an Array


This program has a byte ARRAY and a variable called the LARGEST. The program will find the largest integer in the ARRAY and will assign it to the variable LARGEST.

The basic logic in the program is that we assign the first element of the array to the LARGEST and start iteration from element 2. If the element 2 is larger than the value assigned to LARGEST, we update LARGEST otherwise go to element 3 and compare LARGEST with element 3 and so on.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
.MODEL SMALL
.STACK 100H
.DATA
ARRAY DB 55, 32, 98, 21, 13, 16, 38, 25, 56, 12
LARGEST DB ?
.CODE
MAIN PROC
    
    MOV AX, @DATA
    MOV DS, AX
    
    MOV BX, 0
    
    MOV AL, ARRAY[BX]
    
    MOV LARGEST, AL
    
    COMPARE:
    
    INC BX
    
    CMP BX, 10
    
    JE EXIT
    
    MOV AL, ARRAY[BX]
    
    CMP AL, LARGEST
    
    JG UPDATE_LARGEST
    
    JMP COMPARE
    
    UPDATE_LARGEST:
    
    MOV LARGEST, AL
    
    JMP COMPARE
    
    EXIT:
    
    MAIN ENDP
END MAIN

Output:


No comments