汇编语言(二十)之分类统计字符个数
输入一串字符串,分别统计英文字符,数字字符和其他字符的个数
程序运行:
代码:
datas segment
line_max_length db 0ffh
line db 0, 100h dup(?)
letter_count dw 0
digit_count dw 0
other_count dw 0
input db 'input a line:$'
output_letter_count db 0dh,0ah,'letter count:$'
output_digit_count db 0dh,0ah,'digit count:$'
output_other_count db 0dh,0ah,'other count:$'
datas ends
stacks segment stack
db 100h dup(?)
stacks ends
codes segment
assume cs:codes,ds:datas,ss:stacks
main proc far
start:
push ds
mov ax,0h
push ax
mov ax,datas ;初始化ds
mov ds,ax
;输入一行字符提示
lea dx,input
mov ah,9
int 21h
;输入一行字符
lea dx,line_max_length
mov ah,10
int 21h
;对输入的一行字符进行统计
mov cl,line
xor ch,ch
mov si,1
cmp cx,0 ;判断字符串的长度是否为0
jz break ;若为0,跳转
s:
mov al,line[si]
cmp al,'0' ;判断字符是否小于'0'
jb other ;若小于'0',则字符为其他字符
cmp al,'9' ;判断字符是否大于'9'
ja letter_or_other ;若大于'9',则字符为字母或其他字符
inc digit_count ;否则,字符为数字字符,数字记录器加1
jmp next ;跳转
letter_or_other:
and al,11011111b ;将字符转为大写字符
cmp al,'A' ;判断字符是否大于'A'
jb other ;若小于,则字符是其他字符,跳转
cmp al,'Z' ;判断字符是否大于'Z'
ja other ;若大于,则字符为其他字符,跳转
inc letter_count ;否则,字符为字母字符,字母记录器加1
jmp next
other:
inc other_count ;字符为其他字符,其他记录器加1
next:
inc si
loop s
break:
;输出记录器的值
lea dx,output_letter_count
mov ah,9
int 21h
mov ax,letter_count
call decimal
lea dx,output_digit_count
mov ah,9
int 21h
mov ax,digit_count
call decimal
lea dx,output_other_count
mov ah,9
int 21h
mov ax,other_count
call decimal
ret
main endp
decimal proc near
push ax
push cx
push dx
push bx
cmp ax,0
jge no_negative
mov bx,ax
mov dl,'-'
mov ah,2
int 21h
neg bx
mov ax,bx
no_negative:
mov cx,0
mov bx,10
de:
xor dx,dx
div bx
push dx
inc cx
cmp ax,0
jnz de
de1:
pop dx
add dl,30h
mov ah,2
int 21h
loop de1
pop bx
pop dx
pop cx
pop ax
ret
decimal endp
codes ends
end main