数组复制
把一个数组的元素复制到另一个数组的程序。
程序源代码
title .model small .stack 100h data segment public askUser db "Enter the number of array elements: \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 ; 代码段的开始 assume ds:data, cs:code include Handyfar.inc ; 引入包含函数的文件 include Handymac.inc ; 引入包含宏的文件 main proc ; 过程 main 的开始 call SetDataSegment ; 调用过程 SetDataSegment lea ax, ds:[askUser] ; 把变量 askUser 的地址装入寄存器 ax call WriteString ; 调用过程 WriteString lea ax, ds:[arraySize] ; 把变量 arraySize 的地址装入寄存器 ax push ax ; 第 2 个:把寄存器 ax 的值保存在堆栈中 lea ax, ds:[fmtinput] ; 第 1 个:把格式字符串 fmtinput 的地址装入寄存器 ax call ScanFormatString ; 调用过程 ScanFormatString lea bx, ds:[array1] ; 把变量 array 的地址装入寄存器 bx mov cx, 1 ; 把计数器设为 1 InputArrayLoop: push cx ; 第 2 个:把计数寄存器 cx 的值保存在堆栈中 lea ax, ds:[element] ; 第 1 个:把格式字符串 element 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString lea ax, word ptr[bx+si] ; 把寄存器 bx 中索引 si 处的地址装入寄存器 ax push ax ; 第 2 个:把寄存器 ax 的值保存在堆栈中 lea ax, ds:[fmtinput] ; 第 1 个:把格式字符串 fmtinput 的地址装入寄存器 ax call ScanFormatString ; 调用过程 ScanFormatString incw si ; 调用宏 incw。把索引 si 增加 2 inc cx ; 把索引寄存器 cx 增加 1 cmp cx, arraySize ; 把计数寄存器中的值与 arraySize 进行比较 jbe InputArrayLoop ; 当 CX 小于或等于 arraySize 中的值时执行循环 xor si, si ; 把索引 si 清零 xor di, di ; 把索引 di 清零 lea bp, ds:[array2] ; 把变量 array2 的地址装入寄存器 bp mov cx, 1 ; 把计数器设为 1 CopyArrayLoop: mov ax, word ptr[bx+si] ; 把寄存器 bx 中索引 si 处的值复制到寄存器 ax mov word ptr[bp+di], ax ; 把寄存器 ax 的值复制到寄存器 bp 索引 di 处 incw si ; 调用宏 incw。把索引 si 增加 2 incw di ; 调用宏 incw。把索引 di 增加 2 inc cx ; 把索引寄存器 cx 增加 1 cmp cx, arraySize ; 把计数寄存器中的值与 arraySize 进行比较 jbe CopyArrayLoop ; 当 CX 小于或等于 arraySize 中的值时执行循环 lea ax, ds:[firstArray] ; 第 1 个:把变量 firstArray 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString xor si, si ; 把索引 si 清零 mov cx, 1 ; 把计数器设为 1 PrintFirstArrayLoop: push word ptr[bx+si] ; 第 2 个:把寄存器 bx 中索引 si 处的值复制到堆栈 lea ax, ds:[outputArray] ; 第 1 个:把格式字符串 outputArray 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString incw si ; 调用宏 incw。把索引 si 增加 2 inc cx ; 把索引寄存器 cx 增加 1 cmp cx, arraySize ; 把计数寄存器中的值与 arraySize 进行比较 jbe PrintFirstArrayLoop ; 当 CX 小于或等于 arraySize 中的值时执行循环 lea ax, ds:[secondArray] ; 第 1 个:把变量 secondArray 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString xor di, di ; 把索引 di 清零 mov cx, 1 ; 把计数器设为 1 PrintSecondArrayLoop: push word ptr[bp+di] ; 第 2 个:把寄存器 bp 中索引 di 处的值复制到堆栈 lea ax, ds:[outputArray] ; 第 1 个:把格式字符串 outputArray 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString incw di ; 调用宏 incw。把索引 di 增加 2 inc cx ; 把索引寄存器 cx 增加 1 cmp cx, arraySize ; 把计数寄存器中的值与 arraySize 进行比较 jbe PrintSecondArrayLoop ; 当 CX 小于或等于 arraySize 中的值时执行循环 xor al, al ; 把寄存器 al 清零 call ExitProcess ; 调用过程 ExitProcess main endp ; 过程 main 的结束 code ends ; 代码段的结束 end main ; 文件结束。入口点指定为过程 main
Enter the number of array elements: 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]
示例取自教材 "HANDYFAR - MASM(多夫戈波洛夫·叶夫根尼·谢尔盖耶维奇)".