Sum of array elements
A program that finds the sum of all array elements entered by the user.
Program source code
title .model small .stack 100h data segment public askUser db "Enter the number of elements in the array: \0" element db "\nElement [%d]: \0" output db "\nTotal sum of all array elements: %d\0" fmtinput db "%d" array dw 1000 dup(?) arraySize dw ? numsum 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:[askUser] ; Load the address of the askUser variable into ax call WriteString ; Call the WriteString procedure lea ax, ds:[arraySize] ; Load the address of the arraySize variable into the ax register push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtinput] ; 1st: load the address of the fmtinput format string into ax call ScanFormatString ; Call the ScanFormatString procedure lea bx, ds:[array] ; Load the address of the array variable into the bx register mov cx, 1 ; Set the counter to 1 InputArrayLoop: push cx ; 2nd: Save the cx counter register value on the stack lea ax, ds:[element] ; 1st: Load the address of the element format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure lea ax, word ptr[bx+si] ; Load the address of the bx register at index si into the ax register push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtinput] ; 1st: load the address of the fmtinput format string into ax call ScanFormatString ; Call the ScanFormatString procedure incw si ; Call the incw macro. Increment the si index by 2 inc cx ; Increment the cx index register by 1 cmp cx, arraySize ; Compare the value in the counter register with arraySize jbe InputArrayLoop ; Loop while CX is less than or equal to the value in arraySize xor si, si ; Clear the si index mov cx, 1 ; Set the counter to 1 CalculateLoop: mov ax, word ptr[bx+si] ; Copy the value in the bx register at index si into the ax register add numsum, ax ; Add the value in the ax register to the numsum variable incw si ; Call the incw macro. Increment the si index by 2 inc cx ; Increment the cx counter register by 1 cmp cx, arraySize ; Compare the value in the counter register with arraySize jbe CalculateLoop ; Loop while CX is less than or equal to the value in arraySize push numsum ; 2nd: Copy the value of the numsum variable onto the stack lea ax, ds:[output] ; 1st: Load the address of the output format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure 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
Enter the number of elements in the array: 4
Element [1]: 1
Element [2]: 2
Element [3]: 3
Element [4]: 4
Total sum of all array elements: 10
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".