Maximum array element
A program that searches for the largest element in an array.
Program source code
title .model small .stack 100h data segment public strout db "Array elements: \0" strres db "\nThe largest element in the array: %d\0" array dw 54, 26, 8831, 23, 16, 76, 84, 13, 456, 3 arrlen equ ($-array) / 2 ; Compute the array length fmtout db "%d \0" largest dw ? data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions include Handymac.inc ; Include the file with macros main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure lea ax, ds:[strout] ; Load the address of the strout variable into the ax register call WriteString ; Call the WriteString procedure lea bx, ds:[array] ; Load the address of the array variable into the bx register mov cx, arrlen ; Set the counter equal to the length of the array PrintArrayLoop: push word ptr[bx+si] ; 2nd: Copy the value in the bx register at index si onto the stack lea ax, ds:[fmtout] ; 1st: load the address of the fmtout format string into ax call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack incw si ; Call the incw macro. Increment the si index by 2 loop PrintArrayLoop ; Loop while CX≠0 xor si, si ; Clear the si index register mov cx, arrlen ; Set the counter equal to the length of the array FindLargestLoop: mov ax, word ptr[bx+si] ; Copy the value in the bx register at index si into the ax register cmp ax, largest ; Compare the value in ax with the value in largest jb Skip ; If the value in ax is below the value in largest, then jump to Skip mov largest, ax ; Copy the ax register value into the largest variable Skip: incw si ; Call the incw macro. Increment the si index by 2 loop FindLargestLoop ; Loop while CX≠0 push largest ; 2nd: Copy the value of the largest variable onto the stack lea ax, ds:[strres] ; 1st: Load the address of the strres format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Array elements: 54 26 8831 23 16 76 84 13 456 3
The largest element in the array: 8831
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".