统计字符串中的字符数
程序统计给定字符串中句点、逗号、空格和大写字母出现的次数。
程序源代码
title .model small .stack 100h data segment public string db "Hi. My name is Evgeny. I'm 37, and you?\0" strlen equ ($-string) - 2 ; 计算数组的长度 output db "Number of dots: %.4d\n" db "Number of commas: %.2d\n" db "Number of spaces: %.1d\n" db "Number of uppercase letters: %d\0" dots dw 0 commas dw 0 spaces dw 0 uppers dw 0 data ends code segment public ; 代码段的开始 assume ds:data, cs:code include Handyfar.inc ; 引入包含函数的文件 main proc ; 过程 main 的开始 call SetDataSegment ; 调用过程 SetDataSegment lea si, ds:[string] ; 把变量 string 的地址装入寄存器 si mov cx, strlen ; 把计数器设为 string 的长度 CountSymbolsLoop: lodsb ; 把源寄存器 si 中的字节装入寄存器 al cmp al, 2Eh ; 把寄存器 al 中的值与字符 . 进行比较 jz IncrementDots ; 如果 ZF=1,则跳转到标号 IncrementDots cmp al, 2Ch ; 把寄存器 al 中的值与字符 , 进行比较 jz IncrementCommas ; 如果 ZF=1,则跳转到标号 IncrementCommas cmp al, 20h ; 把寄存器 al 中的值与字符 ' ' 进行比较 jz IncrementSpaces ; 如果 ZF=1,则跳转到标号 IncrementSpaces jmp CheckUpperCase ; 无条件跳转到标号 CheckUpperCase IncrementDots: inc dots ; 增加变量 dots 的值 jmp Next ; 无条件跳转到标号 Next IncrementCommas: inc commas ; 增加变量 commas 的值 jmp Next ; 无条件跳转到标号 Next IncrementSpaces: inc spaces ; 增加变量 spaces 的值 jmp Next ; 无条件跳转到标号 Next CheckUpperCase: cmp al, 0C0h ; 把寄存器 al 中的值与西里尔大写字母(0C0h)进行比较 jae CheckHigher ; 如果字符不小于西里尔大写字母(0C0h),则跳转到标号 CheckHigher jmp Next ; 无条件跳转到标号 Next CheckHigher: cmp al, 0DFh ; 把寄存器 al 中的值与西里尔大写字母(0DFh)进行比较 jbe IncrementUppers ; 如果字符不大于西里尔大写字母(0DFh),则跳转到标号 IncrementUppers jmp Next ; 无条件跳转到标号 Next IncrementUppers: inc uppers ; 增加变量 uppers 的值 Next: loop CountSymbolsLoop ; 当 CX≠0 时执行循环 push uppers ; 第 5 个:把变量 uppers 的值保存在堆栈中 push spaces ; 第 4 个:把变量 spaces 的值保存在堆栈中 push commas ; 第 3 个:把变量 commas 的值保存在堆栈中 push dots ; 第 2 个:把变量 dots 的值保存在堆栈中 lea ax, ds:[output] ; 第 1 个:把格式字符串 output 的地址装入寄存器 ax call PrintFormatString ; 调用过程 PrintFormatString add sp, 8 ; 对齐堆栈 xor al, al ; 把寄存器 al 清零 call ExitProcess ; 调用过程 ExitProcess main endp ; 过程 main 的结束 code ends ; 代码段的结束 end main ; 文件结束。入口点指定为过程 main
Number of dots: 2
Number of commas: 1
Number of spaces: 8
Number of uppercase letters: 4
示例取自教材 "HANDYFAR - MASM(多夫戈波洛夫·叶夫根尼·谢尔盖耶维奇)".