打印二维数组
使用按行和按列的嵌套循环输出二维数组元素的程序。
程序源代码
title .model small .stack 100h data segment public array dw 10, 20, 30, 40, 50, 60 dw 70, 80, 90, 10, 20, 30 dw 40, 50, 60, 70, 80, 90 dw 10, 20, 30, 40, 50, 60 rows dw 4 cols dw 6 fmtout db "%d \0" data ends code segment public ; 代码段的开始 assume ds:data, cs:code include Handyfar.inc ; 引入包含函数的文件 include Handymac.inc ; 引入包含宏的文件 main proc ; 过程 main 的开始 call SetDataSegment ; 调用过程 SetDataSegment lea bx, ds:[array] ; 把变量 array 的地址装入寄存器 bx mov cx, rows ; 把计数器设为变量 rows 中的值 PrintRowsLoop: push cx ; 把计数寄存器 cx 的值保存在堆栈中 mov cx, cols ; 把计数器设为变量 cols 中的值 PrintColsLoop: push word ptr[bx+si] ; 第 2 个:把寄存器 bx 中索引 si 处的值压入堆栈 lea ax, ds:[fmtout] ; 第 1 个:把格式字符串 fmtout 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString add sp, 2 ; 对齐栈 incw si ; 调用宏 incw。把索引 si 增加 2 loop PrintColsLoop ; 当 CX≠0 时执行循环 pop cx ; 从堆栈弹出值到寄存器 cx call PrintNewLine ; 调用过程 PrintNewLine loop PrintRowsLoop ; 当 CX≠0 时执行循环 xor al, al ; 把寄存器 al 清零 call ExitProcess ; 调用过程 ExitProcess main endp ; 过程 main 的结束 code ends ; 代码段的结束 end main ; 文件结束。入口点指定为过程 main
10 20 30 40 50 60
70 80 90 10 20 30
40 50 60 70 80 90
10 20 30 40 50 60
示例取自教材 "HANDYFAR - MASM(多夫戈波洛夫·叶夫根尼·谢尔盖耶维奇)".