汇编实验
题目: 分类统计字符个数
内容: 程序接受用户从键盘输入的一行字符(字符个数不超过 80 个字符, 该
字符串以回车符结束), 并按字母、 数字及其他字符分类统计个数, 然后将相
应的结果存放于 letter、 digit 和 other 中, 并在显示器上显示如下信息:
The counted result of the program:
letter:× ×
digit:× ×
other:× ×
× × 表示一个十进制数
代码
assume cs:code,ss:stack,ds:data
data segment
db 0,0,0
note db 'Please enter the sentence:',0dh,0ah,'$'
str1 db 'The counted result of the program:',0dh,0ah,'$'
str2 db 'letter:','$'
str3 db 'digit :','$'
str4 db 'other :','$'
data ends
stack segment
db 100 dup(0)
stack ends
code segment
start:
mov ax,data
mov ds,ax
mov ah,09h
mov dx,offset note
int 21h ;打印note字符串
mov ax,stack
mov ss,ax
mov sp,100
input:
read:
mov ah,01h ;输入字符
int 21h ;输入一个字符,存入al
cmp al,0dh ;判断字符是不是回车
jz outstr ;若是回车,跳转到next,句子输入完毕
cmp al,'0' ;与0作比较
jl other ;比0小,跳转到other,属于其他字符,存储在ds:[2]中
cmp al,'9' ;比较字符与9的关系
jg nextread ;如果比9大,判定不属于数字,继续判断
jl num ;若比9小,则属于数字
num:
inc byte ptr ds:[1]
jmp read
other:
inc byte ptr ds:[2] ;d累加加一
jmp read ;继续读取下一个字符
nextread:
cmp al,'A' ;判断与A的关系
jl other ;比A小,该字符>9但小于A,属于其他字符
cmp al,'Z' ;若比A大,就判断字符与Z的关系
jnle next1 ;>Z,就jmp到next1,继续判断
inc byte ptr ds:[0] ;若比Z小,则在A到Z当中,属于大写字母,ds[0]++
jmp read ;继续读取下一个字符
next1: ;用来判断是其他字符还是小写字母
cmp al,'a'
jl other ;如果比a小,则是其他字符,跳到other
cmp al,'z' ;若比a大,继续判断与z的关系
inc byte ptr ds:[0] ;如果比z小,用来存小写字母
jmp read
jmp other ;若比z大,则属于其他字符,进行跳转
outstr:
mov ah,09h
mov dx,offset str1
int 21h ;输出提示字符串
a:
mov ah,09h
mov dx,offset str2
int 21h ;输出字符串
mov al,ds:[0]
mov ah,0 ;将letter的个数送到ax
call print ;调用print子程序
b:
mov ah,09h
mov dx,offset str3
int 21h ;输出digit字符串
mov al,ds:[1]
mov ah,0 ;将digit的个数送到ax
call print ;输出个数
c:
mov ah,09h
mov dx,offset str4
int 21h ;输出字符串“other:”
mov al,ds:[2]
mov ah,0 ;将str4的个数送到ax
call print ;输出个数
exit:
mov ah,4ch
int 21h ;结束返回
print: ;输出个数
mov bx,10 ;dx=10,除以10
mov cx,0 ;cx记录位数,最后输出字符的个数,记录循环次数
mov dx,0 ;dx记录余数
s0: ;将单个数字依次入栈
div bx ;ax=ax/10商,dx=ax/10余数
push dx ;将余数dx入栈
inc cx ;位数加一
cwd ;将ax扩展到dx.ax
cmp ax,0 ;将剩下的数ax与0比较
jne s0 ;若ax!=0,继续循环
s1: ;输出字符,cx为循环次数,一个数有多少位
pop dx ;栈顶的数据出栈,给dx
add dl,30h ;转化为字符
mov ah,02h
int 21h ;dl字符输出
loop s1 ;循环cx次
mov ah,02h
mov dl,0dh
int 21h ;打印回车
mov ah,02h
mov dl,0ah
int 21h ;换行
ret
code ends
end start
实验结果