100 到 200 之间能被 9 整除的数

查找 100 到 200 范围内所有能被 9 整除的数并计算它们之和的程序。

程序源代码

title Author: Dovgopolov Evgeny Sergeevich

.model small
.stack 100h

data segment public
    string db "Numbers from 100 to 200 divisible by 9:\0"
    result db "\nSum: %d\0"
    fmtstr db "\n[%d]\0"
    numsum dw ?
data ends

code segment public                ; 代码段的开始
    assume  ds:data, cs:code
    include Handyfar.inc           ; 引入包含函数的文件

    main proc                      ; 过程 main 的开始
        call    SetDataSegment     ; 调用过程 SetDataSegment

        lea     ax, ds:[string]    ; 把变量 string 的地址装入寄存器 ax
        call    WriteString        ; 调用过程 WriteString

        mov     cx, 101            ; 把计数器设为 101
        mov     bx, 9              ; 把值 9 复制到寄存器 bx

    MainLoop:
        mov     ax, cx             ; 把计数器 cx 的值复制到寄存器 ax
        cwd                        ; 对寄存器 dx 进行符号扩展。寄存器 dx 被清零
        div     bx                 ; 把寄存器 ax 中的值除以 bx 中的值

        test    dl, dl             ; 检查寄存器 dl 中的值是否为 0
        jnz     Skip               ; 如果 ZF=0,则跳转到标号 Skip

        push    cx                 ; 第 2 个:把计数寄存器 cx 的值保存在堆栈中
        lea     ax, ds:[fmtstr]    ; 第 1 个:把格式字符串 fmtstr 的地址装入寄存器 ax
        call    PrintFormatString  ; 调用过程 PrintFormatString
        add     sp, 2              ; 对齐栈

        add     numsum, cx         ; 把计数器 cx 的值加到变量 numsum

    Skip:
        inc     cx                 ; 把计数寄存器 cx 增加 1
        cmp     cx, 200            ; 把计数寄存器 cx 的值与数字 200 进行比较
        jbe     MainLoop           ; 如果 CX 的值小于或等于 200,则跳转到 MainLoop

        push    numsum             ; 第 2 个:把变量 numsum 的值保存在堆栈中
        lea     ax, ds:[result]    ; 第 1 个:把变量 result 的地址装入寄存器 ax
        call    PrintFormatString  ; 调用过程 PrintFormatString
        add     sp, 2              ; 对齐栈

        xor     al, al             ; 把寄存器 al 清零
        call    ExitProcess        ; 调用过程 ExitProcess
    main endp                      ; 过程 main 的结束

code ends                          ; 代码段的结束
end main	                       ; 文件结束。入口点指定为过程 main

Numbers from 100 to 200 divisible by 9:
[108]
[117]
[126]
[135]
[144]
[153]
[162]
[171]
[180]
[189]
[198]

示例取自教材 "HANDYFAR - MASM(多夫戈波洛夫·叶夫根尼·谢尔盖耶维奇)".