Array copying
A program that copies elements from one array into another array.
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" firstArray db "\n\nElements of the first array: \0" secondArray db "\nElements of the second array: \0" fmtinput db "%d" outputArray db "[%d] \0" array1 dw 1000 dup(?) array2 dw 1000 dup(?) arraySize 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:[array1] ; 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 xor di, di ; Clear the di index lea bp, ds:[array2] ; Load the address of the array2 variable into the bp register mov cx, 1 ; Set the counter to 1 CopyArrayLoop: mov ax, word ptr[bx+si] ; Copy the value in the bx register at index si into the ax register mov word ptr[bp+di], ax ; Copy the value of the ax register into the bp register at index di incw si ; Call the incw macro. Increment the si index by 2 incw di ; Call the incw macro. Increment the di 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 CopyArrayLoop ; Loop while CX is less than or equal to the value in arraySize lea ax, ds:[firstArray] ; 1st: Load the address of the firstArray 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 PrintFirstArrayLoop: push word ptr[bx+si] ; 2nd: Copy the value in the bx register at index si onto the stack lea ax, ds:[outputArray] ; 1st: Load the address of the outputArray format string into the ax register call PrintFormatString ; Call the PrintFormatString 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 PrintFirstArrayLoop ; Loop while CX is less than or equal to the value in arraySize lea ax, ds:[secondArray] ; 1st: Load the address of the secondArray variable into the ax register call PrintFormatString ; Call the PrintFormatString procedure xor di, di ; Clear the di index mov cx, 1 ; Set the counter to 1 PrintSecondArrayLoop: push word ptr[bp+di] ; 2nd: Copy the value in the bp register at index di onto the stack lea ax, ds:[outputArray] ; 1st: Load the address of the outputArray format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure incw di ; Call the incw macro. Increment the di 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 PrintSecondArrayLoop ; Loop while CX is less than or equal to the value in arraySize 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]: -12
Element [2]: 23
Element [3]: 34
Element [4]: -451
Elements of the first array: [-12] [23] [34] [-451]
Elements of the second array: [-12] [23] [34] [-451]
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".