Array input/output
A program that stores 10 elements in an array, reads them from the keyboard and then prints them.
Program source code
title .model small .stack 100h data segment public askUser db "Enter 10 array elements:\0" element db "\nElement [%d]: \0" outputMsg db "\nArray elements: \0" fmtinput db "%d" fmtoutput db "%d \0" array dw 10 dup(?) 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 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 ; Save the cx counter register value on the stack lea ax, ds:[element] ; Load the address of the element format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack 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 add sp, 2 ; Align the stack incw si ; Call the incw macro. Increment the si index by 2 inc cx ; Increment the cx index register by 1 cmp cx, 10 ; Compare the value in the counter register with the number 10 jbe InputArrayLoop ; Loop while CX is less than or equal to 10 lea ax, ds:[outputMsg] ; 1st: Load the address of the outputMsg variable into the ax register call PrintFormatString ; Call the PrintFormatString procedure xor si, si ; Clear the si index mov cx, 1 ; Set the counter to 1 PrintArrayLoop: push word ptr[bx+si] ; 2nd: Copy the value in the bx register at index si onto the stack lea ax, ds:[fmtoutput] ; 1st: Load the address of the fmtoutput format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack incw si ; Call the incw macro. Increment the si index by 2 inc cx ; Increment the cx index register by 1 cmp cx, 10 ; Compare the value in the counter register with the number 10 jbe PrintArrayLoop ; Loop while CX is less than or equal to 10 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 10 array elements:
Element [1]: -1
Element [2]: 20
Element [3]: -30
Element [4]: -45
Element [5]: 50
Element [6]: 60
Element [7]: 70
Element [8]: -80
Element [9]: 90
Element [10]: -1100
Array elements: -1 20 -30 -45 50 60 70 -80 90 -1100
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".