Skip to main content

8086 ALP: largest and smallest, display

Short answer

For array of 10 bytes, initialize largest in BL and smallest in BH with first element, compare remaining nine using unsigned JBE/JAE, update. Clear screen, display both in decimal. For sample array largest=120, smallest=3.

Write an assembly language program for 8086 to find the largest and smallest numbers of an array having 10 numbers. Display the found numbers in the clear screen.

Method: the first element is taken as both the largest (BL) and the smallest (BH); the remaining nine are compared one by one and BL/BH are updated (unsigned comparison). The screen is cleared and both values are displayed in decimal with the helper PRINT_DEC.

.MODEL SMALL
.STACK 100H

DISPSTR MACRO MSG            ; display a '$'-terminated string
        LEA  DX,MSG
        MOV  AH,09H
        INT  21H
        ENDM

CLRSCR  MACRO                ; clear screen and home the cursor
        MOV  AX,0600H        ; scroll whole window (AL=0 -> blank it)
        MOV  BH,07H          ; normal attribute
        MOV  CX,0000H        ; upper-left corner (row 0, col 0)
        MOV  DX,184FH        ; lower-right corner (row 24, col 79)
        INT  10H
        MOV  AH,02H          ; set cursor position
        MOV  BH,00H          ; page 0
        MOV  DX,0000H        ; row 0, col 0
        INT  10H
        ENDM

.DATA
    ARR   DB 25,7,98,64,3,120,45,88,17,60
    MSG1  DB 'Largest  = $'
    MSG2  DB 'Smallest = $'
    CRLF  DB 13,10,'$'

.CODE
MAIN PROC
    MOV  AX,@DATA
    MOV  DS,AX

    LEA  SI,ARR            ; SI -> array
    MOV  BL,[SI]           ; BL = largest (first element)
    MOV  BH,BL             ; BH = smallest
    INC  SI
    MOV  CX,9              ; 9 more elements
NEXT:
    MOV  AL,[SI]
    CMP  AL,BL
    JBE  NOTMAX            ; AL <= largest
    MOV  BL,AL             ; new largest
NOTMAX:
    CMP  AL,BH
    JAE  NOTMIN            ; AL >= smallest
    MOV  BH,AL             ; new smallest
NOTMIN:
    INC  SI
    LOOP NEXT

    PUSH BX                ; CLRSCR uses BH, so keep largest/smallest safe
    CLRSCR                 ; clear screen
    POP  BX
    DISPSTR MSG1
    MOV  AL,BL
    XOR  AH,AH
    CALL PRINT_DEC         ; show largest
    DISPSTR CRLF
    DISPSTR MSG2
    MOV  AL,BH
    XOR  AH,AH
    CALL PRINT_DEC         ; show smallest
    MOV  AH,4CH            ; return to DOS
    INT  21H
MAIN ENDP

; ---- print the unsigned number in AX in decimal (registers preserved)
PRINT_DEC PROC
    PUSH AX
    PUSH BX
    PUSH CX
    PUSH DX
    MOV  BX,10
    XOR  CX,CX             ; digit counter
PD1:XOR  DX,DX
    DIV  BX                ; AX = quotient, DX = remainder (digit)
    PUSH DX
    INC  CX
    OR   AX,AX
    JNZ  PD1
PD2:POP  DX                ; digits come out in correct order
    ADD  DL,30H            ; to ASCII
    MOV  AH,02H
    INT  21H
    LOOP PD2
    POP  DX
    POP  CX
    POP  BX
    POP  AX
    RET
PRINT_DEC ENDP

END MAIN

Output: Largest = 120 and Smallest = 3. (For signed numbers use JLE/JGE instead of JBE/JAE.)

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 3 questions →