数组输入与输出

在数组中保存 10 个元素、从键盘输入它们并在之后输出的程序。

程序源代码

title Author: Dovgopolov Evgeny Sergeevich

.model small
.stack 100h

data segment public
    askUser   db "Enter 10 array elements:\0"
    element   db "\nElement [%d]: \0"
    outputMsg db "\nArray elements: \0"
    fmtinput  db "%d"
    fmtoutput db "%d \0"
    array     dw 10 dup(?)
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     bx, ds:[array]       ; 把变量 array 的地址装入寄存器 bx
        mov     cx, 1                ; 把计数器设为 1

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

        lea     ax, word ptr[bx+si]  ; 把寄存器 bx 中索引 si 处的地址装入寄存器 ax
        push    ax                   ; 第 2 个:把寄存器 ax 的值保存在堆栈中
        lea     ax, ds:[fmtinput]    ; 第 1 个:把格式字符串 fmtinput 的地址装入寄存器 ax
        call    ScanFormatString     ; 调用过程 ScanFormatString
        add     sp, 2                ; 对齐堆栈

        incw    si                   ; 调用宏 incw。把索引 si 增加 2
        inc     cx                   ; 把索引寄存器 cx 增加 1
        cmp     cx, 10               ; 把计数寄存器中的值与数值 10 进行比较
        jbe     InputArrayLoop       ; 当 CX 小于或等于 10 时执行循环

        lea     ax, ds:[outputMsg]   ; 第 1 个:把变量 outputMsg 的地址装入寄存器 ax
        call    PrintFormatString    ; 调用过程 PrintFormatString

        xor     si, si               ; 把索引 si 清零
        mov     cx, 1                ; 把计数器设为 1

    PrintArrayLoop:
        push    word ptr[bx+si]      ; 第 2 个:把寄存器 bx 中索引 si 处的值复制到堆栈
        lea     ax, ds:[fmtoutput]   ; 第 1 个:把格式字符串 fmtoutput 的地址装入寄存器 ax
        call    PrintFormatString    ; 调用过程 PrintFormatString
        add     sp, 2                ; 对齐堆栈

        incw    si                   ; 调用宏 incw。把索引 si 增加 2
        inc     cx                   ; 把索引寄存器 cx 增加 1
        cmp     cx, 10               ; 把计数寄存器中的值与数值 10 进行比较
        jbe     PrintArrayLoop       ; 当 CX 小于或等于 10 时执行循环

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

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

Enter 10 array elements:
Element [1]: -1
Element [2]: 20
Element [3]: -30
Element [4]: -45
Element [5]: 50
Element [6]: 60
Element [7]: 70
Element [8]: -80
Element [9]: 90
Element [10]: -1100
Array elements: -1 20 -30 -45 50 60 70 -80 90 -1100

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