Array sorting and squaring
A program that sorts an array with the bubble method and squares each element.
Program source code
title .model small .stack 100h data segment public array dw -3, 4, 2, 6, 1, -7, 5 arrlen equ ($-array) / 2 fmtout db "[%d] \0" 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 mov cx, arrlen ; Set the counter equal to the length of the array lea si, ds:[array] ; Load the address of the array into the si register call SortAndSquare ; Call the SortAndSquare procedure MainLoop: push word ptr[si] ; 2nd: Save the si register value on 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 MainLoop ; Loop while CX≠0 xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure SortAndSquare proc ; Start of the SortAndSquare procedure push bp mov bp, sp sub sp, 4 mov word ptr ss:[bp-4], cx ; Save the cx register value in the local stack area mov word ptr ss:[bp-2], si ; Save the si register value in the local stack area xchg bx, si ; Exchange the contents of the si and bx registers xor si, si ; Clear the si index register ToSquareLoop: mov ax, word ptr[bx+si] ; Copy the value in the bx register at index si into the ax register imul ax ; Perform signed multiplication of the value in ax by ax mov word ptr[bx+si], ax ; Copy the value in the ax register into the bx register at index si incw si ; Call the incw macro. Increment the si index by 2 loop ToSquareLoop ; Loop while CX≠0 mov cx, word ptr ss:[bp-4] ; Copy the value from the local stack area into the cx register mov bx, word ptr ss:[bp-2] ; Copy the value from the local stack area into the bx register dec cx ; Decrement the cx counter register by 1 mov dx, cx ; Save the cx counter register value in the dx register OuterSortLoop: push cx ; Save the cx counter register value on the stack xor si, si ; Clear the si index register mov cx, dx ; Use the dx register value as the counter InnerSortLoop: mov ax, word ptr[bx+si] ; Copy the value in the bx register at index si into the ax register cmp ax, word ptr[bx+si+2] ; Compare the value in ax with the value in bx at index si+2 jle SkipSorting ; If the value is less than or equal, then jump to the label xchg word ptr[bx+si+2], ax ; Exchange the contents of ax with the bx register at index si+2 mov word ptr[bx+si], ax ; Copy the ax register value into the bx register at index si SkipSorting: incw si ; Call the incw macro. Increment the si index by 2 loop InnerSortLoop ; Loop while CX≠0 pop cx ; Pop the value from the stack into the cx register loop OuterSortLoop ; Loop while CX≠0 mov cx, word ptr ss:[bp-4] ; Copy the value from the local stack area into the cx register mov si, word ptr ss:[bp-2] ; Copy the value from the local stack area into the si register add sp, 4 pop bp ret SortAndSquare endp ; End of the SortAndSquare procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
[1] [4] [9] [16] [25] [36] [49]
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".