Search for the smallest element in an array

In the following program example we will search for the smallest element in an array and save the result in a variable.

Program source code

; Author: Evgeny Sergeevich Dovgopolov

array:  db 23h, 15h, 37h, 42h, 51h, 68h, 74h, 89h, 92h, 10h
result: db 00h

main:
    lxi     h, array          ; Load the address of the variable array into the HL register pair
    mvi     c, 09h            ; Set the loop counter C to 9

    mov     a, m              ; Assume that the first array element is the smallest element
    mov     b, a              ; Copy the value of register A into register B

FindSmallestLoop:
    inx     h                 ; Increment the HL register pair by 1
    mov     a, m              ; Copy the memory value pointed to by the HL register pair into A
    cmp     b                 ; Compare the value in register A with the value in register B
    jc      UpdateSmallest    ; If C=1, then jump to the label UpdateSmallest

    jmp     ContinueLoop      ; Perform an unconditional jump to the label ContinueLoop

UpdateSmallest:
    mov     b, a              ; Copy A into B. Update the smallest array element

ContinueLoop:
    dcr     c                 ; Decrement the C counter register by 1
    jnz     FindSmallestLoop  ; Jump to the label FindSmallestLoop until C becomes 0

    mov     a, b              ; Copy the value of register B into register A
    sta     result            ; Save the value of register A in memory (result)

    hlt                       ; Stop program execution

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".