乘法表
输出指定数字 1 到 9 的乘法表的程序。
程序源代码
title .model small .stack 100h data segment public askUser db "Enter a positive number: \0" output db "\n%d * %d = %d\0" fmtinput db "%d" number dw ? data ends code segment public ; 代码段的开始 assume ds:data, cs:code include Handyfar.inc ; 引入包含函数的文件 main proc ; 过程 main 的开始 call SetDataSegment ; 调用过程 SetDataSegment lea ax, ds:[askUser] ; 把变量 askUser 的地址装入寄存器 ax call WriteString ; 调用过程 WriteString lea ax, ds:[number] ; 把变量 number 的地址装入寄存器 ax push ax ; 第 2 个:把寄存器 ax 的值保存在堆栈中 lea ax, ds:[fmtinput] ; 第 1 个:把格式字符串 fmtinput 的地址装入寄存器 ax call ScanFormatString ; 调用过程 ScanFormatString add sp, 2 ; 对齐栈 mov cx, 1 ; 把计数器设为 1 MulitplyLoop: mov ax, number ; 把变量 number 的值复制到寄存器 ax mul cx ; 把寄存器 ax 中的值乘以 cx 中的值 push ax ; 第 4 个:把寄存器 ax 的值(乘法结果)保存在堆栈中 push cx ; 第 3 个:把计数寄存器 cx 的值保存在堆栈中 push number ; 第 2 个:把变量 number 的值保存在堆栈中 lea ax, ds:[output] ; 第 1 个:把格式字符串 output 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString add sp, 6 ; 对齐栈 inc cx ; 把计数寄存器 cx 的值增加 1 cmp cx, 9 ; 把 cx 的值与数字 9 进行比较 jbe MulitplyLoop ; 如果 CX 的值小于或等于 9,则跳转到 MulitplyLoop xor al, al ; 把寄存器 al 清零 call ExitProcess ; 调用过程 ExitProcess main endp ; 过程 main 的结束 code ends ; 代码段的结束 end main ; 文件结束。入口点指定为过程 main
Enter a positive number: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
示例取自教材 "HANDYFAR - MASM(多夫戈波洛夫·叶夫根尼·谢尔盖耶维奇)".