Assembly Program to find Smallest Number in an Array


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

The basic logic in the program is that we assign the first element of the array to the SMALLEST and start iteration from element 2. If the element 2 is smaller than the value assigned to SMALLEST, we update SMALLEST otherwise go to element 3 and compare SMALLEST 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
SMALLEST DB ?
.CODE
MAIN PROC
    
    MOV AX, @DATA
    MOV DS, AX
    
    MOV BX, 0
    
    MOV AL, ARRAY[BX]
    
    MOV SMALLEST, AL
    
    COMPARE:
    
    INC BX
    
    CMP BX, 10
    
    JE EXIT
    
    MOV AL, ARRAY[BX]
    
    CMP AL, SMALLEST
    
    JL UPDATE_SMALLEST
    
    JMP COMPARE
    
    UPDATE_SMALLEST:
    
    MOV SMALLEST, AL
    
    JMP COMPARE
    
    EXIT:
    
    MAIN ENDP
END MAIN

Output:


No comments